Skip to content

Commit

Permalink
day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Dec 17, 2023
1 parent 8b8daa0 commit 16ad944
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions aoc_wim/aoc2023/q16.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
https://adventofcode.com/2023/day/16
"""
from aocd import data

from aoc_wim.zgrid import ZGrid


Expand Down
48 changes: 48 additions & 0 deletions aoc_wim/aoc2023/q17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
--- Day 17: Clumsy Crucible ---
https://adventofcode.com/2023/day/17
"""
from aocd import data

from aoc_wim.search import AStar
from aoc_wim.zgrid import ZGrid


class Q17AStar(AStar):
def __init__(self, state0, min_tail, max_tail):
self.min_tail = min_tail
self.max_tail = max_tail
super().__init__(state0, None)

def cost(self, current_state, next_state):
return grid[next_state[-1]]

def adjacent(self, state0):
tail, dz, z = state0
adj = []
if tail < self.max_tail and z + dz in grid:
adj.append((tail + 1, dz, z+dz))
if self.min_tail <= tail and z + dz * 1j in grid:
adj.append((1, dz * 1j, z + dz * 1j,))
if self.min_tail <= tail and z + dz * -1j in grid:
adj.append((1, dz * -1j, z + dz * -1j,))
if z == 0 and self.min_tail:
adj.append((1, 1j, z + 1j))
return adj

def target_reached(self, current_state, target):
tail, dz, z = current_state
return z == bottom_right and tail >= self.min_tail


grid = ZGrid(data, transform=int)
s0 = (0, 1, 0) # state vector: tail-length, direction, position
bottom_right = grid.bottom_right # target

astar = Q17AStar(s0, min_tail=0, max_tail=3)
astar.run()
print("answer_a:", astar.gscore[astar.target])

bstar = Q17AStar((0, 1, 0), min_tail=4, max_tail=10)
bstar.run()
print("answer_b:", bstar.gscore[bstar.target])
15 changes: 15 additions & 0 deletions tests/2023/17/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533
102
94
7 changes: 7 additions & 0 deletions tests/2023/17/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
111111111111
999999999991
999999999991
999999999991
999999999991
-
71

0 comments on commit 16ad944

Please sign in to comment.