-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
############### | ||
#.......#....E# | ||
#.#.###.#.###.# | ||
#.....#.#...#.# | ||
#.###.#####.#.# | ||
#.#.#.......#.# | ||
#.#.#####.###.# | ||
#...........#.# | ||
###.#.#####.#.# | ||
#...#.....#.#.# | ||
#.#.#.###.#.#.# | ||
#.....#...#.#.# | ||
#.###.#.#.#.#.# | ||
#S..#.....#...# | ||
############### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
################# | ||
#...#...#...#..E# | ||
#.#.#.#.#.#.#.#.# | ||
#.#.#.#...#...#.# | ||
#.#.#.#.###.#.#.# | ||
#...#.#.#.....#.# | ||
#.#.#.#.#.#####.# | ||
#.#...#.#.#.....# | ||
#.#.#####.#.###.# | ||
#.#.#.......#...# | ||
#.#.###.#####.### | ||
#.#.#...#.....#.# | ||
#.#.#.#####.###.# | ||
#.#.#.........#.# | ||
#.#.#.#########.# | ||
#S#.............# | ||
################# |