-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.c3
186 lines (175 loc) · 3.86 KB
/
day21.c3
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
module day21;
import std::io;
import std::math;
import std::collections::map;
import std::collections::list;
def MonkeyList = List(<Monkey>);
def NameMap = HashMap(<String, MonkeyId>);
enum Action
{
VALUE,
MULT,
DIV,
PLUS,
MINUS,
}
distinct MonkeyId = int;
struct Monkey
{
MonkeyId id;
String name;
Action action;
bool is_human;
bool had_human;
union
{
Monkey*[2] other_monkey;
String[2] other_monkey_unresolved;
long value;
}
}
fn void load_monkeys(MonkeyList* list, MonkeyId* root_index, MonkeyId *humn_index)
{
File f = file::open("monkeys.txt", "rb")!!;
defer (void)f.close();
NameMap map;
map.new_init();
defer map.free();
MonkeyId index = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
String[] parts = line.tsplit(": ");
String name = parts[0].copy();
if (name == "root")
{
*root_index = index;
}
Monkey m = { .name = name, .id = index };
if (name == "humn")
{
*humn_index = index;
m.is_human = true;
m.had_human = true;
}
if (parts[1][0] < 'a')
{
m.value = parts[1].to_long()!!;
m.action = Action.VALUE;
}
else
{
parts = parts[1].tsplit(" ");
m.other_monkey_unresolved[0] = parts[0].copy();
m.other_monkey_unresolved[1] = parts[2].copy();
switch (parts[1][0])
{
case '*': m.action = Action.MULT;
case '/': m.action = Action.DIV;
case '-': m.action = Action.MINUS;
case '+': m.action = Action.PLUS;
default: unreachable();
}
}
map.set(name, index++);
list.push(m);
};
}
foreach (&monkey : list)
{
if (monkey.action == Action.VALUE) continue;
MonkeyId monkey1 = map.get(monkey.other_monkey_unresolved[0])!!;
MonkeyId monkey2 = map.get(monkey.other_monkey_unresolved[1])!!;
monkey.other_monkey = { list.get_ref((int)monkey1), list.get_ref((int)monkey2) };
}
}
fn long Monkey.getValue(Monkey* m)
{
if (m.action == Action.VALUE) return m.value;
long m1 = m.other_monkey[0].getValue();
long m2 = m.other_monkey[1].getValue();
switch (m.action)
{
case MULT: return m1 * m2;
case DIV: return m1 % m2 ? 0x1FFF_FFFF_FFFF_FFFFi64 : m1 / m2;
case PLUS: return m1 + m2;
case MINUS: return m1 - m2;
default: unreachable();
}
}
fn void part1(Monkey root)
{
long value = root.getValue();
io::printfn("Root yells: %d", value);
}
fn bool Monkey.has_human(Monkey* m)
{
if (m.is_human) return true;
if (m.action == Action.VALUE) return false;
bool m1 = m.other_monkey[0].has_human();
bool m2 = m.other_monkey[1].has_human();
if (m1 || m2)
{
m.had_human = true;
assert(m1 != m2);
return true;
}
return false;
}
fn bool Monkey.discover_human(Monkey* m, long value)
{
if (m.is_human)
{
io::printfn("Human yells: %d", value);
return true;
}
if (!m.had_human) return false;
Monkey* m1 = m.other_monkey[0];
Monkey* m2 = m.other_monkey[1];
switch (m.action)
{
case VALUE:
return false;
case PLUS:
if (m2.had_human) @swap(m1, m2);
return m1.discover_human(value - m2.getValue());
case MINUS:
if (m1.had_human)
{
return m1.discover_human(value + m2.getValue());
}
return m2.discover_human(m1.getValue() - value);
case DIV:
if (m1.had_human)
{
return m1.discover_human(value * m2.getValue());
}
return m2.discover_human(m1.getValue() / value);
case MULT:
if (m2.had_human) @swap(m1, m2);
return m1.discover_human(value / m2.getValue());
default:
unreachable();
}
}
fn void part2(Monkey* root, Monkey* humn)
{
Monkey* m1 = root.other_monkey[0];
Monkey* m2 = root.other_monkey[1];
Monkey* h = m1.has_human() ? m1 : m2;
Monkey* o = h == m1 ? m2 : m1;
bool found = h.discover_human(o.getValue());
assert(found);
}
fn void main()
{
MonkeyList list;
list.new_init();
MonkeyId root_index;
MonkeyId humn_index;
load_monkeys(&list, &root_index, &humn_index);
part1(list.get((int)root_index));
part2(list.get_ref((int)root_index), list.get_ref((int)humn_index));
}