From e85429629f0373b1484ba02250b2092fa2568e91 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Tue, 9 Dec 2025 11:00:01 +0000 Subject: [PATCH] Generalise `allUnorderedPairs` Now that it's in the prelude, the desire to remove diagonals is less obvious than it was. --- haskell/Pre.hs | 4 ++-- haskell/Puzzles/Day8.hs | 2 +- haskell/Puzzles/Day9.hs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/haskell/Pre.hs b/haskell/Pre.hs index 9447da9..7e6a5c5 100644 --- a/haskell/Pre.hs +++ b/haskell/Pre.hs @@ -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) diff --git a/haskell/Puzzles/Day8.hs b/haskell/Puzzles/Day8.hs index d0a730d..47d7ecf 100644 --- a/haskell/Puzzles/Day8.hs +++ b/haskell/Puzzles/Day8.hs @@ -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 diff --git a/haskell/Puzzles/Day9.hs b/haskell/Puzzles/Day9.hs index 826eee3..740cc15 100644 --- a/haskell/Puzzles/Day9.hs +++ b/haskell/Puzzles/Day9.hs @@ -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 }