-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.py
81 lines (65 loc) · 1.75 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
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
from collections import defaultdict
from adventlib import *
DAY = 23
def runner(code):
sent = 0
ip = 0
regs = defaultdict(lambda: 0)
def get_val(a):
if a.isalpha():
return regs[a]
return int(a)
cl = len(code)
mulc = 0
while 0 <= ip < cl:
insn = code[ip]
op = insn[0]
if op == "set":
regs[insn[1]] = get_val(insn[2])
elif op == "add":
regs[insn[1]] += get_val(insn[2])
elif op == "sub":
regs[insn[1]] -= get_val(insn[2])
elif op == "mul":
mulc += 1
regs[insn[1]] *= get_val(insn[2])
elif op == "mod":
regs[insn[1]] %= get_val(insn[2])
elif op == "jgz":
if get_val(insn[1]) > 0:
ip += get_val(insn[2])
continue
elif op == "jnz":
if get_val(insn[1]) != 0:
ip += get_val(insn[2])
continue
ip += 1
return mulc
def primes_upto(limit):
"""
Taken from https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Python
"""
is_prime = [False] * 2 + [True] * (limit - 1)
for n in range(int(limit**0.5 + 1.5)):
if is_prime[n]:
for i in range(n*n, limit+1, n):
is_prime[i] = False
return [i for i, prime in enumerate(is_prime) if prime]
def main():
inp = store_input(DAY)
if inp is None:
return
code = split_table(inp, None)
print(runner(code))
b = 81 * 100 + 100000
c = b + 17000
p = primes_upto(int(c ** 0.5 + 1.5))
h = 0
for i in range(b, c + 17, 17):
for e in p:
if i % e == 0:
h += 1
break
print(h)
if __name__ == '__main__':
main()