Skip to content

Instantly share code, notes, and snippets.

@newton-migosi
Created December 2, 2020 19:05
Show Gist options
  • Save newton-migosi/cfefc1a4af35edf401e126bc79ff790d to your computer and use it in GitHub Desktop.
Save newton-migosi/cfefc1a4af35edf401e126bc79ff790d to your computer and use it in GitHub Desktop.
Code for generating solutions to the Add23 Puzzle
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