Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created December 12, 2017 10:52
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 JadenGeller/cb68695b1ac6068d3fa99512114a5324 to your computer and use it in GitHub Desktop.
Save JadenGeller/cb68695b1ac6068d3fa99512114a5324 to your computer and use it in GitHub Desktop.
Advent 2017 Day 1
import Data.Vect
import Data.Vect.Views
%default total
infixl 1 &
(&) : a -> (a -> b) -> b
x & f = f x
rotate : Vect len elem -> Vect len elem
rotate xs {len} with (snocVect xs)
rotate [] | Empty = []
rotate (ys ++ [y]) {len=m + S Z} | Snoc z = rewrite (plusCommutative m 1)
in [y] ++ ys
toDigit : Char -> Maybe Nat
toDigit c with (toNat c)
toDigit c | n with (48 `isLTE` n)
toDigit c | n | Yes prf = Just (n - 48)
toDigit c | n | No _ = Nothing
toDigits : String -> Maybe (List Nat)
toDigits str = sequence (map toDigit (unpack str))
computeCaptcha : List Nat -> Nat
computeCaptcha xs = let xs' = fromList xs
in zip xs' (rotate xs')
& toList
& filter (uncurry (==))
& map fst
& sum
solve : String -> Maybe Nat
solve str = toDigits str & map computeCaptcha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment