Third day, second stop, a forest. Apparently, we’re stopping here because the elves needs to collect some petrichor, whatever that is. (Seriously, I have no idea what it is. Some elf told me it was some kind of smell that comes after rain, but I had never heard that name before.)
Well, while they collect everything they need, I guess I’ll just lay down and rest for a while, I think I deserve it…
I hear someone talking to me during my nap: “Excuse me mister cat, I think we might need your help”.
Of course, I wasn’t going to relax for long. Guess it’s my time to shine yet again!
“Alright, what’s the problem this time?”
- “Well, we need to collect petrichor from natual forests, because they have the best petrichor, but we don’t know where the artificial forests are.”
This looks like a job for me. Alright, time to get to work.
The elves gave me a map of all the trees in my area, and they explained to me that in order to find the trees in an artificial forest I can simply find groups of four trees that form a perfectly grid-aligned square.
First of all, I need to parse the map: it is a grid where ‘#’ represents a tree and ‘.’ the ground. I only need to get the coordinates of the trees.
Thanksfully, I already have in my utils toolkit a function to transform a grid into a coordinate-indexed list:
-- Takes a 2D list and returns an indexed 1D list.
index2D :: [[a]] -> [((Int, Int), a)]
index2D g = [((i, j), x) | (i, row) <- zip [0 ..] g, (j, x) <- zip [0 ..] row]
I can use that to get the coordinates of all the ‘#’ tiles:
parseInput :: String -> Input
parseInput = map fst . filter ((==) '#' . snd) . index2D . lines
Next thing, the heart of the problem: how do I find the squares?
Well, one solution would be to bruteforce all possible groups of four corners and see if they form a square, but that would be quite slow (there are a lot of them).
How about being a bit (just a very tiny bit) smarter about it: I can find all pairs of corners on the same row (because they need to be grid-aligned) and check if they can form the two top corners of a square. Checking that is easy, I just need to find the coordinates of the next two corners and check if they’re inside my grid.
Even better, I can check all ordered pairs on the same row, so I’m now checking one top edge twice! I made a small util function to get that:
-- Returns all the ordered pairs (with no identity pair) in a list
getOrderedPairs :: (Ord a) => [a] -> [(a, a)]
getOrderedPairs xs = [(a, b) | a <- xs, b <- xs, a < b]
Getting the coordinates of the bottom corners is quite easy: we simply need to shift each corner by a 90 degree rotation of the vector described by the two top corners:
-- From the top left and top right corners of a square, get all the corners
getSquareOtherCorners :: (Int, Int) -> (Int, Int) -> [(Int, Int)]
getSquareOtherCorners (i1, j1) (i2, j2) = [(i1 + dj, j1 - di), (i2 + dj, j2 - di)]
where
di = i2 - i1
dj = j2 - j1
Note here that my grid is indexed with rows first and columns second. That doesn’t change much in the end but remember that the first coordinate is the y coordinate here
Then as I said, we can check if these two points are in the grid to know if this forms a valid square. Because there are many points in the grid,
checking them with elem would be quite slow (it is an O(n) operation, and we might do it many times depending on how many pairs we have),
so I used a set to speed things up a little bit, as it has an O(log n) lookup.
isValidSquare :: Set (Int, Int) -> ((Int, Int), (Int, Int)) -> Bool
isValidSquare input = all (`member` input) . uncurry getSquareOtherCorners
Now we can put all that together: we start by getting all the ordered pairs of two top corners on each row, and we only keep the ones that can form a valid square:
partOne :: Input -> Output
partOne input = length (concatMap (filter (isValidSquare inputSet) . getOrderedPairs) rows)
where
inputSet = fromList input
rows = groupBy ((==) `on` fst) input
That’s quite a trivial thing to do!
At least it was trivial, until an elf decided to tell me that squares aren’t actually always grid-aligned… They can be rotated…
Well, actually it still is trivial! You see, My algorithm is pretty dumb:
The thing about only checking pairs on the same row was to make sure we were only getting grid-aligned squares. Now, we can totally discard that and check all ordered pairs on the entire grid!
Well, except for one small problem: some squares might be counted twice. Now, there are two solutions here:
I decided to go with option 2, as it was simple to observe by running the examples: it’s 2.
But why is it two? Well, we can consider the three cases of squares:
Consider this, the “right-tilted” square:
.A...
....B
.....
C....
...D.
With our algorithm:
So we counted the square twice.
Now, this, the “aligned” square:
.A..B
.....
.....
.C..D
With our algorithm:
And finally, the “left-tilted” square:
....B..
.......
A......
.......
......D
.......
..C....
With our algorithm:
All of that to say: we can just divide the result by 2 after checking if each ordered pair forms two corners of a square:
partTwo :: Input -> Output
partTwo input = (length . filter (isValidSquare inputSet) . getOrderedPairs $ input) `div` 2
where
inputSet = fromList input
Well, I feel like I bruteforced my way through this, but the elves seem happy with the result.
Hopefully I can finally start to relax now! Until tomorrow, that is :p