Browse Source

Day 10

pull/1/head
Maya Herrscher 5 months ago
parent
commit
dc4447ccfb
  1. 130
      2024/day10/challenge
  2. 50
      2024/day10/code.py
  3. 8
      2024/day10/example
  4. 50
      2024/day10/input

130
2024/day10/challenge

@ -0,0 +1,130 @@
--- 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 629.
--- 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 1242.
Both parts of this puzzle are complete! They provide two gold stars: **

50
2024/day10/code.py

@ -0,0 +1,50 @@
#!/usr/bin/env python3
import sys
def trailhead_score(tm, x, y):
height = tm[y][x]
end_positions = set([])
if height == 9: return set([(x,y)])
directions = [(0,1), (1,0), (0,-1), (-1,0)]
for d in directions:
nx, ny = x+d[0], y+d[1]
if nx < 0 or ny < 0: continue
try:
if tm[ny][nx] - height == 1:
end_positions = end_positions.union(trailhead_score(tm, nx, ny))
except IndexError: continue
return end_positions
def trailhead_ranking(tm, x, y):
height = tm[y][x]
score = 0
if height == 9: return 1
directions = [(0,1), (1,0), (0,-1), (-1,0)]
for d in directions:
nx, ny = x+d[0], y+d[1]
if nx < 0 or ny < 0: continue
try:
if tm[ny][nx] - height == 1:
score += trailhead_ranking(tm, nx, ny)
except IndexError: continue
return score
if __name__ == '__main__':
tm = [[int(l) for l in line.strip('\n')] for line in open(sys.argv[1])]
res1, res2 = 0, 0
for j, y in enumerate(tm):
for i, x in enumerate(y):
if x == 0:
res1 += len(trailhead_score(tm, i, j))
res2 += trailhead_ranking(tm, i, j)
# challenge 1
print(f"challenge 1:\n{res1}\n")
# challenge 2
print(f"challenge 2:\n{res2}")

8
2024/day10/example

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

50
2024/day10/input

@ -0,0 +1,50 @@
56760198543405456770107687012987873265430701234123
47891087612514320889298596543236982176321878985054
30932912503423411978321436760145453089490965076567
21043803498598502565450345897896334569584324105498
43256754387687643445601231056787276578675013212389
50105620896014553238782322348996189432106522301210
60234211205423469109695210567345023456987101232569
71454301312345678100556501051269010787832198543478
82365985407856743291447892340178732696949037643323
99878876556989856782332309650065341054658743252110
12389565445845698943221218761234456565789650167087
03458890330532787892100321052442787074656782108996
12766721221621056521098478934321692183245891210105
00875437876521245430167569035630013290194300121234
21980356985430130101456122128754324389980215432101
32341245234561221212340033439961015671271326990123
41239832101676678703121542345872343760362347887654
50146701098787569254035671096112654854454558943210
69655410889693452164549389987003569943003967656787
78789324989582543076678432176124578752112876545490
49670123476451001289986581065437669801198743454321
34565404560302654345677893034458954321034652345210
21670313401212701238766732123361056700125601216787
10781223304343870349676678901272341811098700305891
87690433210154901234587565078980110925643212456730
98521049873269100448996652169765223434756965569821
83430656794378234567385543258894304589807874321034
12345690185123247855434789043210113676212981012125
04396783276030110982329872178901923494345891234596
65287654896543225671012763561032876587436780989687
70132108987652334430101654432945214306525891078765
89945345456701498521012346547876305211014342569854
67876276789876567677890107236521456523210213410743
56940189678930438988743298101430567894765104323212
45434328509821321089654340122334567765894101298701
50125613410030012127763019831021998568903210345692
23498701322147897898892123742340867478912653210789
10567654213456786721089054654356789302801743105678
01678123402110995437658766789210676211010892234109
67569016567021874378941043212101245432346781103201
58454323898134565632332456701234306011015490321232
49323454234012906701454327890965412102367305410147
31012365125643812898765410378876543233458216568758
21009876034756763019034101269989100146569325679669
21898745129829654323129654352123299656478012784578
30743231065018760563238765543034788798329983693056
45656189654323011056769894672145632347012674212147
01218088745896522349850123283034701456981065100238
14309893236787631438943210190129892123672578921109
25456782101896540127654301012010181034543465433212
Loading…
Cancel
Save