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 2018/06

+111
+59
2018/day06.py
··· 1 + import fileinput 2 + from collections import Counter 3 + 4 + from utils import Point, DIRS_4, parse_nums 5 + 6 + 7 + COORDS = [] 8 + 9 + for line in fileinput.input(): 10 + x, y = parse_nums(line) 11 + COORDS.append((x, y)) 12 + 13 + X, Y = zip(*COORDS) 14 + 15 + ASSIGNS = {} 16 + REGIONS = Counter() 17 + SAFETY = Counter() 18 + 19 + for 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 + 40 + for 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 + 59 + print "Size of Part 2 region:", sum(n < 10000 for n in SAFETY.values())
+50
2018/inputs/06.txt
··· 1 + 108, 324 2 + 46, 91 3 + 356, 216 4 + 209, 169 5 + 170, 331 6 + 332, 215 7 + 217, 104 8 + 75, 153 9 + 110, 207 10 + 185, 102 11 + 61, 273 12 + 233, 301 13 + 278, 151 14 + 333, 349 15 + 236, 249 16 + 93, 155 17 + 186, 321 18 + 203, 138 19 + 103, 292 20 + 47, 178 21 + 178, 212 22 + 253, 174 23 + 348, 272 24 + 83, 65 25 + 264, 227 26 + 239, 52 27 + 243, 61 28 + 290, 325 29 + 135, 96 30 + 165, 339 31 + 236, 132 32 + 84, 185 33 + 94, 248 34 + 164, 82 35 + 325, 202 36 + 345, 323 37 + 45, 42 38 + 292, 214 39 + 349, 148 40 + 80, 180 41 + 314, 335 42 + 210, 264 43 + 302, 108 44 + 235, 273 45 + 253, 170 46 + 150, 303 47 + 249, 279 48 + 255, 159 49 + 273, 356 50 + 275, 244
+2
2018/outputs/06.txt
··· 1 + 4166 2 + 42250