-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.c
200 lines (163 loc) · 4.64 KB
/
day21.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "advent_of_code.h"
#define MAGIC 0xdead // hoping this is unlikely enough to collide...
typedef union {
int64_t val;
struct {
uint16_t magic;
uint16_t l, r;
char op;
} calc;
} Monkey;
DICT_DEF2(monkey_ids, uint32_t, uint16_t)
int64_t eval(Monkey* mks, uint16_t id, bool resolve_humn);
int64_t inverse(Monkey* mks, uint16_t id, int64_t goal);
void solve_day21(AdventOfCode* aoc, Stream* file_stream) {
FuriString* line = furi_string_alloc();
uint32_t tmp = 0;
int64_t p1 = 0;
int64_t p2 = 0;
Monkey* mks;
// 2500 should be plenty
mks = malloc(sizeof(Monkey) * 2500);
monkey_ids_t ids;
monkey_ids_init(ids);
memcpy(&tmp, "root", 4);
monkey_ids_set_at(ids, tmp, 0);
memcpy(&tmp, "humn", 4);
monkey_ids_set_at(ids, tmp, 1);
int i = 2;
while(true) {
if(!stream_read_line(file_stream, line)) break;
furi_string_trim(line);
char* ptr = (char*)furi_string_get_cstr(line);
memcpy(&tmp, ptr, 4);
uint16_t id;
uint16_t* idp = monkey_ids_get(ids, tmp);
if(idp == NULL) {
id = i;
monkey_ids_set_at(ids, tmp, i++);
} else {
id = *idp;
}
ptr += 6;
Monkey* m = &mks[id];
if('0' <= *ptr && *ptr <= '9') {
m->val = strtol(ptr, NULL, 10);
} else {
m->calc.magic = MAGIC;
m->calc.op = ptr[5];
memcpy(&tmp, ptr, 4);
idp = monkey_ids_get(ids, tmp);
if(idp == NULL) {
m->calc.l = i;
monkey_ids_set_at(ids, tmp, i++);
} else {
m->calc.l = *idp;
}
memcpy(&tmp, ptr + 7, 4);
idp = monkey_ids_get(ids, tmp);
if(idp == NULL) {
m->calc.r = i;
monkey_ids_set_at(ids, tmp, i++);
} else {
m->calc.r = *idp;
}
}
}
char root_op = mks[0].calc.op;
mks[0].calc.op = '-';
eval(mks, 0, false);
p2 = inverse(mks, 0, 0);
mks[0].calc.op = root_op;
p1 = eval(mks, 0, true);
FuriString* part_1 = furi_string_alloc_printf("%lld", p1);
FuriString* part_2 = furi_string_alloc_printf("%lld", p2);
update_results(aoc, part_1, part_2);
monkey_ids_clear(ids);
free(mks);
furi_string_free(part_2);
furi_string_free(part_1);
furi_string_free(line);
}
int64_t eval(Monkey* mks, uint16_t id, bool resolve_humn) {
if(mks[id].calc.magic != MAGIC) return mks[id].val;
array_int_t stack;
array_int_init(stack);
array_int_push_back(stack, id);
while(!array_int_empty_p(stack)) {
int i = *array_int_back(stack);
int64_t lv = 0;
uint16_t l = mks[i].calc.l;
if(mks[l].calc.magic != MAGIC)
lv = mks[l].val;
else if((mks[l].calc.op & 0x80) == 0) {
array_int_push_back(stack, l);
continue;
}
int64_t rv = 0;
uint16_t r = mks[i].calc.r;
if(mks[r].calc.magic != MAGIC)
rv = mks[r].val;
else if((mks[r].calc.op & 0x80) == 0) {
array_int_push_back(stack, r);
continue;
}
array_int_pop_back(NULL, stack);
if((((mks[l].calc.op | mks[r].calc.op) & 0x80) != 0 || l == 1 || r == 1) &&
!resolve_humn) {
mks[i].calc.op |= 0x80;
continue;
}
switch(mks[i].calc.op) {
case '+':
mks[i].val = lv + rv;
break;
case '-':
mks[i].val = lv - rv;
break;
case '*':
mks[i].val = lv * rv;
break;
case '/':
mks[i].val = lv / rv;
break;
}
}
array_int_clear(stack);
return mks[id].val;
}
int64_t inverse(Monkey* mks, uint16_t id, int64_t goal) {
uint16_t l, r;
bool swap;
while(true) {
l = mks[id].calc.l;
r = mks[id].calc.r;
mks[id].calc.op &= ~0x80;
swap = false;
if(mks[r].calc.magic == MAGIC) {
swap = true;
M_SWAP(uint16_t, l, r);
}
switch(mks[id].calc.op) {
case '+':
goal -= mks[r].val;
break;
case '-':
if(swap)
goal = mks[r].val - goal;
else
goal += mks[r].val;
break;
case '*':
goal /= mks[r].val;
break;
case '/':
if(swap) {
goal = mks[r].val / goal;
} else
goal *= mks[r].val;
}
if(l == 1) return goal;
id = l;
}
}