My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2016/12

+68
+45
2016/day12.py
··· 1 + import fileinput 2 + 3 + 4 + def simulate(program, a=0, b=0, c=0, d=0): 5 + pc = 0 6 + regs = {'a': a, 'b': b, 'c': c, 'd': d} 7 + 8 + while True: 9 + if pc >= len(program): 10 + return regs 11 + 12 + inst = program[pc].split() 13 + 14 + if len(inst) == 2: 15 + cmd, x = inst 16 + y = None 17 + else: 18 + cmd, x, y = inst 19 + 20 + if cmd == 'cpy': 21 + regs[y] = int(x) if x.isdigit() else regs[x] 22 + 23 + elif cmd == 'inc': 24 + regs[x] += 1 25 + 26 + elif cmd == 'dec': 27 + regs[x] -= 1 28 + 29 + elif cmd == 'jnz': 30 + x = int(x) if x.isdigit() else regs[x] 31 + 32 + if x == 0: 33 + pc += 1 34 + continue 35 + else: 36 + pc += int(y) 37 + continue 38 + 39 + pc += 1 40 + 41 + 42 + PROGRAM = [line.strip() for line in fileinput.input()] 43 + 44 + print "Value in register a:", simulate(PROGRAM)['a'] 45 + print "When setting c to 1:", simulate(PROGRAM, c=1)['a']
+23
2016/input12.txt
··· 1 + cpy 1 a 2 + cpy 1 b 3 + cpy 26 d 4 + jnz c 2 5 + jnz 1 5 6 + cpy 7 c 7 + inc d 8 + dec c 9 + jnz c -2 10 + cpy a c 11 + inc a 12 + dec b 13 + jnz b -2 14 + cpy c b 15 + dec d 16 + jnz d -6 17 + cpy 19 c 18 + cpy 11 d 19 + inc a 20 + dec d 21 + jnz d -2 22 + dec c 23 + jnz c -5