From 59962502d5df77c5ccc55be80223b32d8af04b72 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 30 Nov 2021 01:01:08 +0100 Subject: [PATCH] day10, FINALLY! --- day02.py | 2 +- day10.py | 55 +++++++++++++++++++++++++++++++++++++++++ inputs/input10 | 34 +++++++++++++++++++++++++ inputs/test_input10_1_0 | 5 ++++ inputs/test_input10_1_1 | 10 ++++++++ inputs/test_input10_1_2 | 10 ++++++++ inputs/test_input10_1_3 | 10 ++++++++ inputs/test_input10_1_4 | 20 +++++++++++++++ inputs/test_input10_2_0 | 20 +++++++++++++++ 9 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 day10.py create mode 100644 inputs/input10 create mode 100644 inputs/test_input10_1_0 create mode 100644 inputs/test_input10_1_1 create mode 100644 inputs/test_input10_1_2 create mode 100644 inputs/test_input10_1_3 create mode 100644 inputs/test_input10_1_4 create mode 100644 inputs/test_input10_2_0 diff --git a/day02.py b/day02.py index f6bc251..82080f4 100644 --- a/day02.py +++ b/day02.py @@ -12,7 +12,7 @@ class Day(AOCDay): memory = self.getInputAsArraySplit(',', int) memory[1] = 12 memory[2] = 2 - comp = IntCode(memory, 100) + comp = IntCode(memory) comp.run() return comp.memory[0] diff --git a/day10.py b/day10.py new file mode 100644 index 0000000..6a04362 --- /dev/null +++ b/day10.py @@ -0,0 +1,55 @@ +from aoc import AOCDay +from coordinate import Coordinate +from grid import Grid +from typing import Any, List + + +def radarPing(grid: Grid, origin: Coordinate) -> List[Coordinate]: + angleList = {} + for asteroid in grid.getActiveCells(): + if asteroid == origin: + continue + + angle = origin.getAngleTo(asteroid, normalized=True) + if angle in angleList and origin.getDistanceTo(asteroid) > origin.getDistanceTo(angleList[angle]): + continue + + angleList[angle] = asteroid + + return [angleList[k] for k in sorted(angleList.keys())] + + +def getMaxAsteroidsSeen(grid: Grid) -> (Coordinate, int): + maxSeen = [] + maxCoord = None + for check in grid.getActiveCells(): + asteroids_seen = radarPing(grid, check) + if len(asteroids_seen) > len(maxSeen): + maxSeen = asteroids_seen + maxCoord = check + + return maxCoord, len(maxSeen) + + +class Day(AOCDay): + test_solutions_p1 = [8, 33, 35, 41, 210] + test_solutions_p2 = [802] + + def part1(self) -> Any: + grid = Grid() + for y, l in enumerate(self.input): + for x, c in enumerate(l): + grid.set(Coordinate(x, y), c == '#') + + return getMaxAsteroidsSeen(grid)[1] + + def part2(self) -> Any: + grid = Grid() + for y, l in enumerate(self.input): + for x, c in enumerate(l): + grid.set(Coordinate(x, y), c == '#') + + asteroids_seen = radarPing(grid, getMaxAsteroidsSeen(grid)[0]) + twohundreth = asteroids_seen[199] + + return twohundreth.x * 100 + twohundreth.y diff --git a/inputs/input10 b/inputs/input10 new file mode 100644 index 0000000..0f33b82 --- /dev/null +++ b/inputs/input10 @@ -0,0 +1,34 @@ +#....#.....#...#.#.....#.#..#....# +#..#..##...#......#.....#..###.#.# +#......#.#.#.....##....#.#.....#.. +..#.#...#.......#.##..#........... +.##..#...##......##.#.#........... +.....#.#..##...#..##.....#...#.##. +....#.##.##.#....###.#........#### +..#....#..####........##.........# +..#...#......#.#..#..#.#.##......# +.............#.#....##.......#...# +.#.#..##.#.#.#.#.......#.....#.... +.....##.###..#.....#.#..###.....## +.....#...#.#.#......#.#....##..... +##.#.....#...#....#...#..#....#.#. +..#.............###.#.##....#.#... +..##.#.........#.##.####.........# +##.#...###....#..#...###..##..#..# +.........#.#.....#........#....... +#.......#..#.#.#..##.....#.#.....# +..#....#....#.#.##......#..#.###.. +......##.##.##...#...##.#...###... +.#.....#...#........#....#.###.... +.#.#.#..#............#..........#. +..##.....#....#....##..#.#.......# +..##.....#.#...................... +.#..#...#....#.#.....#.........#.. +........#.............#.#......... +#...#.#......#.##....#...#.#.#...# +.#.....#.#.....#.....#.#.##......# +..##....#.....#.....#....#.##..#.. +#..###.#.#....#......#...#........ +..#......#..#....##...#.#.#...#..# +.#.##.#.#.....#..#..#........##... +....#...##.##.##......#..#..##.... diff --git a/inputs/test_input10_1_0 b/inputs/test_input10_1_0 new file mode 100644 index 0000000..737ae7f --- /dev/null +++ b/inputs/test_input10_1_0 @@ -0,0 +1,5 @@ +.#..# +..... +##### +....# +...## diff --git a/inputs/test_input10_1_1 b/inputs/test_input10_1_1 new file mode 100644 index 0000000..987698f --- /dev/null +++ b/inputs/test_input10_1_1 @@ -0,0 +1,10 @@ +......#.#. +#..#.#.... +..#######. +.#.#.###.. +.#..#..... +..#....#.# +#..#....#. +.##.#..### +##...#..#. +.#....#### diff --git a/inputs/test_input10_1_2 b/inputs/test_input10_1_2 new file mode 100644 index 0000000..e28e424 --- /dev/null +++ b/inputs/test_input10_1_2 @@ -0,0 +1,10 @@ +#.#...#.#. +.###....#. +.#....#... +##.#.#.#.# +....#.#.#. +.##..###.# +..#...##.. +..##....## +......#... +.####.###. diff --git a/inputs/test_input10_1_3 b/inputs/test_input10_1_3 new file mode 100644 index 0000000..af5b6e9 --- /dev/null +++ b/inputs/test_input10_1_3 @@ -0,0 +1,10 @@ +.#..#..### +####.###.# +....###.#. +..###.##.# +##.##.#.#. +....###..# +..#.#..#.# +#..#.#.### +.##...##.# +.....#.#.. diff --git a/inputs/test_input10_1_4 b/inputs/test_input10_1_4 new file mode 100644 index 0000000..33437ba --- /dev/null +++ b/inputs/test_input10_1_4 @@ -0,0 +1,20 @@ +.#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..## diff --git a/inputs/test_input10_2_0 b/inputs/test_input10_2_0 new file mode 100644 index 0000000..33437ba --- /dev/null +++ b/inputs/test_input10_2_0 @@ -0,0 +1,20 @@ +.#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..##