Solve day 9 part 1

This commit is contained in:
George Thomas 2025-12-09 10:47:32 +00:00
parent ce70643476
commit c608e21e26
4 changed files with 14 additions and 6 deletions

View File

@ -37,6 +37,7 @@ module Pre (
digit,
digitsToInt,
listIndex,
allUnorderedPairs,
)
where
@ -106,3 +107,6 @@ listIndex n =
else \case
[] -> Nothing
x : xs -> if n == 0 then Just x else listIndex (n - 1) xs
allUnorderedPairs :: [a] -> [(a, a)]
allUnorderedPairs = concat . join (zipWith (flip $ map . (,)) . tail . tails)

View File

@ -41,6 +41,3 @@ 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 boxes
allUnorderedPairs :: [a] -> [(a, a)]
allUnorderedPairs = concat . join (zipWith (flip $ map . (,)) . tail . tails)

View File

@ -2,14 +2,20 @@ module Puzzles.Day9 (puzzle) where
import Pre
import Data.Text.Lazy qualified as TL
puzzle :: Puzzle
puzzle =
Puzzle
{ number = 9
, parser = mempty
, parser = const $ (V2 @Int <$> decimal <* single ',' <*> decimal) `sepEndBy1` newline
, parts =
[ \() ->
"TODO"
[ TL.show
. maximum
. fmap ((\(V2 x y) -> x * y) . (+ 1) . fmap abs . uncurry (-))
. fromMaybe (error "empty input")
. nonEmpty
. allUnorderedPairs
]
, extraTests = mempty
}

1
outputs/real/9/1 Normal file
View File

@ -0,0 +1 @@
4758598740