Day 3 needed a little bit of thinking to convert general ideas into concrete implementation, mainly to ensure precision in what you meant.
Representation
This is the same as 2025 day 4, using a Set of V2 objects to represent the trees. I return the bounds of the forest as well.
type Tree = V2 Int -- r, c
type Forest = S.Set Tree
readGrid :: String -> (Forest, Tree)
readGrid text = (S.fromList forest, V2 rMax cMax)
where rows = lines text
rMax = length rows - 1
cMax = (length $ head rows) - 1
forest = [ V2 r c
| r <- [0..rMax], c <- [0..cMax]
, rows !! r !! c == '#'
]Part 1
The basic idea is that I can construct a candidate square if I know the top-left corner of the square and its width (a four-element Set of Tree). That square exists in the forest if all four trees are in the forest, that is the candidate isSubsetOf forest. That means if I filter the list of candidates by presence in the forest, the length of the survivors is the answer.
That means I need to generate a list of candidates, given a choice of top-left and width. That's the sort of thing easily expressed as a list comprehension. The row and column of the top-left range across the possible rows and columns, and the width ranges across the space that's left.
part1 forest (V2 rMax cMax) = length $ filter (`S.isSubsetOf` forest) candidates
where candidates = [ S.fromList [ V2 r c, V2 (r + w) c
, V2 r (c + w), V2 (r + w) (c + w)
]
| r <- [0..rMax]
, c <- [0..cMax]
, let wMax = min (rMax - r) (cMax - c)
, wPart 2
In this part, squares can be rotated. The logic remains the same, but there's another factor to range over: the amount of skew of the square.
If a square has a top-left corner at V2 10 10 and a width of 3, the top-right corner can be at any of V2 13 10, V2 12 11, and V2 11 12 . I constrain the top-right corner to be strictly rightwards of the top-left, otherwise what I was calling the top-left corner is actually the top-right corner.
The maximum width of the square is a bit trickier to work out. I just plumped for the first upper bound I could think of: the lower of the height and width of the forest.
But, given the location of the top-left of a candidate square, and the width of the square, I can find the possible values the change in row and column for the top edge of the candidate. That also fixes the change in row and column for the bottom-left corner of the square, and hence all four corners.
The rest of the solution is the same.
part2 forest (V2 rMax cMax) = length $ filter (`S.isSubsetOf` forest) $ candidates
where candidates = [ S.fromList [ topLeft
, topLeft + deltaR
, topLeft + deltaD
, topLeft + deltaR + deltaD
]
| r <- [0..rMax]
, c <- [0..cMax]
, let topLeft = V2 r c
, w <- [1..wMax]
, dr <- [1..w]
, let dc = w - dr
, let deltaR = V2 dr dc
, let deltaD = V2 dc (- dr)
]
wMax = min rMax cMaxThat works, but is a bit slow. There are two main reasons.
This code picks a point to be the top-left corner of the square, picks a width, then calculates the other points of the square based on that. An easy optimisation is to just pick the top-left corner from the trees in the forest.
The other problem is that the points generated could be outside the bounds of the forest. Checking that the points are inside the forest is faster than the subset test in the filter, so I can introduce those tests as guards in the list comprehension.
The problem is that this method generates a lot of squares that have points outside the boundary of the forest.
The code isn't much different, but now looks like this.
part2 forest (V2 rMax cMax) = length $ filter (`S.isSubsetOf` forest) $ candidates
where candidates = [ S.fromList [ topLeft + deltaR
, topLeft + deltaD
, topLeft + deltaR + deltaD
]
| topLeft <- S.toList forest
, let V2 r c = topLeft
, w <- [1..wMax]
, dr <- [1..w]
, let dc = w - dr
, r + dr + dc <= rMax
, c + dc <= cMax
, c - dr >= 0
, let deltaR = V2 dr dc
, let deltaD = V2 dc (- dr)
]
wMax = min rMax cMaxThat brings the runtime down from about a minute to about 1.5 seconds. Not great, but tolerably fast.
Code
You can get the code from Codeberg.