···11+import math
22+import fileinput
33+from collections import defaultdict
44+55+from utils import parse_line, mul, all_unique, factors, memoize, primes # NOQA
66+77+INSTRS = []
88+B = None
99+1010+# Read puzzle input
1111+for line in fileinput.input():
1212+ instr = line.strip() + ' foo'
1313+ op, x, y = instr.split()[:3]
1414+ INSTRS.append((op, x, y))
1515+1616+ if fileinput.isfirstline():
1717+ B = int(y)
1818+1919+# Part 1
2020+REGS = defaultdict(int)
2121+pc = 0
2222+muls = 0
2323+2424+while 0 <= pc < len(INSTRS):
2525+ op, x, y = INSTRS[pc]
2626+ y = REGS[y] if y.isalpha() else int(y)
2727+2828+ if op == 'set':
2929+ REGS[x] = y
3030+ elif op == 'sub':
3131+ REGS[x] -= y
3232+ elif op == 'mul':
3333+ REGS[x] *= y
3434+ muls += 1
3535+ elif op == 'mod':
3636+ REGS[x] %= y
3737+ elif op == 'jnz':
3838+ if (REGS[x] if x.isalpha() else int(x)) != 0:
3939+ pc += y
4040+ continue
4141+4242+ pc += 1
4343+4444+print "`mul` instruction invocations:", muls
4545+4646+# Part 2
4747+b = (B * 100) + 100000
4848+c = b + 17000
4949+h = 0
5050+5151+for n in range(b, c + 1, 17):
5252+ for i in range(2, int(math.sqrt(n))):
5353+ if n % i == 0:
5454+ h += 1
5555+ break
5656+5757+print "Value in register h, ie. primes in [{}, {}]: {}".format(b, c, h)
+32
2017/inputs/23.txt
···11+set b 57
22+set c b
33+jnz a 2
44+jnz 1 5
55+mul b 100
66+sub b -100000
77+set c b
88+sub c -17000
99+set f 1
1010+set d 2
1111+set e 2
1212+set g d
1313+mul g e
1414+sub g b
1515+jnz g 2
1616+set f 0
1717+sub e -1
1818+set g e
1919+sub g b
2020+jnz g -8
2121+sub d -1
2222+set g d
2323+sub g b
2424+jnz g -13
2525+jnz f 2
2626+sub h -1
2727+set g b
2828+sub g c
2929+jnz g 2
3030+jnz 1 3
3131+sub b -17
3232+jnz 1 -23