Skip to content

Commit

Permalink
adding day 20 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
Fadi88 committed Dec 20, 2024
1 parent 618431b commit 1116932
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
73 changes: 69 additions & 4 deletions 2024/day20/code.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# pylint: disable=C0114,C0116,C0301,C0209,W1514,C0414,E0001


from itertools import product
from typing import Any
import os
from time import perf_counter_ns
import heapq

input_file = os.path.join(os.path.dirname(__file__), "input.txt")
# input_file = os.path.join(os.path.dirname(__file__), "test.txt")
Expand All @@ -24,16 +27,78 @@ def wrapper_method(*args: Any, **kwargs: Any) -> Any:
return wrapper_method


def dijkstra(start, free_spaces):
to_visit = []
visited = {
(start): 0,
}

heapq.heappush(to_visit, (0, start))

while to_visit:
score, (cx, cy) = heapq.heappop(to_visit)

if (cx, cy) in visited and visited[(cx, cy)] < score:
continue

for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
if (cx+dx, cy+dy) in free_spaces:
np = (cx+dx, cy+dy)
if np not in visited or visited[np] > score + 1:
visited[np] = score + 1
heapq.heappush(to_visit, (score+1, np))

return visited


def get_savings(distances, jump_size, free_space):
ret = 0
for p in free_space:
for dx, dy in product(range(-jump_size, jump_size+1), repeat=2):
if dx == dy == 0 or abs(dx) + abs(dy) > jump_size:
continue
np = (p[0]+dx, p[1]+dy)
if np in free_space:
no_walls_cost = abs(p[0]-np[0]) + abs(p[1]-np[1])
all_walls_cost = distances[np] - distances[p]

if (all_walls_cost - no_walls_cost) >= 100:
ret += 1
return ret


@profiler
def part_1():
with open(input_file) as _f:
pass
free_space = set()
end = None
with open(input_file) as f:
for y, line in enumerate(f):
for x, c in enumerate(line):
if c in ".SE":
free_space.add((x, y))
if c == "E":
end = (x, y)

from_end = dijkstra(end, free_space)

print(get_savings(from_end, 2, free_space))


@profiler
def part_2():
with open(input_file) as _f:
pass
free_space = set()
end = None
with open(input_file) as f:
for y, line in enumerate(f):
for x, c in enumerate(line):
if c in ".SE":
free_space.add((x, y))
if c == "E":
end = (x, y)

from_end = dijkstra(end, free_space)

print(get_savings(from_end, 20, free_space))


if __name__ == "__main__":
Expand Down
15 changes: 15 additions & 0 deletions 2024/day20/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
###############
#...#...#.....#
#.#.#.#.#.###.#
#S#...#.#.#...#
#######.#.#.###
#######.#.#...#
#######.#.###.#
###..E#...#...#
###.#######.###
#...###...#...#
#.#####.#.###.#
#.#...#.#.#...#
#.#.#.#.#.#.###
#...#...#...###
###############

0 comments on commit 1116932

Please sign in to comment.