forked from marcosfede/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd16.py
145 lines (96 loc) · 2.86 KB
/
d16.py
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
from dataclasses import dataclass
from typing import List
import re
@dataclass
class Sample:
before: List[int]
instructions: List[int]
after: List[int]
regex = r'.+: +\[(\d), (\d), (\d), (\d)\]'
samples = []
with open('./16/input1') as f:
lines = f.readlines()
for index in range(0, len(lines), 4):
m = re.match(regex, lines[index])
before = [int(x) for x in m.groups()]
instructions = [int(x) for x in lines[index+1].split(' ')]
m = re.match(regex, lines[index+2])
after = [int(x) for x in m.groups()]
samples.append(Sample(before, instructions, after))
def addr(r, a, b):
return r[a]+r[b]
def addi(r, a, b):
return r[a]+b
def mulr(r, a, b):
return r[a]*r[b]
def muli(r, a, b):
return r[a] * b
def banr(r, a, b):
return r[a] & r[b]
def bani(r, a, b):
return r[a] & b
def borr(r, a, b):
return r[a] | r[b]
def bori(r, a, b):
return r[a] | b
def setr(r, a, b):
return r[a]
def seti(r, a, b):
return a
def gtir(r, a, b):
return 1 if a > r[b] else 0
def grti(r, a, b):
return 1 if r[a] > b else 0
def gtrr(r, a, b):
return 1 if r[a] > r[b] else 0
def eqir(r, a, b):
return 1 if a == r[b] else 0
def eqri(r, a, b):
return 1 if r[a] == b else 0
def eqrr(r, a, b):
return 1 if r[a] == r[b] else 0
def apply_opcode(opcode, registers, a, b, c):
newregisters = registers[:]
newregisters[c] = opcode(registers, a, b)
return newregisters
opcodes = [addr, addi, mulr, muli, banr, bani, borr,
bori, setr, seti, gtir, grti, gtrr, eqir, eqri, eqrr]
opcode_map = {k: set(opcodes) for k in range(16)}
# p1
sample_count = 0
for sample in samples:
count = 0
opcode_code, a, b, c = sample.instructions
for opcode in opcodes:
if apply_opcode(opcode, sample.before, a, b, c) == sample.after:
count += 1
elif opcode in opcode_map[opcode_code]:
opcode_map[opcode_code].remove(opcode)
if count >= 3:
sample_count += 1
print(sample_count)
# p2
# there are codes with only one choice, remove those opcodes from the other keys
while True:
unique_ops = set()
for code, ops in opcode_map.items():
if len(ops) == 1:
unique_ops |= ops
for code, ops in opcode_map.items():
if len(ops) > 1:
ops -= unique_ops
if all(len(v) == 1 for v in opcode_map.values()):
break
# set values to the only opcode remaining
opcode_map = {k: next(iter(v)) for k, v in opcode_map.items()}
# parse second part
instructions = []
with open('./16/input2') as f:
for line in f:
instructions.append([int(x) for x in line.split(' ')])
# run the program
current_state = [0, 0, 0, 0]
for instruction in instructions:
code, a, b, c = instruction
current_state = apply_opcode(opcode_map[code], current_state, a, b, c)
print(current_state[0])