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

+113
+84
2019/day10.py
··· 1 + import math 2 + import fileinput 3 + from collections import deque, defaultdict 4 + 5 + 6 + def dist(x, y): 7 + return (((x[0] - y[0]) ** 2) + ((x[1] - y[1]) ** 2)) ** 0.5 8 + 9 + 10 + def angle(a, b): 11 + return math.atan2(a[1] - b[1], a[0] - b[0]) 12 + 13 + 14 + EPS = 1e-6 15 + 16 + # Read problem input 17 + grid = [] 18 + asteroids = set() 19 + 20 + for y, line in enumerate(fileinput.input()): 21 + for x, c in enumerate(line.strip()): 22 + if c == '#': 23 + asteroids.add((x, y)) 24 + grid.append([c == '#' for c in line.strip()]) 25 + 26 + width = len(grid[0]) 27 + height = len(grid) 28 + 29 + # Part 1 30 + detections = {} 31 + 32 + for y in range(height): 33 + for x in range(width): 34 + if not grid[y][x]: 35 + continue 36 + 37 + p = (x, y) 38 + count = 0 39 + seen_angles = set() 40 + 41 + for other in sorted(asteroids, key=lambda a: dist(p, a))[1:]: 42 + if angle(p, other) not in seen_angles: 43 + seen_angles.add(angle(p, other)) 44 + count += 1 45 + 46 + detections[x, y] = count 47 + 48 + station, num = max(detections.items(), key=lambda x: x[1]) 49 + print "Number of detections:", num 50 + 51 + 52 + # Part 2 53 + asteroids.remove(station) 54 + 55 + # Construct a circular list that is sorted by increasing angle, 56 + # and contains sorted lists of the asteroids at that angle from 57 + # the station (going from furthest to closest). We rotate in the 58 + # positive-radian direction as a hack, since the y-axis coordinates 59 + # actually increase going downwards (as opposed to upwards). 60 + by_angles = defaultdict(list) 61 + for a in asteroids: 62 + by_angles[angle(station, a)].append(a) 63 + 64 + for k, v in by_angles.items(): 65 + v.sort(key=lambda a: -dist(station, a)) 66 + 67 + queue = deque(sorted(by_angles.items())) 68 + 69 + # Rotate the queue to the starting angle 70 + start = math.pi / 2 71 + while abs(queue[0][0] - start) > EPS: 72 + queue.rotate(-1) 73 + 74 + # Iterate through the circular list, vaporizing the closest asteroid 75 + # at that angle, then moving to the next angle in the rotation. 76 + for i in range(200): 77 + at_angle = queue[0][1] 78 + vaporized = at_angle.pop() 79 + if not at_angle: 80 + queue.popleft() 81 + else: 82 + queue.rotate(-1) 83 + 84 + print "200th asteroid checksum:", vaporized[0] * 100 + vaporized[1]
+27
2019/inputs/10.txt
··· 1 + .###..#######..####..##...# 2 + ########.#.###...###.#....# 3 + ###..#...#######...#..####. 4 + .##.#.....#....##.#.#.....# 5 + ###.#######.###..##......#. 6 + #..###..###.##.#.#####....# 7 + #.##..###....#####...##.##. 8 + ####.##..#...#####.#..###.# 9 + #..#....####.####.###.#.### 10 + #..#..#....###...#####..#.. 11 + ##...####.######....#.####. 12 + ####.##...###.####..##....# 13 + #.#..#.###.#.##.####..#...# 14 + ..##..##....#.#..##..#.#..# 15 + ##.##.#..######.#..#..####. 16 + #.....#####.##........##### 17 + ###.#.#######..#.#.##..#..# 18 + ###...#..#.#..##.##..#####. 19 + .##.#..#...#####.###.##.##. 20 + ...#.#.######.#####.#.####. 21 + #..##..###...###.#.#..#.#.# 22 + .#..#.#......#.###...###..# 23 + #.##.#.#..#.#......#..#..## 24 + .##.##.##.#...##.##.##.#..# 25 + #.###.#.#...##..#####.###.# 26 + #.####.#..#.#.##.######.#.. 27 + .#.#####.##...#...#.##...#.
+2
2019/outputs/10.txt
··· 1 + 296 2 + 204