-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday23.py
37 lines (29 loc) · 1.02 KB
/
day23.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
from collections import defaultdict
with open('./inputs/23.txt') as f:
instructions = f.readlines()
def solve(part):
registers = defaultdict(int)
registers['a'] = part - 1
interpret = lambda val: registers[val] if val.isalpha() else int(val)
i = 0
while i < 11:
op, reg, val = instructions[i].split()
if op == 'set':
registers[reg] = interpret(val)
elif op == 'sub':
registers[reg] -= interpret(val)
elif op == 'mul':
registers[reg] *= interpret(val)
elif op == 'jnz':
if interpret(reg) != 0:
i += interpret(val)
continue
i += 1
if part == 1:
return (registers['b'] - registers['e']) * (registers['b'] - registers['d'])
nonprimes = (registers['c'] - registers['b']) // 34 + 1
for b in range(registers['b']+17, registers['c']+1, 34):
nonprimes += any(b % d == 0 for d in range(3, int(b**0.5), 2))
return nonprimes
print(solve(part=1))
print(solve(part=2))