-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.ndc
146 lines (125 loc) · 2.74 KB
/
16.ndc
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
fst, snd := read_file("input/16").split_once("\n\n\n");
fn addr(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] + pre[b];
return post;
}
fn addi(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] + b;
return post;
}
fn mulr(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] * pre[b];
return post;
}
fn muli(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] * b;
return post;
}
fn banr(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] & pre[b];
return post;
}
fn bani(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] & b;
return post;
}
fn borr(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] | pre[b];
return post;
}
fn bori(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a] | b;
return post;
}
fn setr(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = pre[a];
return post;
}
fn seti(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = a;
return post;
}
fn gtir(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = int(a > pre[b]);
return post;
}
fn gtri(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = int(pre[a] > b);
return post;
}
fn gtrr(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = int(pre[a] > pre[b]);
return post;
}
fn eqir(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = int(a == pre[b]);
return post;
}
fn eqri(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = int(pre[a] == b);
return post;
}
fn eqrr(pre, ins) {
op, a, b, c := ins;
post := [x for x in pre];
post[c] = int(pre[a] == pre[b]);
return post;
}
instructions := [addr, addi, mulr, muli, banr, bani, borr, bori, setr, seti, gtir, gtri, gtrr, eqir, eqri, eqrr];
mapping := %{};
count := 0;
for group in fst.split("\n\n") {
pre, ins, post := [line.nums() for line in group.lines()];
options := [ instruction for instruction in instructions, if instruction(pre, ins) == post ];
if options.len() >= 3 {
count += 1;
}
}
print("Part 1:", count);
mapped := %{};
used := %{};
for group in fst.split("\n\n") {
pre, ins, post := [line.nums() for line in group.lines()];
options := [i for i in instructions, if i(pre, ins) == post, if i not in used];
if options.len() == 1 {
mapped[ins[0]] = options[0];
used.insert(options[0]);
}
}
assert(used.len() == 16, "not every instruction was mapped");
mem := [0,0,0,0];
for line in snd.trim().lines() {
ins := line.nums();
mem = mapped[ins[0]](mem, ins);
}
print("Part 2:", mem[0]);