Created
December 12, 2017 10:52
-
-
Save JadenGeller/cb68695b1ac6068d3fa99512114a5324 to your computer and use it in GitHub Desktop.
Advent 2017 Day 1
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
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