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.

at main 59 lines 1.3 kB view raw
1import fileinput 2from collections import Counter 3 4from utils import Point, DIRS_4, parse_nums 5 6 7COORDS = [] 8 9for line in fileinput.input(): 10 x, y = parse_nums(line) 11 COORDS.append((x, y)) 12 13X, Y = zip(*COORDS) 14 15ASSIGNS = {} 16REGIONS = Counter() 17SAFETY = Counter() 18 19for y in range(min(Y), max(Y)): 20 for x in range(min(X), max(X)): 21 p = Point(x, y) 22 total_distance = 0 23 to_point = {} 24 25 for (xx, yy) in COORDS: 26 q = Point(xx, yy) 27 dist = p.to_manhattan(q) 28 to_point[q] = dist 29 total_distance += dist 30 31 SAFETY[p] = total_distance 32 33 dists = list(sorted(to_point.items(), key=lambda x: x[1])) 34 if dists[0][1] < dists[1][1]: 35 REGIONS[dists[0][0]] += 1 36 ASSIGNS[p] = dists[0][0] 37 else: 38 ASSIGNS[p] = Point(-1, -1) 39 40for p, n in REGIONS.most_common(): 41 for d in DIRS_4: 42 q = p + d 43 while q in ASSIGNS: 44 if ASSIGNS[q] != p: 45 # Not infinite 46 break 47 48 q += d 49 else: 50 # Confirmed infinite 51 break 52 53 else: 54 # This is the largest non-infinite region 55 print "Size of Part 1 region:", n 56 break 57 58 59print "Size of Part 2 region:", sum(n < 10000 for n in SAFETY.values())