-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.py
More file actions
156 lines (110 loc) · 2.76 KB
/
21.py
File metadata and controls
156 lines (110 loc) · 2.76 KB
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
# Import data
data = open('21.in', 'r').readlines()
data = [s.strip() for s in data]
def addr(reg, a, b, c):
reg[c] = reg[a] + reg[b]
def addi(reg, a, b, c):
reg[c] = reg[a] + b
def mulr(reg, a, b, c):
reg[c] = reg[a] * reg[b]
def muli(reg, a, b, c):
reg[c] = reg[a] * b
def banr(reg, a, b, c):
reg[c] = reg[a] & reg[b]
def bani(reg, a, b, c):
reg[c] = reg[a] & b
def borr(reg, a, b, c):
reg[c] = reg[a] | reg[b]
def bori(reg, a, b, c):
reg[c] = reg[a] | b
def setr(reg, a, b, c):
reg[c] = reg[a]
def seti(reg, a, b, c):
reg[c] = a
def gtir(reg, a, b, c):
reg[c] = 1 if a > reg[b] else 0
def gtri(reg, a, b, c):
reg[c] = 1 if reg[a] > b else 0
def gtrr(reg, a, b, c):
reg[c] = 1 if reg[a] > reg[b] else 0
def eqir(reg, a, b, c):
reg[c] = 1 if a == reg[b] else 0
def eqri(reg, a, b, c):
reg[c] = 1 if reg[a] == b else 0
def eqrr(reg, a, b, c):
reg[c] = 1 if reg[a] == reg[b] else 0
opcodes = [
addr,
addi,
mulr,
muli,
banr,
bani,
borr,
bori,
setr,
seti,
gtir,
gtri,
gtrr,
eqir,
eqri,
eqrr,
]
# Part 1
def part1():
start = 10
reg = [start, 0, 0, 0, 0, 0]
ip = int(data[0].split()[1])
program = data[1:]
count = 0
while 0 <= reg[ip] < len(program):
i = reg[ip]
before = list(reg)
instr, a, b, c = program[i].split()
fn = [o for o in opcodes if o.__name__ == instr][0]
fn(reg, int(a), int(b), int(c))
reg[ip] += 1
if instr == 'eqrr':
# By understanding the code, only when start equals reg[3]
# here, then it will halt.
return reg[3]
count += 1
res = part1()
print('Part 1 solution: %d' % res)
def part2():
start = 10
reg = [start, 0, 0, 0, 0, 0]
ip = int(data[0].split()[1])
program = data[1:]
values = set()
last = 0
while 0 <= reg[ip] < len(program):
i = reg[ip]
before = list(reg)
instr, a, b, c = program[i].split()
fn = [o for o in opcodes if o.__name__ == instr][0]
if i != 8:
fn(reg, int(a), int(b), int(c))
elif i == 8:
reg[3] += (reg[2] & 255)
reg[3] &= 16777215
reg[3] *= 65899
reg[3] &= 16777215
if reg[2] < 256:
l1 = len(values)
values.add(reg[3])
l2 = len(values)
if l2 == l1:
return last
last = reg[3]
reg[4] = 0
reg[1] = 5
else:
reg[4] = int(reg[2] / 256)
reg[5] = 1
reg[2] = reg[4]
reg[1] = 7
reg[ip] += 1
res = part2()
print('Part 2 Solution: %d' % res)