|
|
|
|
|
by cronin101
1262 days ago
|
|
Day 12 was a little hacky because of my foolhardy reluctance to ever use the obvious Dijkstra's Algorithm solution and always rolling my own organic recursive "flood fill" on the fly instead. -- ((start, end), heightMap)
type Input = ((Coordinate, Coordinate), BoundedHeightMap)
-- (length remaining, next coordinate)
type DirectionsMap = M.Map Coordinate (Int, Coordinate)
I built up a map of "where to go next and how far left" and flood-filled it starting from the end and calculating for neighbours of those that just changed value for each step until convergence, which ended up being super useful for Part 2 since I got it "for free":https://github.com/cronin101/AdventOfCode/blob/master/2022/D... Day 13 on the other hand is something I'm far less ashamed of and really lets Haskell shine, it basically didn't need any code at all. I just defined how to parse an equivalent data representation, how it was sorted via Eq/Ord typeclass (following the brief), and used `compare` to do the lifting. instance Eq PacketData where
(L l) == (P p) = P [L l] == P p
(P p) == (L l) = L l == P p
P p == P p' = p == p'
L l == L l' = l == l'
instance Ord PacketData where
compare (L l) (P p) = compare (P [L l]) (P p)
compare (P p) (L l) = compare (P p) (P [L l])
compare (P p) (P p') = compare p p'
compare (L l) (L l') = compare l l'
https://github.com/cronin101/AdventOfCode/blob/master/2022/D... |
|
For me the parsing was the hardest part when trying without mutable state so this helps a lot.