Skip to content

Commit

Permalink
2024 day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Dec 26, 2024
1 parent ba916d3 commit 4d33bdf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
55 changes: 55 additions & 0 deletions 2024/10/solve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from utils import lines, Point
from collections import deque, namedtuple, defaultdict

Node = namedtuple('Node', ['position', 'origin'])

def parse(lines):
heights = {}
trailheads = []
peaks = []
for y, line in enumerate(lines):
for x, h in enumerate(map(int, line)):
p = Point(x, y)
heights[p] = h
if h == 0:
trailheads.append(p)
if h == 9:
peaks.append(p)
return heights, trailheads, peaks

def part1(heights, peaks):
reachable = defaultdict(set)
score = defaultdict(int)
fringe = deque(Node(p, p) for p in peaks)
while fringe:
pos, origin = fringe.popleft()
for n in pos.neighbors():
if n not in heights or heights[n] != heights[pos] - 1 or n in reachable[origin]:
continue
reachable[origin].add(n)
if heights[n] == 0:
score[n] += 1
else:
fringe.append(Node(n, origin))
return sum(score.values())

def part2(heights, trailheads):
rating = defaultdict(int)
fringe = deque(Node(p, p) for p in trailheads)
while fringe:
pos, origin = fringe.popleft()
for n in pos.neighbors():
if n not in heights or heights[n] != heights[pos] + 1:
continue
if heights[n] == 9:
rating[origin] += 1
else:
fringe.append(Node(n, origin))
return sum(rating.values())



if __name__ == '__main__':
heights, trailheads, peaks = parse(lines())
print(part1(heights, peaks))
print(part2(heights, trailheads))
22 changes: 22 additions & 0 deletions 2024/10/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def lines():
while True:
try:
yield input()
except:
break

class Point:
def __init__(self, x, y): self.x, self.y = x, y
def neighbor(self, D): return self + [(0,-1),(1,0),(0,1),(-1,0)][D]
def neighbors(self):
for i in range(4):
yield self.neighbor(i)
def __add__(self, other): return Point(self.x + other[0], self.y + other[1])
def __sub__(self, other): return Point(self.x - other[0], self.y - other[1])
def __neg__(self): return Point(-self.x, -self.y)
def __eq__(self, other): return self.x == other[0] and self.y == other[1]
def __getitem__(self, i): return self.x if i == 0 else self.y
def __str__(self): return "({}, {})".format(self.x, self.y)
def __repr__(self): return "({} {})".format(self.x, self.y)
def __hash__(self): return (self.x, self.y).__hash__()
#def __contains__(self, other): return other.x in range(self.x) and other.y in range(self.y)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 07 ||:star::star:||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 08 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 09 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 10 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 10 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 11 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 12 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 13 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
Expand All @@ -30,6 +30,6 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 23 |||||||:star::star:|:star:|
| 24 |||||:star::star:|:star::star:|:star::star:|:star:|
| 25 |||||:star:|:star:|:star::star:||:star:
| Total | 10 | 14 | 4 | 12 | 46 | 44 | 50 | 44 | 33
| Total | 10 | 14 | 4 | 12 | 46 | 44 | 50 | 44 | 35

Total stars: 257
Total stars: 259

0 comments on commit 4d33bdf

Please sign in to comment.