diff --git a/2024/18/18-1.py b/2024/18/18-1.py new file mode 100644 index 0000000..a4b6cc3 --- /dev/null +++ b/2024/18/18-1.py @@ -0,0 +1,58 @@ +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' + stopAt = 12 +else: + width = height = 71 + input = '2024/18/input.txt' + stopAt = 1024 + +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): + if i >= stopAt: + break + line = line.strip().split(',') + corruptedNode = (int(line[0]), int(line[1])) + fallingBytes.append(corruptedNode) + g.remove_node(corruptedNode) + +path = nx.shortest_path(g, (0,0), (width-1, height-1)) + +print(len(path) - 1) # don't count the last step, like the reindeer maze + \ No newline at end of file diff --git a/2024/18/testinput.txt b/2024/18/testinput.txt new file mode 100644 index 0000000..0371b23 --- /dev/null +++ b/2024/18/testinput.txt @@ -0,0 +1,25 @@ +5,4 +4,2 +4,5 +3,0 +2,1 +6,3 +2,4 +1,5 +0,6 +3,3 +2,6 +5,1 +1,2 +5,5 +2,5 +6,5 +1,4 +0,4 +6,4 +1,1 +6,1 +1,0 +0,5 +1,6 +2,0 \ No newline at end of file