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 2019/11

+168
+166
2019/day11.py
··· 1 + import time 2 + import fileinput 3 + from collections import defaultdict 4 + 5 + 6 + GLOBAL_INPUTS = [0] 7 + 8 + # True = read, False = write 9 + INSTRUCTIONS = { 10 + 1: ('ADD', (True, True, False)), 11 + 2: ('MUL', (True, True, False)), 12 + 3: ('INP', (False, None, None)), 13 + 4: ('OUT', (True, None, None)), 14 + 5: ('JZ ', (True, True, None)), 15 + 6: ('JNZ', (True, True, None)), 16 + 7: ('LT ', (True, True, False)), 17 + 8: ('EQ ', (True, True, False)), 18 + 9: ('REL', (True, None, None)), 19 + 99: ('HLT', (None, None, None)), 20 + } 21 + 22 + 23 + def parse_mode(tape, mode_op, a, b, c): 24 + op = mode_op % 100 25 + mode_a = (mode_op // 100) % 10 26 + mode_b = (mode_op // 1000) % 10 27 + mode_c = (mode_op // 10000) % 10 28 + 29 + return op, mode_a, mode_b, mode_c 30 + 31 + 32 + def emulate(pid, pc=0): 33 + tape = TAPE[:] 34 + outs = [] 35 + relative_base = 0 36 + 37 + def resolve_modes(op, params, modes): 38 + res = [a, b, c] 39 + 40 + for i, (is_read, p, m) in enumerate(zip(INSTRUCTIONS[op][1], params, modes)): 41 + if is_read is None: 42 + continue 43 + 44 + if is_read: 45 + if m == 0: 46 + res[i] = tape[p] 47 + elif m == 1: 48 + res[i] = p 49 + elif m == 2: 50 + res[i] = tape[relative_base + p] 51 + else: 52 + if m == 0: 53 + res[i] = p 54 + elif m == 2: 55 + res[i] = relative_base + p 56 + 57 + return res 58 + 59 + while pc < len(tape): 60 + mode_op, a, b, c = tape[pc:pc+4] 61 + op, mode_a, mode_b, mode_c = parse_mode(tape, mode_op, a, b, c) 62 + a, b, c = resolve_modes(op, (a, b, c), (mode_a, mode_b, mode_c)) 63 + 64 + # ADD a b c 65 + if op == 1: 66 + tape[c] = a + b 67 + pc += 4 68 + 69 + # MUL a b c 70 + elif op == 2: 71 + tape[c] = a * b 72 + pc += 4 73 + 74 + # INP a 75 + elif op == 3: 76 + tape[a] = GLOBAL_INPUTS[pid] 77 + pc += 2 78 + 79 + # OUT b 80 + elif op == 4: 81 + yield a 82 + pc += 2 83 + 84 + # JZ a b 85 + elif op == 5: 86 + if a != 0: 87 + pc = b 88 + else: 89 + pc += 3 90 + 91 + # JNZ a b 92 + elif op == 6: 93 + if a == 0: 94 + pc = b 95 + else: 96 + pc += 3 97 + 98 + # LT a b c 99 + elif op == 7: 100 + tape[c] = 1 if a < b else 0 101 + pc += 4 102 + 103 + # EQ a b c 104 + elif op == 8: 105 + tape[c] = 1 if a == b else 0 106 + pc += 4 107 + 108 + elif op == 9: 109 + relative_base += a 110 + pc += 2 111 + 112 + # HALT 113 + elif op == 99: 114 + raise StopIteration() 115 + 116 + 117 + def simulate_robot(start=0): 118 + grid = {} 119 + grid[0, 0] = start 120 + 121 + facing = 0 122 + x, y = 0, 0 123 + 124 + robot = emulate(0) 125 + 126 + try: 127 + while True: 128 + grid[x, y] = next(robot) 129 + if next(robot) == 0: 130 + facing = (facing - 1) % 4 131 + else: 132 + facing = (facing + 1) % 4 133 + 134 + if facing == 0: 135 + y += 1 136 + elif facing == 1: 137 + x += 1 138 + elif facing == 2: 139 + y -= 1 140 + elif facing == 3: 141 + x -= 1 142 + 143 + GLOBAL_INPUTS[0] = grid.get((x, y), 0) 144 + except StopIteration: 145 + return grid 146 + 147 + 148 + # Read input 149 + TAPE = [int(x) for x in fileinput.input()[0].split(',')] 150 + TAPE += [0] * 100000 151 + 152 + 153 + # Part 1 154 + grid = simulate_robot(0) 155 + print "Number of painted tiles:", len(grid.keys()) 156 + 157 + 158 + # Part 2 159 + grid = simulate_robot(1) 160 + 161 + print "Registration ID:\n" 162 + xs, ys = zip(*grid.keys()) 163 + for y in reversed(range(min(ys), max(ys) + 1)): 164 + print ''.join( 165 + '#' if grid.get((x, y)) else ' ' 166 + for x in range(min(xs), max(xs) + 1))
+1
2019/inputs/11.txt
··· 1 + 3,8,1005,8,319,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,28,2,1008,7,10,2,4,17,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,59,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,81,1006,0,24,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,105,2,6,13,10,1006,0,5,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,134,2,1007,0,10,2,1102,20,10,2,1106,4,10,1,3,1,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,172,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,194,1,103,7,10,1006,0,3,1,4,0,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,228,2,109,0,10,1,101,17,10,1006,0,79,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,260,2,1008,16,10,1,1105,20,10,1,3,17,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,295,1,1002,16,10,101,1,9,9,1007,9,1081,10,1005,10,15,99,109,641,104,0,104,1,21101,387365733012,0,1,21102,1,336,0,1105,1,440,21102,937263735552,1,1,21101,0,347,0,1106,0,440,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,3451034715,1,1,21101,0,394,0,1105,1,440,21102,3224595675,1,1,21101,0,405,0,1106,0,440,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,838337454440,1,21102,428,1,0,1105,1,440,21101,0,825460798308,1,21101,439,0,0,1105,1,440,99,109,2,22101,0,-1,1,21102,1,40,2,21101,0,471,3,21101,461,0,0,1106,0,504,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,466,467,482,4,0,1001,466,1,466,108,4,466,10,1006,10,498,1102,1,0,466,109,-2,2105,1,0,0,109,4,2101,0,-1,503,1207,-3,0,10,1006,10,521,21101,0,0,-3,21202,-3,1,1,22102,1,-2,2,21101,1,0,3,21102,540,1,0,1105,1,545,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,568,2207,-4,-2,10,1006,10,568,22102,1,-4,-4,1106,0,636,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,587,1,0,1105,1,545,21201,1,0,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,606,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,628,22102,1,-1,1,21102,1,628,0,105,1,503,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0
+1
2019/outputs/11.txt
··· 1 + 2293