Generalise allUnorderedPairs

Now that it's in the prelude, the desire to remove diagonals is less obvious than it was.
This commit is contained in:
George Thomas 2025-12-09 11:00:01 +00:00
parent c608e21e26
commit e85429629f
3 changed files with 4 additions and 4 deletions

View File

@ -108,5 +108,5 @@ listIndex n =
[] -> 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)
allUnorderedPairs :: Bool -> [a] -> [(a, a)]
allUnorderedPairs diagonals = concat . join (zipWith (flip $ map . (,)) . (bool tail toList diagonals) . tails)

View File

@ -40,4 +40,4 @@ puzzle =
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
allPairs = sortOn (quadrance . uncurry (-)) $ allUnorderedPairs False boxes

View File

@ -15,7 +15,7 @@ puzzle =
. fmap ((\(V2 x y) -> x * y) . (+ 1) . fmap abs . uncurry (-))
. fromMaybe (error "empty input")
. nonEmpty
. allUnorderedPairs
. allUnorderedPairs False
]
, extraTests = mempty
}