Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
module Tmp where
import Data.Monoid
import Control.Monad
import Data.Char
import Control.Applicative
import Control.Lens
initMap :: [(Char, (Int, Int))]
initMap = ('0', (1, 3)) : zip (map (chr . (ord '0' +)) [1..9]) (flip (,) <$> [0..2] <*> [0..2])
initMap' :: [((Int, Int), Char)]
initMap' = ((1, 3), '0') : zip (flip (,) <$> [0..2] <*> [0..2]) (map (chr . (ord '0' +)) [1..9])
list :: Int -> [(Int, Int)]
list maxOff = (,) <$> [-maxOff .. maxOff] <*> [-maxOff .. maxOff]
check :: Char -> (Int, Int) -> Maybe Char
check c off = (+) <$> lookup c initMap <*> return off >>= flip lookup initMap'
inp' :: String -> (Int, Int) -> Maybe [Char]
inp' = foldr ((liftA2 . liftA2) (:)) (const $ return []) . fmap check
success :: Maybe a -> Int
success Nothing = 0
success _ = 1
sol :: String -> Int -> Int
sol inp = foldl (\a -> (a +) . success) 0 . fmap (inp' inp) . list
instance Num (Int, Int) where
(+) (x, y) (x', y') = (x + x', y + y')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment