Skip to content

Instantly share code, notes, and snippets.

@LukaHorvat
Created May 21, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukaHorvat/d8aa733fee3da7ab5ac08817e4203ca7 to your computer and use it in GitHub Desktop.
Save LukaHorvat/d8aa733fee3da7ab5ac08817e4203ca7 to your computer and use it in GitHub Desktop.
import qualified Data.List.NonEmpty as NE
import Data.List (sortOn, sort)
import qualified Data.Set as Set
import Data.Function
uniqueFactorsAndSums :: Set.Set (Int, Int)
uniqueFactorsAndSums =
[(x * y, x + y) | x <- [1..1000], y <- [x..1000]]
& NE.groupAllWith fst
& filter ((== 1) . length)
& map NE.head
& Set.fromList
uniqueSums :: Set.Set Int
uniqueFactors :: Set.Set Int
(uniqueFactors, uniqueSums) = (Set.fromList p, Set.fromList s)
where
(p, s) = unzip (Set.toList uniqueFactorsAndSums)
uniqueFactorsAfterBob :: Set.Set Int
uniqueFactorsAfterBob =
[x * y | x <- [1..1000], y <- [x..1000], not ((x + y) `Set.member` uniqueSums)]
& NE.groupAllWith id
& filter ((== 1) . length)
& map NE.head
& Set.fromList
uniqueSumsAfterAlice :: Set.Set (Int, (Int, Int))
uniqueSumsAfterAlice =
[(x + y, (x, y))
| x <- [1..1000], y <- [x..1000]
, not ((x + y) `Set.member` uniqueSums)
, (x * y) `Set.member` uniqueFactorsAfterBob ]
& NE.groupAllWith fst
& filter ((== 1) . length)
& map NE.head
& Set.fromList
afterAliceAndBob :: Set.Set (Int, Int)
afterAliceAndBob = Set.map snd uniqueSumsAfterAlice
charlie =
[(y - x, [x, y]) | (x, y) <- Set.toList afterAliceAndBob]
& NE.groupAllWith fst
& fmap (\g -> (fst (NE.head g), sort $ foldMap snd g))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment