For now this applies to Haskell only, and it may turn out to be tricky for the Rust implementation. In practice, the limitation hasn't turned out to be important, and we could even go the other way and use `Integer` everywhere. This does however at least help with debugging, as well as just being conceptually right. The `nil` and `(/\)` functions are intended to be overloaded to work for other list-like things in a later commit, and from there we will investigate using `OverloadedLists` and `RebindableSyntax` to recover standard list syntax, although there are probably limitations due to `(:)` being special.
43 lines
1.4 KiB
Haskell
43 lines
1.4 KiB
Haskell
module Puzzles.Day8 (puzzle) where
|
|
|
|
import Pre
|
|
|
|
import Control.Lens
|
|
import Data.DisjointSet qualified as DS
|
|
import Linear.Metric
|
|
import Linear.V3
|
|
|
|
puzzle :: Puzzle
|
|
puzzle =
|
|
Puzzle
|
|
{ number = 8
|
|
, parser = \isRealData ->
|
|
(if isRealData then 1000 else 10,)
|
|
<$> (V3 <$> decimal <* single ',' <*> decimal <* single ',' <*> decimal) `sepEndBy` newline
|
|
, parts =
|
|
( uncurry \n ->
|
|
product
|
|
. take 3
|
|
. sortOn Down
|
|
. map length
|
|
. DS.toLists
|
|
. maybe (error "not enough boxes") snd
|
|
. listIndex n
|
|
. connectBoxes
|
|
)
|
|
/\ ( uncurry . const $
|
|
uncurry ((*) `on` view _x)
|
|
. maybe (error "sets never unified") fst
|
|
. lastMay
|
|
. takeWhile ((> 1) . DS.sets . snd)
|
|
. connectBoxes
|
|
)
|
|
/\ nil
|
|
, extraTests = mempty
|
|
}
|
|
|
|
connectBoxes :: [V3 Int] -> [((V3 Int, V3 Int), DS.DisjointSet (V3 Int))]
|
|
connectBoxes boxes = zip allPairs $ scanl (flip $ uncurry DS.union) (foldMap DS.singleton boxes) allPairs
|
|
where
|
|
allPairs = sortOn (quadrance . uncurry (-)) $ allUnorderedPairs False boxes
|