add day 10

This commit is contained in:
Dmitry Fedotov
2024-12-29 19:08:08 +03:00
parent c5d735efde
commit af2bce9a89
5 changed files with 255 additions and 0 deletions

View File

@@ -15,3 +15,6 @@ class Input(object):
def lines_as_lists(self):
return [list(l) for l in self.lines()]
def lines_split_as_ints(self):
return [[int(n) for n in list(l)] for l in self.lines()]

69
day10/code.py Normal file
View File

@@ -0,0 +1,69 @@
import sys
sys.path.append('../aoclib')
from aoclib import Input
def find_starting_points(field) -> list[tuple[int, int]]:
points = []
for y in range(len(field)):
for x in range(len(field[0])):
if field[y][x] == 0:
points.append((x, y))
return points
def num_trails(point: tuple[int, int], curr: int, field: list[list[int]]) -> list[tuple[int, int]]:
if out_of_bounds(point, field):
return [None]
x, y = point
if field[y][x] != curr:
# impossible path
return [None]
if curr == 9 and field[y][x] == curr:
# goal
return [point]
curr += 1
opts = list()
opts.extend(num_trails((x+1, y), curr, field))
opts.extend(num_trails((x-1, y), curr, field))
opts.extend(num_trails((x, y+1), curr, field))
opts.extend(num_trails((x, y-1), curr, field))
return opts
def out_of_bounds(point: tuple[int, int], field: list[list[int]]) -> bool:
x, y = point
return y < 0 or y >= len(field) or x < 0 or x >= len(field[0])
def solve1(field: list[list[int]]) -> int:
starts = find_starting_points(field)
count = 0
for p in starts:
trails = num_trails(p, 0, field)
count += sum([1 if n else 0 for n in set(trails)])
return count
def solve2(field: list[list[int]]) -> int:
starts = find_starting_points(field)
count = 0
for p in starts:
trails = num_trails(p, 0, field)
count += sum([1 if n else 0 for n in trails])
return count
if __name__ == '__main__':
#field = Input('input_test.txt').lines_split_as_ints()
field = Input('input.txt').lines_split_as_ints()
#part 1
print('part 1:', solve1(field)) # 733
#part 2
print('part 2:', solve2(field)) # 1514

54
day10/input.txt Normal file
View File

@@ -0,0 +1,54 @@
543067650323210321032110356789890110710189878760134567
612148941212306782345091235410765227893258759651021298
701238932301456798106787549323454336794567549842330807
898547215450589867287654678892961245987654456732498910
987656306769678956398013234781870301256003301201567821
234543215878760145478920105690210982340112210341056932
109650124981232230569834124567345673451234985452347845
018744323890341021656765033438454589298545676701037796
199803210787650110765017842129823672107655677812378987
785012345654789541874326956010510563201234389983405676
174321498723498632903455467173421454102348210234514430
065430239014323721212762398982567876218959650149623521
145540128765017890101871081071018967327968743898734678
236692134984178768718981012567894458456876212798604329
987783005673269659654108923458763349663212108789015012
896654012562154546743267830309854218767103419276126787
765780123473043432890154321212903109878756578125435698
434099874984012301740125654567812893469017647030165556
323101065765101787434436763058906732154178736543256765
013298187890678696325569892143215440043289128954343894
010367296541549045016678765014300351234567037760012983
329458305032132134787867410967891267653438546891898012
458789414145010120196986323898763298344129655432743210
567076523236678743285898456777654101201098766653658901
456167654107549658374567689985403100312434985567877014
343228943298938569843210576876312238920125676656986323
651014543498127478755765435410278947437876985543235434
743223672567078369876890324320127656506987234350145632
894102981089569232125981210451256543215432122341076541
765091234676352101034674323401237894101341001456789830
014780365445443032126501012532345743103210112565898321
123689876034321145098432107645236743254101101876543210
094545763125210056788943258896107858969010332987674301
785430432106782169877652349787567967678321245696784561
876021569045893458965601654343478914565414326781093870
987109878010432167014298710234323901258905015492012901
789219894324569052123109620123217892109876012323458692
654308765543678743013234538994106543010870965410069783
789877787632189852101110145885287676521961871012178654
034565896601087601211001406776896789437652676523468743
121053795542194512389132312345445676548943989430549012
262342187634765433498545434531230107401232104321678298
876533014123892324567696526540043298340981010780890187
983414523016101019876587017832134781256672125690743296
032101654327892100345678789901295690764543434321652345
123089789236743981234589654800387789835678987787601336
854176590187656876104310343211456876544549016098543223
965765410194545675465231230898565963233432145123210112
876894321243232980370110141767653210112321039654101101
146765232100101061289879852632154100203410128789210322
036787103761267876518768763543069911012543454376325412
123498654854356987405650654985678892327632567215436701
210567569983543292314321569876301765438761078904589898
325456478754330101223433478765432654349652987615678765

8
day10/input_test.txt Normal file
View File

@@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732

121
day10/task.txt Normal file
View File

@@ -0,0 +1,121 @@
--- Day 10: Hoof It ---
You all arrive at a Lava Production Facility on a floating island in the sky. As the others begin to search the massive industrial complex, you feel a small nose boop your leg and look down to discover a reindeer wearing a hard hat.
The reindeer is holding a book titled "Lava Island Hiking Guide". However, when you open the book, you discover that most of it seems to have been scorched by lava! As you're about to ask how you can help, the reindeer brings you a blank topographic map of the surrounding area (your puzzle input) and looks up at you excitedly.
Perhaps you can help fill in the missing hiking trails?
The topographic map indicates the height at each position using a scale from 0 (lowest) to 9 (highest). For example:
0123
1234
8765
9876
Based on un-scorched scraps of the book, you determine that a good hiking trail is as long as possible and has an even, gradual, uphill slope. For all practical purposes, this means that a hiking trail is any path that starts at height 0, ends at height 9, and always increases by a height of exactly 1 at each step. Hiking trails never include diagonal steps - only up, down, left, or right (from the perspective of the map).
You look up from the map and notice that the reindeer has helpfully begun to construct a small pile of pencils, markers, rulers, compasses, stickers, and other equipment you might need to update the map with hiking trails.
A trailhead is any position that starts one or more hiking trails - here, these positions will always have height 0. Assembling more fragments of pages, you establish that a trailhead's score is the number of 9-height positions reachable from that trailhead via a hiking trail. In the above example, the single trailhead in the top left corner has a score of 1 because it can reach a single 9 (the one in the bottom left).
This trailhead has a score of 2:
...0...
...1...
...2...
6543456
7.....7
8.....8
9.....9
(The positions marked . are impassable tiles to simplify these examples; they do not appear on your actual topographic map.)
This trailhead has a score of 4 because every 9 is reachable via a hiking trail except the one immediately to the left of the trailhead:
..90..9
...1.98
...2..7
6543456
765.987
876....
987....
This topographic map contains two trailheads; the trailhead at the top has a score of 1, while the trailhead at the bottom has a score of 2:
10..9..
2...8..
3...7..
4567654
...8..3
...9..2
.....01
Here's a larger example:
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
This larger example has 9 trailheads. Considering the trailheads in reading order, they have scores of 5, 6, 5, 3, 1, 3, 5, 3, and 5. Adding these scores together, the sum of the scores of all trailheads is 36.
The reindeer gleefully carries over a protractor and adds it to the pile. What is the sum of the scores of all trailheads on your topographic map?
Your puzzle answer was 733.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
The reindeer spends a few minutes reviewing your hiking trail map before realizing something, disappearing for a few minutes, and finally returning with yet another slightly-charred piece of paper.
The paper describes a second way to measure a trailhead called its rating. A trailhead's rating is the number of distinct hiking trails which begin at that trailhead. For example:
.....0.
..4321.
..5..2.
..6543.
..7..4.
..8765.
..9....
The above map has a single trailhead; its rating is 3 because there are exactly three distinct hiking trails which begin at that position:
.....0. .....0. .....0.
..4321. .....1. .....1.
..5.... .....2. .....2.
..6.... ..6543. .....3.
..7.... ..7.... .....4.
..8.... ..8.... ..8765.
..9.... ..9.... ..9....
Here is a map containing a single trailhead with rating 13:
..90..9
...1.98
...2..7
6543456
765.987
876....
987....
This map contains a single trailhead with rating 227 (because there are 121 distinct hiking trails that lead to the 9 on the right edge and 106 that lead to the 9 on the bottom edge):
012345
123456
234567
345678
4.6789
56789.
Here's the larger example from before:
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
Considering its trailheads in reading order, they have ratings of 20, 24, 10, 4, 1, 4, 5, 8, and 5. The sum of all trailhead ratings in this larger example topographic map is 81.
You're not sure how, but the reindeer seems to have crafted some tiny flags out of toothpicks and bits of paper and is using them to mark trailheads on your topographic map. What is the sum of the ratings of all trailheads?
Your puzzle answer was 1514.
Both parts of this puzzle are complete! They provide two gold stars: **