Skip to content

Instantly share code, notes, and snippets.

@theSeafarer
Created December 1, 2018 21:27
Show Gist options
  • Save theSeafarer/07eb10102bb5400d8f9e35853c034174 to your computer and use it in GitHub Desktop.
Save theSeafarer/07eb10102bb5400d8f9e35853c034174 to your computer and use it in GitHub Desktop.
Advent of Code 2018, Day 1
-- I don't know if I'm gonna continue doing Advent of Code till the end,
-- but I felt that I should share with the world, the horror that is this module's `main`
module Adv1 where
import qualified Data.IntSet as S
partOne :: [Int] -> Int
partOne = sum
partTwo :: [Int] -> Int
partTwo ls = goThrough (cycle ls) 0 S.empty
where
goThrough list frq frqs = if newFrq `S.member` frqs
then newFrq
else goThrough (tail list) newFrq (S.insert newFrq frqs)
where
newFrq = frq + (head list)
main = (loop []) >>= mconcat . (print <$>) . (<*>) [partOne, partTwo] . (: [])
where
loop listed = do
line <- getLine
if line == ""
then pure listed
else loop $ listed ++ [read' line]
read' s = if head s == '+' then read $ tail s else read s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment