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 2024/14

+62
+62
2024/day14.py
··· 1 + import sys 2 + import fileinput 3 + from collections import Counter 4 + 5 + from utils import parse_nums, mul 6 + 7 + 8 + MAX_X = 101 9 + MAX_Y = 103 10 + TREE_CRITERIA = 15 11 + 12 + 13 + # Parse problem input. 14 + robots = [] 15 + for line in fileinput.input(): 16 + px, py, vx, vy = parse_nums(line) 17 + robots.append((px, py, vx, vy)) 18 + 19 + 20 + # Simulate robots. 21 + for iteration in range(1, 10000): 22 + new_robots = [] 23 + for px, py, vx, vy in robots: 24 + nx = (px + vx) % MAX_X 25 + ny = (py + vy) % MAX_Y 26 + new_robots.append((nx, ny, vx, vy)) 27 + 28 + robots = new_robots 29 + 30 + # The "Christmas tree" is nicely outlined by a border, so if we 31 + # find a contiguous line of robots, it's probably the tree. 32 + picture = set((px, py) for px, py, _, _ in robots) 33 + 34 + contig = 0 35 + max_contig = 0 36 + 37 + for y in range(MAX_Y): 38 + for x in range(MAX_X): 39 + if (x, y) in picture: 40 + contig += 1 41 + max_contig = max(contig, max_contig) 42 + else: 43 + contig = 0 44 + 45 + if max_contig > TREE_CRITERIA: 46 + print("Part 2:", iteration) 47 + sys.exit() 48 + 49 + 50 + if iteration == 100: 51 + quadrants = Counter() 52 + for px, py, _, _ in robots: 53 + if px < MAX_X // 2 and py < MAX_Y // 2: 54 + quadrants[1] += 1 55 + elif px < MAX_X // 2 and py > MAX_Y // 2: 56 + quadrants[2] += 1 57 + elif px > MAX_X // 2 and py < MAX_Y // 2: 58 + quadrants[3] += 1 59 + elif px > MAX_X // 2 and py > MAX_Y // 2: 60 + quadrants[4] += 1 61 + 62 + print("Part 1:", mul(quadrants.values()))