day1: add solution

This commit is contained in:
Sijmen 2018-12-01 14:52:17 +01:00
commit ab438d84d2
3 changed files with 23 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.in

8
Day1A.hs Normal file
View File

@ -0,0 +1,8 @@
module Day1A where
read' :: String -> Integer
read' ('+':str) = read str
read' str = read str
main :: IO ()
main = interact $ show . sum . map read' . lines

14
Day1B.hs Normal file
View File

@ -0,0 +1,14 @@
module Day1B where
import Day1A (read')
import qualified Data.Set as S
handle :: Integer -> (S.Set Integer) -> [Integer] -> Integer
handle freq history (nextDelta:xs)
| S.member freq history = freq -- When the frequency is in the history set, return it
| otherwise = handle (freq + nextDelta) (S.insert freq history) xs -- Recurse otherwise
handle _ _ _ = error "xs should not be empty :("
main :: IO ()
main = interact $ show . handle 0 S.empty . deltaFreqs
where deltaFreqs = cycle . map read' . lines -- Convert input to Integers and repeat infinitely