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.

Refactor Intcode VM to pop input queue

+18 -23
+1 -1
2019/day11.py
··· 32 32 elif facing == 3: 33 33 x -= 1 34 34 35 - inputs[0] = grid.get((x, y), 0) 35 + inputs.append(grid.get((x, y), 0)) 36 36 except StopIteration: 37 37 return grid 38 38
+3 -3
2019/day13.py
··· 35 35 ball_x = x 36 36 37 37 if ball_x < padd_x: 38 - inputs[0] = -1 38 + inputs.append(-1) 39 39 elif ball_x > padd_x: 40 - inputs[0] = 1 40 + inputs.append(1) 41 41 else: 42 - inputs[0] = 0 42 + inputs.append(0) 43 43 44 44 except StopIteration: 45 45 print "Score after last block is broken:", score
+2 -2
2019/day15.py
··· 28 28 continue 29 29 30 30 # Attempt to move to next tile 31 - instructions[0] = d + 1 31 + instructions.append(d + 1) 32 32 resp = next(vm) 33 33 34 34 if resp == 0: ··· 41 41 robot_dfs(vm, instructions, graph, np, d) 42 42 43 43 # Can't rely on call stack alone to backtrack 44 - instructions[0] = INVERSE_DIRS[approach_d] + 1 44 + instructions.append(INVERSE_DIRS[approach_d] + 1) 45 45 next(vm) 46 46 47 47
+1 -1
2019/day17.py
··· 61 61 instructions.append(ord('\n')) 62 62 63 63 TAPE[0] = 2 64 - for c in emulate(TAPE[:], instructions): 64 + for c in emulate(TAPE[:], instructions[::-1]): 65 65 try: 66 66 print chr(c), 67 67 except Exception:
+2 -2
2019/day19.py
··· 10 10 if x < 0 or y < 0: 11 11 return 0 12 12 13 - vm = emulate(TAPE, [x, y]) 13 + vm = emulate(TAPE, [y, x]) 14 14 return next(vm) 15 15 16 16 ··· 54 54 55 55 break 56 56 else: 57 - print "Point checksum", x * 10000 + (y - 99) 57 + print "100x100 point checksum:", x * 10000 + (y - 99) 58 58 sys.exit()
+1 -1
2019/day21.py
··· 29 29 30 30 for instructions in (walking, running): 31 31 program = [ord(c) for c in instructions] 32 - vm = emulate(TAPE, program) 32 + vm = emulate(TAPE, program[::-1]) 33 33 try: 34 34 while True: 35 35 resp = next(vm)
+4 -4
2019/day23.py
··· 17 17 18 18 19 19 def start_nic(pid): 20 - vm = emulate(TAPE, PACKET_QUEUES[pid], consume_input=True) 20 + vm = emulate(TAPE, PACKET_QUEUES[pid]) 21 21 22 22 while True: 23 23 addr = next(vm) ··· 28 28 NAT[0] = x 29 29 NAT[1] = y 30 30 else: 31 - PACKET_QUEUES[addr].extend([x, y]) 31 + PACKET_QUEUES[addr].extendleft([x, y]) 32 32 33 33 34 34 def start_nat(): ··· 48 48 os._exit(1) 49 49 seen.add(y) 50 50 51 - PACKET_QUEUES[0].extend(NAT) 51 + PACKET_QUEUES[0].extendleft(NAT) 52 52 NAT[0] = None 53 53 NAT[1] = None 54 54 55 - time.sleep(1) 55 + time.sleep(0.1) 56 56 57 57 58 58 threads = []
+4 -9
2019/intcode.py
··· 37 37 return op, mode_a, mode_b, mode_c 38 38 39 39 40 - def emulate(tape, inputs, pc=0, relative_base=0, debug=False, consume_input=False): 40 + def emulate(tape, inputs, pc=0, relative_base=0, debug=False): 41 41 tape = tape[:] 42 - ipc = 0 43 42 44 43 def resolve_modes(op, params, modes): 45 44 res = [a, b, c] ··· 99 98 100 99 # INP a 101 100 elif op == 3: 102 - if consume_input: 103 - if len(inputs) == 0: 104 - tape[a] = -1 105 - else: 106 - tape[a] = inputs.popleft() 101 + if len(inputs) == 0: 102 + tape[a] = -1 107 103 else: 108 - tape[a] = inputs[ipc % len(inputs)] 109 - ipc += 1 104 + tape[a] = inputs.pop() 110 105 pc += 2 111 106 112 107 # OUT b