From 7fc22b858a2e158c09f55d81642f708323a06fdb Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 10 Dec 2024 09:18:31 +0100 Subject: [PATCH] day10 - unpolished --- day10.py | 78 ++++++++++++++++++++++++++++++++++++++++++++ inputs/input10 | 53 ++++++++++++++++++++++++++++++ inputs/input10_test | 8 +++++ inputs/input10_test2 | 4 +++ inputs/input10_test3 | 7 ++++ inputs/input10_test4 | 7 ++++ inputs/input10_test5 | 7 ++++ inputs/input10_test7 | 7 ++++ 8 files changed, 171 insertions(+) create mode 100644 day10.py create mode 100644 inputs/input10 create mode 100644 inputs/input10_test create mode 100644 inputs/input10_test2 create mode 100644 inputs/input10_test3 create mode 100644 inputs/input10_test4 create mode 100644 inputs/input10_test5 create mode 100644 inputs/input10_test7 diff --git a/day10.py b/day10.py new file mode 100644 index 0000000..38f400f --- /dev/null +++ b/day10.py @@ -0,0 +1,78 @@ +from collections import deque + +from tools.aoc import AOCDay +from tools.coordinate import Coordinate +from tools.grid import Grid +from typing import Any + + +def find_nine(grid: Grid, start: Coordinate) -> int: + q = deque([start]) + seen = set() + nines = set() + while q: + pos = q.popleft() + cur_val = grid.get(pos) + if cur_val == 9: + nines.add(pos) + continue + if pos in seen: + continue + seen.add(pos) + + for next_pos in grid.getNeighboursOf(pos, includeDiagonal=False): + if grid.get(next_pos) == cur_val + 1: + q.append(next_pos) + + return len(nines) + + +def find_nine_ways(grid: Grid, start: Coordinate, next_val: int = 1) -> int: + if next_val == 9: + return sum(1 for x in grid.getNeighboursOf(start, includeDiagonal=False) if grid.get(x) == 9) + else: + return sum( + find_nine_ways(grid, x, next_val + 1) + for x in grid.getNeighboursOf(start, includeDiagonal=False) + if grid.get(x) == next_val + ) + + +class Day(AOCDay): + inputs = [ + [ + (1, "input10_test2"), + (2, "input10_test3"), + (4, "input10_test4"), + (3, "input10_test5"), + (36, "input10_test"), + (644, "input10"), + ], + [ + (3, "input10_test7"), + (81, "input10_test"), + (1366, "input10"), + ], + ] + + def parse_input(self) -> Grid: + return Grid.from_data(self.getInput(), default=0, translate={"[0-9]": int}) + + def part1(self) -> Any: + grid = self.parse_input() + trail_heads = [ + Coordinate(x, y) for y in grid.rangeY() for x in grid.rangeX() if grid.get(Coordinate(x, y)) == 0 + ] + return sum(find_nine(grid, trail) for trail in trail_heads) + + def part2(self) -> Any: + grid = self.parse_input() + trail_heads = [ + Coordinate(x, y) for y in grid.rangeY() for x in grid.rangeX() if grid.get(Coordinate(x, y)) == 0 + ] + return sum(find_nine_ways(grid, trail) for trail in trail_heads) + + +if __name__ == "__main__": + day = Day(2024, 10) + day.run(verbose=True) diff --git a/inputs/input10 b/inputs/input10 new file mode 100644 index 0000000..67ac89d --- /dev/null +++ b/inputs/input10 @@ -0,0 +1,53 @@ +78760345801298896321001230110976301870134341034545687 +69671236989367765410678345221885432963233452127632796 +10589987670456001323569896336797010154324569498101845 +23478678561032132124428987445788923245013478545613932 +12034569432983045034310176596657654356932103456754301 +06123765301874126965223456780345543267843212769861212 +67610854322965437876104320691275676189732301890770023 +98545921013456946766782111234987089078721450321981167 +23434530145697854325693003435672128569670965492843298 +10127647654787765014234512345965432154589876782194587 +01018758943439890143109671236878941003432106543087656 +10129667872108743232198780987567650012343267890989943 +78734531231065654651015697898232109871054154901077830 +69632340345676544543234506701145696780567063212566321 +12541256543989035672103215012098785991278343103455410 +01230167612108120780043454123210034876369752101769511 +43456898909817431891012763204987121765459861019878700 +32197899768746532982367874215672100894312772923467011 +44089432897655673675498985365523456783203689876554322 +43476521784554984566780189474310327894104501298458943 +32562340603443295432110276589210016765067898347457654 +41001456512782106798023345341232108778754106756346655 +58989867425691065897654301250343419689603245891258743 +67670198304781478654761298765430589576512231230969512 +50501078213210569763890181056521677433443100945872402 +41432189362387659812083212347893478922109811876721321 +32343276651096543202144307438982560013078320125630430 +21054565789823430143435698729861011224565410432145561 +78769834658712894354998705610154398348901565589006776 +69876765541006765267888214567863287657652651671217889 +56945454532214890122379823098978101098343780980368923 +47834503654323012001456702112109780123210891276456914 +32322112567410163100014511003498898194541201345567805 +01012012398789654011723621014586767087687632436679876 +01201034498218765327898734765675432101598543567987015 +32789125567309014456783349898987013216967845678872126 +45694876323418123242124456787016524567856934569543034 +34543945410567032103023456689145434787643021078676545 +67432876509878749854510965670236543694532122189089496 +78761789034569858987627874321987231243231821672102387 +89450321123988567546596765345698100340100910565201256 +44321450210676875654587034276765657654327823434389855 +32340565012363966703456123189894398789016434325670765 +01051876765454567812890012007654289430145045410581678 +98762998898323234906721063218723176521032156921892569 +45101237012210105415430874349812067898149867865433454 +36788946543101076010987965456701098778928766456521043 +29897654218760187623875478309898105667810610347876542 +10898743009451296874566329214321234354320521298985031 +01734542112321345987017812105670985012131494567874120 +45629610321030996506323901078987876543012383456923454 +30018723478945887215489765212985432692105672321012765 +21167634569876774345677894303876501783234561030109876 diff --git a/inputs/input10_test b/inputs/input10_test new file mode 100644 index 0000000..7bb1248 --- /dev/null +++ b/inputs/input10_test @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 \ No newline at end of file diff --git a/inputs/input10_test2 b/inputs/input10_test2 new file mode 100644 index 0000000..dd80454 --- /dev/null +++ b/inputs/input10_test2 @@ -0,0 +1,4 @@ +0123 +1234 +8765 +9876 \ No newline at end of file diff --git a/inputs/input10_test3 b/inputs/input10_test3 new file mode 100644 index 0000000..376ee8e --- /dev/null +++ b/inputs/input10_test3 @@ -0,0 +1,7 @@ +...0... +...1... +...2... +6543456 +7.....7 +8.....8 +9.....9 \ No newline at end of file diff --git a/inputs/input10_test4 b/inputs/input10_test4 new file mode 100644 index 0000000..16c5076 --- /dev/null +++ b/inputs/input10_test4 @@ -0,0 +1,7 @@ +..90..9 +...1.98 +...2..7 +6543456 +765.987 +876.... +987.... \ No newline at end of file diff --git a/inputs/input10_test5 b/inputs/input10_test5 new file mode 100644 index 0000000..2e42d65 --- /dev/null +++ b/inputs/input10_test5 @@ -0,0 +1,7 @@ +10..9.. +2...8.. +3...7.. +4567654 +...8..3 +...9..2 +.....01 \ No newline at end of file diff --git a/inputs/input10_test7 b/inputs/input10_test7 new file mode 100644 index 0000000..98ab931 --- /dev/null +++ b/inputs/input10_test7 @@ -0,0 +1,7 @@ +.....0. +..4321. +..5..2. +..6543. +..7..4. +..8765. +..9.... \ No newline at end of file