Skip to content

Commit

Permalink
AoC 2023 Day 17 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 17, 2023
1 parent 2851c5c commit 546deab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- @BEGIN:ImplementationsTable:2023@ -->
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | [](src/main/python/AoC2023_08.py) | [](src/main/python/AoC2023_09.py) | [](src/main/python/AoC2023_10.py) | [](src/main/python/AoC2023_11.py) | [](src/main/python/AoC2023_12.py) | [](src/main/python/AoC2023_13.py) | [](src/main/python/AoC2023_14.py) | [](src/main/python/AoC2023_15.py) | [](src/main/python/AoC2023_16.py) | | | | | | | | | |
| python3 | [](src/main/python/AoC2023_01.py) | [](src/main/python/AoC2023_02.py) | [](src/main/python/AoC2023_03.py) | [](src/main/python/AoC2023_04.py) | [](src/main/python/AoC2023_05.py) | [](src/main/python/AoC2023_06.py) | [](src/main/python/AoC2023_07.py) | [](src/main/python/AoC2023_08.py) | [](src/main/python/AoC2023_09.py) | [](src/main/python/AoC2023_10.py) | [](src/main/python/AoC2023_11.py) | [](src/main/python/AoC2023_12.py) | [](src/main/python/AoC2023_13.py) | [](src/main/python/AoC2023_14.py) | [](src/main/python/AoC2023_15.py) | [](src/main/python/AoC2023_16.py) | [](src/main/python/AoC2023_17.py) | | | | | | | | |
| java | [](src/main/java/AoC2023_01.java) | [](src/main/java/AoC2023_02.java) | [](src/main/java/AoC2023_03.java) | [](src/main/java/AoC2023_04.java) | [](src/main/java/AoC2023_05.java) | [](src/main/java/AoC2023_06.java) | [](src/main/java/AoC2023_07.java) | [](src/main/java/AoC2023_08.java) | [](src/main/java/AoC2023_09.java) | [](src/main/java/AoC2023_10.java) | [](src/main/java/AoC2023_11.java) | [](src/main/java/AoC2023_12.java) | [](src/main/java/AoC2023_13.java) | [](src/main/java/AoC2023_14.java) | [](src/main/java/AoC2023_15.java) | [](src/main/java/AoC2023_16.java) | | | | | | | | | |
| rust | [](src/main/rust/AoC2023_01/src/main.rs) | [](src/main/rust/AoC2023_02/src/main.rs) | [](src/main/rust/AoC2023_03/src/main.rs) | [](src/main/rust/AoC2023_04/src/main.rs) | | [](src/main/rust/AoC2023_06/src/main.rs) | [](src/main/rust/AoC2023_07/src/main.rs) | [](src/main/rust/AoC2023_08/src/main.rs) | [](src/main/rust/AoC2023_09/src/main.rs) | | | | | | [](src/main/rust/AoC2023_15/src/main.rs) | | | | | | | | | | |
<!-- @END:ImplementationsTable:2023@ -->
Expand Down
49 changes: 43 additions & 6 deletions src/main/python/AoC2023_17.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ def parse_input(self, input_data: InputData) -> Input:
return IntGrid.from_strings([line for line in input_data])

def part_1(self, grid: Input) -> Output1:
# log(grid)

def adjacent(block: Move) -> Iterator[Move]:
# log(block.cell)
for dir in Direction.capitals():
Expand Down Expand Up @@ -124,13 +122,52 @@ def get_cost(block: Move) -> int:
)
return cost

def part_2(self, input: Input) -> Output2:
return 0
def part_2(self, grid: Input) -> Output2:
def adjacent(block: Move) -> Iterator[Move]:
# log(block.cell)
for dir in Direction.capitals():
if dir == block.dir:
continue
if block.dir is not None and dir == block.dir.turn(
Turn.AROUND
):
continue
it = grid.get_cells_dir(block.cell, dir)
tot = 0
ns = []
for i in range(4):
ns.append(next(it, None))
if None in ns:
continue
for i in range(6):
ns.append(next(it, None))
for i, n in enumerate(ns):
if n is None:
break
tot += grid.get_value(n)
if i < 3:
continue
# log((n, dir, tot))
yield Move(n, dir, tot)

def get_cost(block: Move) -> int:
return block.cost

start = Cell(0, 0)
end = Cell(grid.get_max_row_index(), grid.get_max_col_index())
cost, best, path = dijkstra(
Move(start, None, 0),
lambda block: block.cell == end,
adjacent,
get_cost,
)
# log([p.cell for p in reversed(path)])
return cost

@aoc_samples(
(
("part_1", TEST, 102),
# ("part_2", TEST, "TODO"),
# ("part_1", TEST, 102),
("part_2", TEST, 94),
)
)
def samples(self) -> None:
Expand Down

0 comments on commit 546deab

Please sign in to comment.