Skip to content

Instantly share code, notes, and snippets.

@sgronblo
Last active December 2, 2017 16:11
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 sgronblo/08f625fab8f446c68e74fec499cb25d6 to your computer and use it in GitHub Desktop.
Save sgronblo/08f625fab8f446c68e74fec499cb25d6 to your computer and use it in GitHub Desktop.
Advent of Code 2017 01 Captchas in Haskell
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script --package array
{-# LANGUAGE ScopedTypeVariables #-}
import System.Environment
import Data.Array.IArray
captchaPairs :: (IArray a e, Ix i, Num i) => a i e -> ((i, i) -> i) -> [(e, e)]
captchaPairs es getPairIndex =
let (_, maxIndex) = bounds es
createPair i = (es ! i, es ! getPairIndex (i, maxIndex + 1))
pairArray = map createPair (indices es)
in pairArray
getSum :: [(Int, Int)] -> Int
getSum pairs =
let equalPairs = filter (uncurry (==)) pairs
firsts = map fst equalPairs
in sum firsts
solve :: String -> ((Int, Int) -> Int) -> Int
solve s getPairIndex =
let (nums :: Array Int Int) = listArray (0, length s - 1) $ map (\c -> read [c]) s
pairs = captchaPairs nums getPairIndex
in getSum pairs
main :: IO ()
main = do
args <- getArgs
let typeOneSolution = solve (head args) (\(i, l) -> (i + 1) `mod` l)
putStrLn ("Type one solution: " ++ show typeOneSolution)
let typeTwoSolution = solve (head args) (\(i, l) -> (i + (l `div` 2)) `mod` l)
putStrLn ("Type two solution: " ++ show typeTwoSolution)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment