Created
December 2, 2020 19:05
-
-
Save newton-migosi/cfefc1a4af35edf401e126bc79ff790d to your computer and use it in GitHub Desktop.
Code for generating solutions to the Add23 Puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
solution target sides width pool = loop sides 0 [] | |
where | |
loop 0 runningTotal accumulator = do | |
let correct = (== sides + 2) . length . concatMap (uncurry intersect) | |
guard . all correct . chunksOf sides . pairs $ accumulator | |
return accumulator | |
loop counter runningTotal accumulator = do | |
current <- sumTo total width pool | |
case accumulator of | |
[] -> guard True | |
(previous:_) -> guard (last current <= last previous) | |
let shares = map (length . intersect current) accumulator | |
guard $ all (< 2) shares | |
guard $ sum shares <= 2 | |
guard $ runningTotal + sum shares <= 4 | |
loop (counter-1) (runningTotal + sum shares) (current:accumulator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment