Skip to content

Instantly share code, notes, and snippets.

@betaveros
Created June 1, 2013 03:36
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 betaveros/5689202 to your computer and use it in GitHub Desktop.
Save betaveros/5689202 to your computer and use it in GitHub Desktop.
find equations like (a + b + c = d) which are still true after 180-degree rotation
import Control.Applicative
import Data.Maybe
-- Calculator digit rotation
cRotateDigit 0 = Just 0
cRotateDigit 1 = Just 1
cRotateDigit 2 = Just 2
cRotateDigit 5 = Just 5
cRotateDigit 6 = Just 9
cRotateDigit 8 = Just 8
cRotateDigit 9 = Just 6
cRotateDigit _ = Nothing
-- Rotator / Cihan Altay rotation
rRotateDigit 0 = Just 0
rRotateDigit 1 = Just 1
rRotateDigit 2 = Just 3
rRotateDigit 3 = Just 2
rRotateDigit 4 = Just 7
rRotateDigit 5 = Just 5
rRotateDigit 6 = Just 9
rRotateDigit 7 = Just 4
rRotateDigit 8 = Just 8
rRotateDigit 9 = Just 6
rRotateDigit _ = Nothing
-- rotate allows leading zeroes after rotation
rotate n = rotate' n 0
where
rotate' 0 b = Just b
rotate' a b = (rotate' (a `quot` 10) . ((b*10) +)) =<< rRotateDigit (a `rem` 10)
-- rotatewoz rejects numbers that have leading zeroes if rotated
rotatewoz n = if n `rem` 10 == 0 then Nothing else rotate n
findRotationalSums lists = frs lists [] []
where
frs [] ns rns = if
fromMaybe False ((sum ns == ) <$> rotatewoz (sum rns))
then [ns] else []
frs lists ns rns =
concat [frs (tail lists) (n:ns) (rn:rns) |
n <- head lists, rn <- maybeToList $ rotatewoz n]
soln = findRotationalSums $ replicate 3 [100..999]
-- without unlinesing, solutions aren't printed as we find them (buffering)
main = putStrLn $ unlines $ map show $ soln
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment