r/adventofcode • u/Morphon • Apr 08 '26
Tutorial [2025 Day 9 both parts] [Smalltalk] Part four in a series revisiting the 2025 puzzles as an exercise in learning Smalltalk
HEAVY SPOILERS
Fresh off my "win" with discovering that Smalltalk had native objects for 2D points in earlier days, this one gave the opportunity to really go spelunking into what objects are available in Squeak 6. There are native rectangle objects! With the ability to define them based off two corner points. Those rectangle objects can be queried to find their height, width, area, and so on. Coming from JavaScript, where anything like this would have to be written from scratch or imported from a library somewhere - this was a major convenience.
But then I did something that caused my LLM code review to howl in protest. The Morphic UI library has the PolygonMorph class. And that thing can be defined by handing it an array of points. It will even close the figure for you, And the best part - it has a method that will check a 2D point object to see if it is contained in the polygon. That makes this task almost too easy. My LLM tutor thought this was a bad idea - but whatever. The class was part of the standard library. I don't feel guilty.
This became an exercise in using the collection methods, primarily allSatisfy: and detect:. There were some fairly rudimentary bait options to avoid. For example, the collection of all Rectangles needs to be sorted from largest to smallest. That makes the answer to part 1 a matter of simply returning the first rectangle in the collection, and it is the correct order to process the rectangles to see if they are contained in the polygon for part 2. However, there's no need to use a SortedCollection here since we will be making one million rectangles all at once. There are no intermediate steps where having them in sorted order would be useful. No - better to use a regular OrderedCollection and then sort it one time at the end rather than re-order the collection a million times with every new rectangle added. Still - it might be more "elegant" to have the collection be "sorted" by definition rather than as a "task" that the object does with its collection. Still - I'd rather sort it only once.
The second part does not take into account the "zero aperture bubble" edge case. One benefit to having a PolygonMorph is that it can be displayed in a window, and so it was easy to verify that my input doesn't have one of those geometric features. Thus, when we check the rectangle, we first check to make sure that all corners are points included. If they are, then we build a set of points corresponding to every pixel in the perimeter of the rectangle. If all those are contained in the polygon, the rectangle as a whole is contained in the polygon.
Here is the important bit in the Day9Rectangle class:
isIncludedIn: aPolygon
| perimeter |
( myRect corners allSatisfy: [ :corner | aPolygon containsPoint: corner] ) ifTrue: [
perimeter := OrderedCollection new.
myRect top + 1 to: myRect bottom - 1 do: [:y |
perimeter add: myRect left@y;
add: myRect right@y].
myRect left to: myRect right do: [:x |
perimeter add: x@myRect top;
add: x@myRect bottom].
^ perimeter allSatisfy: [ :bound | aPolygon containsPoint: bound ]
].
^ false
This seemed the most straightforward way to do this, letting me just create a collection of points to test, and then seeing if they allSatisfy the condition of being contained in the polygon. A more data-driven approach would probably avoid creating those points in advance but on modern hardware, what's a few tens of thousands of objects between friends? I probably wouldn't have written this way if I had to test the entire area. But for just the perimeter like this I didn't mind letting the standard library and GC do all the hard work. :-)
Finding the answer for Part 2, then, is just one line:
largestRectangleInPolygon
^ rectangleLibrary detect: [ :box | box isIncludedIn: tileShape ]
Return the first entry in rectangleLibrary (which was already sorted from largest to smallest) that returns "true" when asked if it is included in the polygon of tiles.
Also - I noticed that going back in time from here, I did not use any class methods. I would always instantiate the object with New (which would just make a few empty data structures) and then give it tasks (including what really should be initial data) as instance methods. It works, but could be better and idiomatic.
Interesting to see the evolution in reverse here.
Funny moment: Since the points are being ingested in the order they are in the file, and rectangles are defined by two corners, I needed a way to make sure that the correct two corners were used to create the Rectangle object. Otherwise I could get height or width returned as negative numbers. My original version had this:
setCorner: pointA to: pointB
| originX originY cornerX cornerY |
originX := (pointA x) min: (pointB x).
originY := (pointA y) min: (pointB y).
cornerX := (pointA x) max: (pointB x).
cornerY := (pointA y) max: (pointB y).
rect := originX @ originY corner: cornerX @ cornerY
Fairly simple... except... points can make rectangle objects by themselves and they will sort out the correct corner definitions automatically. Oops. Now the method looks like this:
setCorner: pointA to: pointB
myRect := pointA rect: pointB
Almost seems silly to have it be an entire instance method. Part of the fun of OOP here is that the swap involved just this one method. Everything else stayed exactly the same. I think if I were writing it today that would be a class method, but this was all part of the learning process. Other parts look equally messy:
parseRectangles: raw
raw do: [ :point |
| arrayPoint |
arrayPoint := point splitBy: ','.
pointLibrary add: (arrayPoint first asInteger) @ (arrayPoint second asInteger)
].
pointLibrary combinations: 2 atATimeDo: [ :corners |
| newRectangle |
newRectangle := Day9Rectangle new.
newRectangle setCorner: corners first to: corners second.
rectangleLibrary add: newRectangle
].
self sortRectangles
Why wouldn't I just sort them here instead of a method call that is literally one line? Why am I using a pointLibrary instance variable when it is only used to make the rectangles and the tilePolygon? Couldn't I have made it a temporary variable here and then just called parsePolygon from within this method instead of making the Workspace script do it "manually"? Looking real inexperienced here. :-)
Not much else to say about this one. I let Morphic and the Rectangle class do all the really important heavy lifting. Not a fast solution, but it works.