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

+58
+58
2019/day19.py
··· 1 + import sys 2 + import fileinput 3 + 4 + from utils import memoize 5 + from intcode import emulate 6 + 7 + 8 + @memoize 9 + def affected(x, y): 10 + if x < 0 or y < 0: 11 + return 0 12 + 13 + vm = emulate(TAPE, 0, [x, y], seq_input=True) 14 + return next(vm) 15 + 16 + 17 + # Read problem input 18 + TAPE = [int(x) for x in fileinput.input()[0].split(',')] 19 + TAPE += [0] * 100000 20 + 21 + 22 + part_1 = sum(affected(x, y) for y in range(50) for x in range(50)) 23 + print "Points affected by tractor beam:", part_1 24 + 25 + # Arbitrary starting point for my input that skips past the incontinuity 26 + x = 37 27 + y = 49 28 + 29 + while True: 30 + while affected(x, y): 31 + y += 1 32 + 33 + assert affected(x, y) == 0 34 + 35 + while not affected(x, y): 36 + x += 1 37 + 38 + while affected(x, y + 1): 39 + y += 1 40 + 41 + assert affected(x, y) == 1 42 + assert affected(x + 1, y) == 1 43 + assert affected(x - 1, y) == 0 44 + assert affected(x, y + 1) == 0 45 + assert affected(x, y - 1) == 1 46 + 47 + if affected(x, y - 99) == 1 and affected(x + 99, y) == 1: 48 + for yy in range(y - 99, y + 1): 49 + for xx in range(x, x + 100): 50 + if not affected(xx, yy): 51 + break 52 + else: 53 + continue 54 + 55 + break 56 + else: 57 + print "Point checksum", x * 10000 + (y - 99) 58 + sys.exit()