From 36677c9aaa23b5f66fedc67d3a6fbd5dbc504ed5 Mon Sep 17 00:00:00 2001 From: Lachlan Meyer Date: Thu, 19 Dec 2024 01:37:20 +1100 Subject: [PATCH] 18-2 now prints PNGs Make sure to set wantToPrint to True and make the screenshots folder if you want images! --- 2024/18/18-2.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/2024/18/18-2.py b/2024/18/18-2.py index 48b3021..c8e6adb 100644 --- a/2024/18/18-2.py +++ b/2024/18/18-2.py @@ -1,19 +1,31 @@ -import sys import networkx as nx import matplotlib.pyplot as plt testing = False -fewestSteps = sys.maxsize +wantToPrint = False -def printGraph(): +def printGraph(filename, path = None): + if not wantToPrint: + return + pos = {point: point for point in map} fig, ax = plt.subplots() + inches = width if testing else 30 # max 3000x3000px + fig.set_size_inches(inches, inches) 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 + nx.draw(g, pos=pos, ax=ax, with_labels=testing, font_weight='bold') # draw nodes and edges ax.set_xlim(-1,width) ax.set_ylim(-1,height) + if path is not None: + path_edges = list(zip(path,path[1:])) + nx.draw_networkx_nodes(g, pos, nodelist=path, node_color='r') + nx.draw_networkx_edges(g, pos, edgelist=path_edges, edge_color='r') ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True) - plt.show() + + if filename is not None: + prefix = 'test_' if testing else '' + plt.savefig(f'2024/18/screenshots/{prefix}img_{str(filename+1).zfill(4)}.png', format='PNG', bbox_inches='tight') + plt.close() if testing: @@ -50,7 +62,9 @@ def printGraph(): for i, corruptedNode in enumerate(fallingBytes): try: path = nx.shortest_path(g, (0,0), (width-1, height-1)) + printGraph(i, path) except: + printGraph(i, None) print(fallingBytes[i-1]) break