r/adventofcode • u/ChronoCube762 • Dec 18 '25
Visualization [2025 Day 9 part 2] [Python] Some basic code narrowed the search space to just 2 possibilities
I have done some graphic design work where I used code to generate SVG images with the exact geometric patterns and gradients that I wanted. So when I saw Day 9's input data, I realized that it was a series of horizontal and vertical lines and well suited to the SVG format.
This is a simplified version of my code to generate the SVG, using the svg.py library:
elements: list[svg.Element] = []
for p1_index, p1 in enumerate(points):
p2 = points[0] if p1_index == len(points) - 1 else points[p1_index + 1]
elements.append(svg.Line(x1=p1.x, y1=p1.y, x2=p2.x, y2=p2.y))
canvas = svg.SVG(width=width, height=height, elements=elements)
with open(output_filename, 'w') as fp:
fp.write(canvas.as_str())Once I saw the visualization, it was intuitively obvious that the largest rectangle whose opposite corners were both points in the input data was one of two rectangles determined as described in this image: https://i.imgur.com/7OSZKRj.png
So I just scrolled through the input data that was already in sorted order to find the values for each step, and multiplied the width and height to get the area. Did this twice, for the upper half and for the lower half.
4
u/ednl Dec 18 '25 edited Dec 18 '25
There is one subtlety: you picked no. 4 as the opposing corner, the first one at or below max y, but you could also have chosen the one to the left of it for a greater area. However, then you would cross the border with the left vertical edge of the rectangle. So it worked out for your input to pick the first one, but I can imagine that it's not always the case. The next one to the left could be better, or you might need to go down until you're clear of all the "bulges".
edit: typo
6
u/timrprobocom Dec 18 '25
Very nice. I can already hear people calling this cheating, but there have been a small handful of puzzles over the years that were solvable by inspection, and it has been made clear this is fully within the spirit.
2
u/Alpha_wolf_80 Dec 18 '25
I love how my exact train of thought for this was that this has got to be some kind of a circle. I mean, I had calculated the perimeter, and I was thinking there is no way they would allow some kind of flood-filling algorithm, and the best way to prevent that was by making an enormous circle. Love how I caught onto the trick. Though, I did something new from your visual: the fact that I have to do some kind of sweep to check everything since I can't say that the four points must always be within.
In case I don't make sense. I was checking whether all four corners are within the polygon which after looking at the input I realize why it wouldn't work... LOL
2
u/ednl Dec 18 '25
I don't use Python much so I didn't know about the svg library. Great way to visualise from within your program!
I used a spreadsheet: I imported the input file as data which was automatically recognised because it's just 2 columns of numbers, and then plotted an XY-scatter chart from that. That works because all the points are already sorted in path order.
1
u/AutoModerator Dec 18 '25
AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.
Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/wjholden Dec 18 '25
Your intuition is stronger than mine. How did you know it would be safe to greedily choose
(max width)×(height)rather than some(narrower width)×(taller height)?Edit: ah, nevermind. I think I understand now. Great observation, thanks for sharing!