forked from marcosfede/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd21.py
107 lines (67 loc) · 1.98 KB
/
d21.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
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]
opcodes_by_name = {op.__name__: op for op in opcodes}
program = []
with open('./21/input.txt') as f:
pregister = int(f.readline()[4])
for line in f:
op, a, b, c = line.split(' ')
program.append((op, int(a), int(b), int(c)))
# p1
pointer = 0
registers = [0, 0, 0, 0, 0, 0]
seen = set()
while 0 <= pointer < len(program):
registers[pregister] = pointer
op, a, b, c = program[pointer]
registers = apply_opcode(opcodes_by_name[op], registers, a, b, c)
pointer = registers[pregister]
pointer += 1
if pointer == 28:
if registers[5] in seen:
print(registers[5])
break
seen.add(registers[5])
print(registers[5])
print(registers[0])
###
# so basically inspecing the assembly code you realize that the only line where register 0 is used is on line 28 which sets a termination condition.
# when r[0] == r[5] the program halts
# p1 is the value of r[5] when it first reaches l28,
# p2 is the last value of r[5] before it cycles indefinitely