From dbbf97c64d61cde5d51b11e5159d0f5e1f5c9e07 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 14 Aug 2022 13:23:57 +0200 Subject: [PATCH] day6, part1 being painfully slow --- answer_cache.json | 12 +++++++ day06.py | 74 ++++++++++++++++++++++++++++++++++++++++++++ inputs/input6 | 50 ++++++++++++++++++++++++++++++ inputs/test_input6_1 | 6 ++++ 4 files changed, 142 insertions(+) create mode 100644 day06.py create mode 100644 inputs/input6 create mode 100644 inputs/test_input6_1 diff --git a/answer_cache.json b/answer_cache.json index d689699..c47b32d 100644 --- a/answer_cache.json +++ b/answer_cache.json @@ -51,5 +51,17 @@ "wrong": [], "correct": 4996 } + }, + "6": { + "1": { + "wrong": [ + 4787 + ], + "correct": 4771 + }, + "2": { + "wrong": [], + "correct": 39149 + } } } \ No newline at end of file diff --git a/day06.py b/day06.py new file mode 100644 index 0000000..768ebc2 --- /dev/null +++ b/day06.py @@ -0,0 +1,74 @@ +from tools.aoc import AOCDay +from tools.coordinate import Coordinate, DistanceAlgorithm +from tools.grid import Grid +from typing import Any, List + + +def fill_space(grid: Grid, coords: List[Coordinate], distance: int, sizecounter: List[int]) -> int: + filled = 0 + + for i, c in enumerate(coords): + for t in c.getCircle(distance, False, *grid.getBoundaries()): + if grid.get(t) == '.': + for tc in coords: + if tc != c and t.getDistanceTo(tc, DistanceAlgorithm.MANHATTAN, False) == distance: + grid.set(t, False) + break + else: + grid.set(t, i) + sizecounter[i] += 1 + filled += 1 + + return filled + + +class Day(AOCDay): + inputs = [ + [ + (17, "test_input6_1"), + (4771, "input6") + ], + [ + (39149, "input6") + ] + ] + + def get_input_coordinates(self) -> List[Coordinate]: + return [Coordinate(x, y) for x, y in self.getInputAsArraySplit(", ", return_type=int)] + + def part1(self) -> Any: + coords = self.get_input_coordinates() + grid = Grid(".") + for i, c in enumerate(coords): + grid.set(c, i) + + size_counter = [0] * len(coords) + fill_dist = 1 + while fill_space(grid, coords, fill_dist, size_counter) > 0: + fill_dist += 1 + + return max(size_counter) + 1 + + def part2(self) -> Any: + coords = self.get_input_coordinates() + grid = Grid() + for c in coords: + grid.toggle(c) + + region_size = 0 + for x in range(grid.minX, grid.maxX + 1): + for y in range(grid.minY, grid.maxY + 1): + this_dist_sum = 0 + for c in coords: + this_dist_sum += Coordinate(x, y).getDistanceTo(c, DistanceAlgorithm.MANHATTAN, False) + if this_dist_sum >= 10_000: + break + else: + region_size += 1 + + return region_size + + +if __name__ == '__main__': + day = Day(2018, 6) + day.run(verbose=True) diff --git a/inputs/input6 b/inputs/input6 new file mode 100644 index 0000000..0a27cfe --- /dev/null +++ b/inputs/input6 @@ -0,0 +1,50 @@ +260, 78 +42, 40 +87, 276 +219, 124 +166, 137 +341, 138 +82, 121 +114, 174 +218, 289 +61, 358 +328, 164 +279, 50 +218, 107 +273, 320 +192, 349 +354, 103 +214, 175 +128, 196 +237, 67 +333, 150 +98, 260 +166, 217 +92, 212 +55, 165 +205, 138 +321, 199 +285, 148 +217, 130 +357, 319 +160, 67 +63, 75 +345, 123 +316, 220 +41, 253 +240, 245 +201, 124 +336, 166 +95, 301 +55, 181 +219, 315 +209, 237 +317, 254 +314, 300 +242, 295 +295, 293 +285, 263 +330, 204 +112, 106 +348, 49 +81, 185 diff --git a/inputs/test_input6_1 b/inputs/test_input6_1 new file mode 100644 index 0000000..30cd42a --- /dev/null +++ b/inputs/test_input6_1 @@ -0,0 +1,6 @@ +1, 1 +1, 6 +8, 3 +3, 4 +5, 5 +8, 9 \ No newline at end of file