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

+80
+77
2019/day05.py
··· 1 + import fileinput 2 + 3 + # Read input 4 + TAPE = [int(x) for x in fileinput.input()[0].split(',')] 5 + TAPE += [None, None, None, None] 6 + 7 + 8 + def handle_mode(tape, mode_op, a, b, c): 9 + op = mode_op % 100 10 + mode_a = (mode_op // 100) % 10 11 + mode_b = (mode_op // 1000) % 10 12 + mode_c = (mode_op // 10000) % 10 13 + 14 + return op, mode_a, mode_b, mode_c 15 + 16 + 17 + def emulate(input): 18 + pc = 0 19 + tape = TAPE[:] 20 + outs = [] 21 + 22 + while pc < len(tape): 23 + mode_op, a, b, c = tape[pc:pc+4] 24 + op, mode_a, mode_b, mode_c = handle_mode(tape, mode_op, a, b, c) 25 + 26 + # ADD a b c 27 + if op == 1: 28 + tape[c] = (a if mode_a else tape[a]) + (b if mode_b else tape[b]) 29 + pc += 4 30 + 31 + # MUL a b c 32 + elif op == 2: 33 + tape[c] = (a if mode_a else tape[a]) * (b if mode_b else tape[b]) 34 + pc += 4 35 + 36 + # INP a 37 + elif op == 3: 38 + tape[a] = input 39 + pc += 2 40 + 41 + # OUT b 42 + elif op == 4: 43 + outs.append(a if mode_a else tape[a]) 44 + pc += 2 45 + 46 + # JZ a b 47 + elif op == 5: 48 + if (a if mode_a else tape[a]) != 0: 49 + pc = (b if mode_b else tape[b]) 50 + else: 51 + pc += 3 52 + 53 + # JNZ a b 54 + elif op == 6: 55 + if (a if mode_a else tape[a]) == 0: 56 + pc = (b if mode_b else tape[b]) 57 + else: 58 + pc += 3 59 + 60 + # LT a b c 61 + elif op == 7: 62 + tape[c] = 1 if (a if mode_a else tape[a]) < (b if mode_b else tape[b]) else 0 63 + pc += 4 64 + 65 + # EQ a b c 66 + elif op == 8: 67 + tape[c] = 1 if (a if mode_a else tape[a]) == (b if mode_b else tape[b]) else 0 68 + pc += 4 69 + 70 + # HALT 71 + elif op == 99: 72 + if not all(x == 0 for x in outs[:-1]): 73 + return "Bad output", outs 74 + return outs[-1] 75 + 76 + print "Diagnostic code for system ID 1:", emulate(1) 77 + print "Diagnostic code for system ID 5:", emulate(5)
+1
2019/inputs/05.txt
··· 1 + 3,225,1,225,6,6,1100,1,238,225,104,0,101,67,166,224,1001,224,-110,224,4,224,102,8,223,223,1001,224,4,224,1,224,223,223,2,62,66,224,101,-406,224,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1101,76,51,225,1101,51,29,225,1102,57,14,225,1102,64,48,224,1001,224,-3072,224,4,224,102,8,223,223,1001,224,1,224,1,224,223,223,1001,217,90,224,1001,224,-101,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1101,57,55,224,1001,224,-112,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1102,5,62,225,1102,49,68,225,102,40,140,224,101,-2720,224,224,4,224,1002,223,8,223,1001,224,4,224,1,223,224,223,1101,92,43,225,1101,93,21,225,1002,170,31,224,101,-651,224,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,1,136,57,224,1001,224,-138,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1102,11,85,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1107,226,226,224,102,2,223,223,1006,224,329,1001,223,1,223,1007,226,677,224,1002,223,2,223,1005,224,344,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,359,101,1,223,223,1008,226,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,108,677,226,224,1002,223,2,223,1006,224,389,101,1,223,223,7,226,226,224,102,2,223,223,1006,224,404,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,419,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,434,1001,223,1,223,1008,677,677,224,1002,223,2,223,1005,224,449,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,464,1001,223,1,223,1108,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,494,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,509,1001,223,1,223,1007,226,226,224,1002,223,2,223,1005,224,524,1001,223,1,223,7,226,677,224,1002,223,2,223,1005,224,539,1001,223,1,223,8,677,677,224,102,2,223,223,1005,224,554,1001,223,1,223,107,226,677,224,1002,223,2,223,1006,224,569,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,584,1001,223,1,223,1108,677,226,224,102,2,223,223,1006,224,599,1001,223,1,223,1008,677,226,224,102,2,223,223,1006,224,614,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,629,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,644,101,1,223,223,8,226,677,224,102,2,223,223,1005,224,659,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,674,1001,223,1,223,4,223,99,226
+2
2019/outputs/05.txt
··· 1 + 9219874 2 + 5893654