-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday23p1.py
More file actions
executable file
·67 lines (54 loc) · 1.48 KB
/
day23p1.py
File metadata and controls
executable file
·67 lines (54 loc) · 1.48 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
#!/usr/bin/env python
import re
with open('input/day23.txt') as fh:
data = fh.read().rstrip('\n').split('\n')
pat1 = re.compile('(hlf|tpl|inc) (a|b)')
pat2 = re.compile('(jmp) ([+-]\d+)')
pat3 = re.compile('(jie|jio) (a|b), ([+-]\d+)')
instructions = list()
for line in data:
m = re.search(pat1, line)
if m:
instructions.append((m.group(1), m.group(2)))
continue
m = re.search(pat2, line)
if m:
instructions.append((m.group(1), int(m.group(2))))
continue
m = re.search(pat3, line)
if m:
instructions.append((m.group(1), m.group(2), int(m.group(3))))
continue
raise RuntimeError('Bad line!')
registers = {'a': 0, 'b': 0}
index = 0
while True:
try:
instruction = instructions[index]
except IndexError:
break
oper = instruction[0]
if oper == 'hlf':
registers[instruction[1]] /= 2
index += 1
elif oper == 'tpl':
registers[instruction[1]] *= 3
index += 1
elif oper == 'inc':
registers[instruction[1]] += 1
index += 1
elif oper == 'jmp':
index += instruction[1]
elif oper == 'jie':
if registers[instruction[1]] % 2 == 0:
index += instruction[2]
else:
index += 1
elif oper == 'jio':
if registers[instruction[1]] == 1:
index += instruction[2]
else:
index += 1
else:
raise RuntimeError('Should not get here!')
print registers['b']