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/09

+129
+126
2019/day09.py
··· 1 + import fileinput 2 + 3 + 4 + GLOBAL_INPUTS = [0] 5 + 6 + 7 + def parse_mode(tape, mode_op, a, b, c): 8 + op = mode_op % 100 9 + mode_a = (mode_op // 100) % 10 10 + mode_b = (mode_op // 1000) % 10 11 + mode_c = (mode_op // 10000) % 10 12 + 13 + return op, mode_a, mode_b, mode_c 14 + 15 + 16 + def emulate(pid, pc=0): 17 + tape = TAPE[:] 18 + outs = [] 19 + relative_base = 0 20 + 21 + def resolve_modes(a, b, c, mode_a, mode_b, mode_c): 22 + res = [a, b, c] 23 + try: 24 + if mode_a == 0: 25 + res[0] = tape[a] 26 + elif mode_a == 1: 27 + res[0] = a 28 + elif mode_a == 2: 29 + res[0] = tape[relative_base + a] 30 + except e: 31 + pass 32 + 33 + try: 34 + if mode_b == 0: 35 + res[1] = tape[b] 36 + elif mode_b == 1: 37 + res[1] = b 38 + elif mode_b == 2: 39 + res[1] = tape[relative_base + b] 40 + except e: 41 + pass 42 + 43 + # Third param always output, instruction handles indirection 44 + try: 45 + if mode_c == 0: 46 + res[2] = c 47 + elif mode_c == 2: 48 + res[2] = relative_base + c 49 + except e: 50 + pass 51 + 52 + return res 53 + 54 + while pc < len(tape): 55 + mode_op, a, b, c = tape[pc:pc+4] 56 + op, mode_a, mode_b, mode_c = parse_mode(tape, mode_op, a, b, c) 57 + 58 + # INP uses first param to determine indirection, special case 59 + if op != 3: 60 + a, b, c = resolve_modes(a, b, c, mode_a, mode_b, mode_c) 61 + 62 + # ADD a b c 63 + if op == 1: 64 + tape[c] = a + b 65 + pc += 4 66 + 67 + # MUL a b c 68 + elif op == 2: 69 + tape[c] = a * b 70 + pc += 4 71 + 72 + # INP a 73 + elif op == 3: 74 + if mode_a == 0: 75 + tape[a] = GLOBAL_INPUTS[pid] 76 + elif mode_a == 2: 77 + tape[relative_base + a] = GLOBAL_INPUTS[pid] 78 + 79 + pc += 2 80 + 81 + # OUT b 82 + elif op == 4: 83 + outs.append(a) 84 + pc += 2 85 + 86 + # JZ a b 87 + elif op == 5: 88 + if a != 0: 89 + pc = b 90 + else: 91 + pc += 3 92 + 93 + # JNZ a b 94 + elif op == 6: 95 + if a == 0: 96 + pc = b 97 + else: 98 + pc += 3 99 + 100 + # LT a b c 101 + elif op == 7: 102 + tape[c] = 1 if a < b else 0 103 + pc += 4 104 + 105 + # EQ a b c 106 + elif op == 8: 107 + tape[c] = 1 if a == b else 0 108 + pc += 4 109 + 110 + elif op == 9: 111 + relative_base += a 112 + pc += 2 113 + 114 + # HALT 115 + elif op == 99: 116 + return outs 117 + 118 + # Read input 119 + TAPE = [int(x) for x in fileinput.input()[0].split(',')] 120 + TAPE += [0] * 100000 # Pad memory 121 + 122 + GLOBAL_INPUTS[0] = 1 123 + print "BOOST keycode:", emulate(0)[0] 124 + 125 + GLOBAL_INPUTS[0] = 2 126 + print "Coordinates of distress signal:", emulate(0)[0]
+1
2019/inputs/09.txt
··· 1 + 1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,3,0,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1102,1,31,1008,1101,682,0,1027,1101,0,844,1029,1102,29,1,1001,1102,1,22,1014,1101,0,21,1011,1102,428,1,1025,1101,0,433,1024,1101,0,38,1019,1102,1,37,1016,1102,35,1,1017,1102,39,1,1018,1102,32,1,1000,1102,23,1,1012,1102,1,329,1022,1102,26,1,1006,1102,1,24,1003,1102,28,1,1005,1102,36,1,1010,1102,34,1,1004,1101,0,1,1021,1102,326,1,1023,1101,33,0,1015,1101,20,0,1002,1101,0,25,1007,1101,0,853,1028,1102,27,1,1009,1102,1,30,1013,1101,689,0,1026,1102,1,0,1020,109,12,2108,30,-3,63,1005,63,201,1001,64,1,64,1105,1,203,4,187,1002,64,2,64,109,-9,2101,0,6,63,1008,63,29,63,1005,63,227,1001,64,1,64,1106,0,229,4,209,1002,64,2,64,109,-6,1208,5,22,63,1005,63,249,1001,64,1,64,1106,0,251,4,235,1002,64,2,64,109,13,21107,40,41,8,1005,1018,273,4,257,1001,64,1,64,1105,1,273,1002,64,2,64,109,-11,2102,1,8,63,1008,63,25,63,1005,63,299,4,279,1001,64,1,64,1105,1,299,1002,64,2,64,109,15,1205,7,317,4,305,1001,64,1,64,1105,1,317,1002,64,2,64,109,10,2105,1,-1,1105,1,335,4,323,1001,64,1,64,1002,64,2,64,109,-22,1202,1,1,63,1008,63,24,63,1005,63,357,4,341,1106,0,361,1001,64,1,64,1002,64,2,64,109,13,1206,6,373,1106,0,379,4,367,1001,64,1,64,1002,64,2,64,109,11,1206,-6,393,4,385,1105,1,397,1001,64,1,64,1002,64,2,64,109,-32,1208,10,34,63,1005,63,419,4,403,1001,64,1,64,1105,1,419,1002,64,2,64,109,30,2105,1,0,4,425,1106,0,437,1001,64,1,64,1002,64,2,64,109,-28,1207,6,21,63,1005,63,455,4,443,1106,0,459,1001,64,1,64,1002,64,2,64,109,4,2101,0,8,63,1008,63,31,63,1005,63,485,4,465,1001,64,1,64,1105,1,485,1002,64,2,64,109,5,1207,-4,28,63,1005,63,505,1001,64,1,64,1106,0,507,4,491,1002,64,2,64,109,9,21102,41,1,2,1008,1016,39,63,1005,63,531,1001,64,1,64,1106,0,533,4,513,1002,64,2,64,109,-10,1201,4,0,63,1008,63,30,63,1005,63,553,1106,0,559,4,539,1001,64,1,64,1002,64,2,64,109,19,21108,42,41,-4,1005,1019,579,1001,64,1,64,1106,0,581,4,565,1002,64,2,64,109,-26,1201,3,0,63,1008,63,32,63,1005,63,607,4,587,1001,64,1,64,1106,0,607,1002,64,2,64,109,20,1205,3,623,1001,64,1,64,1105,1,625,4,613,1002,64,2,64,109,2,21107,43,42,-1,1005,1018,645,1001,64,1,64,1106,0,647,4,631,1002,64,2,64,109,-11,2102,1,1,63,1008,63,29,63,1005,63,667,1105,1,673,4,653,1001,64,1,64,1002,64,2,64,109,27,2106,0,-8,1001,64,1,64,1105,1,691,4,679,1002,64,2,64,109,-25,2107,25,-4,63,1005,63,713,4,697,1001,64,1,64,1105,1,713,1002,64,2,64,109,-2,21108,44,44,2,1005,1010,735,4,719,1001,64,1,64,1106,0,735,1002,64,2,64,109,11,21101,45,0,-3,1008,1016,45,63,1005,63,757,4,741,1106,0,761,1001,64,1,64,1002,64,2,64,109,-15,1202,3,1,63,1008,63,22,63,1005,63,781,1105,1,787,4,767,1001,64,1,64,1002,64,2,64,109,6,21101,46,0,0,1008,1010,49,63,1005,63,811,1001,64,1,64,1105,1,813,4,793,1002,64,2,64,109,-7,2108,34,1,63,1005,63,835,4,819,1001,64,1,64,1105,1,835,1002,64,2,64,109,15,2106,0,10,4,841,1001,64,1,64,1106,0,853,1002,64,2,64,109,-25,2107,33,7,63,1005,63,873,1001,64,1,64,1106,0,875,4,859,1002,64,2,64,109,7,21102,47,1,10,1008,1010,47,63,1005,63,897,4,881,1105,1,901,1001,64,1,64,4,64,99,21102,1,27,1,21102,915,1,0,1105,1,922,21201,1,12038,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21102,942,1,0,1105,1,922,21202,1,1,-1,21201,-2,-3,1,21101,0,957,0,1106,0,922,22201,1,-1,-2,1106,0,968,22101,0,-2,-2,109,-3,2105,1,0
+2
2019/outputs/09.txt
··· 1 + 3780860499 2 + 33343