Added day 10

This commit is contained in:
d4rkr41n
2024-12-10 12:43:35 -06:00
parent b3e04cac7d
commit 5a2ee72d56
4 changed files with 209 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
[--- Day 10: Hoof It ---
-----------------------
You all arrive at a [Lava Production Facility](/2023/day/15) 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](https://en.wikipedia.org/wiki/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?*
]
+53
View File
@@ -0,0 +1,53 @@
45678701021256787218432154301232100012334301023456789
32569892430543298909845067210145621965421012310545869
01210743549612187610756778143296534874310123458930978
12323651258703066525643889050387546789210234567821567
01434210367012178434512918761236695694391349650131054
12544303438124569232101109678945784321487658743232343
43695496549433450143001234532034653210156961234589787
94786987834342100154519873541128763165432870234671096
85677889929854343267610565690639454076501210165692345
76012870010767256998701234788748348987432101156787654
01043961171258107887898345659654239858901101089810123
32154552987349016576987454564980108765432232123209874
43960143476987657607876523875676501678921349854112365
54871232564890548510965014934189432578900458963012453
69890121465431239423454876821054329657811467874501412
78781230656322102347623945498765018746324320189432303
45610945567212121098510130340121201235435410234534564
44327876438901010101498321233290345110346761809621875
34387654323432129812367321044789876011289898918760976
45297890012343456703455433445602345895670767823451987
56187781201278914567526932563211056734321296744589854
67096654302107803498017801074787654321234585430076763
78945109213456012332101301985698543210987676121125892
21032238376788768945432452394987650121789678032434981
32561247487699854876983345401276345430678549140123470
23470056794521003123476236982345036781565432101210565
14980129873430412001569107810034129092634307870389874
05691234562541343432018098941125678104321216921010123
06788765101632234589127657832103543219450325432167012
12109453210762103678934566543012354308765014703458983
43898344789899872100129875414983989412894327812565410
56701235692198561091223014305894876543781016945678320
12345106541085432782014323216765321789692345238769801
01416787632176306654105450125601450694547654199654432
12109898501201217653296961234702364543498703080123569
01234549654323898741787870149810676032107012678034078
67899678760015677230765487654321980121978710569985127
58908707871234982101896398923450890120879623450276434
43211216910123878982363210110961051234566542141105589
52890345034987965985476545607872340345651033032234676
01761212125675654876983432787401456978762122345897655
10354305430234503210012301294301967869887831056798587
23487416521105012342121345385210876778896990987123496
96596547012276109653010256106321236569045781234012345
87432108983489298764560187287810345652134650965421004
76545017698548345675078894396923456743221045874540218
89632123087632210982189123405410567892100038973234389
56749834128901043983458016512321098754321122980198476
43898765439456712276567087695632347665430101076567567
32109650169329803125690198787541056578920122345445898
78980543278019874034787239645670567810110233410336765
65211230165216565129876543532789436921898398561221234
34302321254305653210210123401890125432765487652310123
+26
View File
@@ -0,0 +1,26 @@
with open("../input.txt", "r") as fp:
inputs = [list(map(int, line.strip())) for line in fp]
def countPaths(hy, hx):
finished = []
paths = [(hy,hx)]
while paths:
hy, hx = paths.pop(0)
for y, x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
ny, nx = hy + y, hx + x
if 0 <= ny < len(inputs) and 0 <= nx < len(inputs[0]) and inputs[ny][nx] == inputs[hy][hx] + 1:
if inputs[ny][nx] == 9:
finished.append((ny,nx))
else:
paths.append((ny,nx))
return len(set(finished))
paths = 0
for y in range(0, len(inputs)):
for x in range(0, len(inputs[y])):
if inputs[y][x] == 0:
paths += countPaths(y, x)
print(paths)
+26
View File
@@ -0,0 +1,26 @@
with open("../input.txt", "r") as fp:
inputs = [list(map(int, line.strip())) for line in fp]
def countPaths(hy, hx):
finished = []
paths = [(hy,hx)]
while paths:
hy, hx = paths.pop(0)
for y, x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
ny, nx = hy + y, hx + x
if 0 <= ny < len(inputs) and 0 <= nx < len(inputs[0]) and inputs[ny][nx] == inputs[hy][hx] + 1:
if inputs[ny][nx] == 9:
finished.append((ny,nx))
else:
paths.append((ny,nx))
return len(finished)
paths = 0
for y in range(0, len(inputs)):
for x in range(0, len(inputs[y])):
if inputs[y][x] == 0:
paths += countPaths(y, x)
print(paths)