From a03ca9e28a3a0792b173b8187516cc728bf4b487 Mon Sep 17 00:00:00 2001 From: Lachlan Meyer Date: Wed, 18 Dec 2024 17:39:29 +1100 Subject: [PATCH] 18-2 --- 2024/18/18-2.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2024/18/18-2.py diff --git a/2024/18/18-2.py b/2024/18/18-2.py new file mode 100644 index 0000000..48b3021 --- /dev/null +++ b/2024/18/18-2.py @@ -0,0 +1,57 @@ +import sys +import networkx as nx +import matplotlib.pyplot as plt + +testing = False +fewestSteps = sys.maxsize + +def printGraph(): + pos = {point: point for point in map} + fig, ax = plt.subplots() + nx.draw(g, pos=pos, node_color='k', ax=ax) + nx.draw(g, pos=pos, node_size=750, ax=ax, with_labels=True) # draw nodes and edges + ax.set_xlim(-1,width) + ax.set_ylim(-1,height) + ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True) + plt.show() + + +if testing: + width = height = 7 + input = '2024/18/testinput.txt' +else: + width = height = 71 + input = '2024/18/input.txt' + +map = {} +fallingBytes = [] + +g = nx.Graph() +directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] +for i in range(height): + for j in range(width): + map[i,j] = '.' + g.add_node((i,j)) + +for node in map: + x, y = node + for direction in directions: + dirX, dirY = direction + neighbour = (x + dirX, y + dirY) + if neighbour in map: + g.add_edge(node, neighbour) + +with open(input) as f: + for i, line in enumerate(f): + line = line.strip().split(',') + corruptedNode = (int(line[0]), int(line[1])) + fallingBytes.append(corruptedNode) + +for i, corruptedNode in enumerate(fallingBytes): + try: + path = nx.shortest_path(g, (0,0), (width-1, height-1)) + except: + print(fallingBytes[i-1]) + break + + g.remove_node(corruptedNode) \ No newline at end of file