Skip to content

Commit

Permalink
16-1
Browse files Browse the repository at this point in the history
  • Loading branch information
koosvary committed Dec 16, 2024
1 parent d1403d0 commit 00df215
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 2024/16/16-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import networkx as nx

directions = (1, -1, 1j, -1j) # use imaginary numbers for east<->west

with open('2024/16/input.txt') as f:
g = nx.DiGraph()
for i, line in enumerate(f):
line = line.strip()
for j, char in enumerate(line):
if char == '#':
continue
node = i + 1j * j

if char == 'E':
e = node
if char == 'S':
s = (node, 1j) # reindeer starts pointing west

for nodeDir in directions:
g.add_node((node, nodeDir))

for node, nodeDir in g.nodes:
if (node + nodeDir, nodeDir) in g.nodes:
# orthogonally connected in the same direction as previous node
# eg, connect node A's north direction with node B's north
# no need to connect A's north to B's south - it'd be redundant
g.add_edge((node, nodeDir), (node + nodeDir, nodeDir), weight=1)
for rotation in -1j, 1j:
# connect 90 degree corners
# using i, since i**2 = 1 so it'll respect a turn
g.add_edge((node, nodeDir), (node, nodeDir * rotation), weight=1000)

# need to make a new endpoint otherwise the exit will not be found
for direction in directions:
# we appear to have an off-by-one problem, remove the last move to the exit
g.add_edge((e, direction), 'end', weight=0)

shortestPath = nx.shortest_path(g, s, 'end', weight='weight')
weight = nx.path_weight(g, shortestPath, weight='weight')

print(shortestPath)
print(weight)
15 changes: 15 additions & 0 deletions 2024/16/testinput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
17 changes: 17 additions & 0 deletions 2024/16/testinput2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################

0 comments on commit 00df215

Please sign in to comment.