From b2f8fbc579dcbe52ce00847b1e0b0612b7b80d96 Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Sat, 1 Dec 2018 16:50:21 -0500 Subject: [PATCH 1/1] Complete Day 1 --- .gitignore | 1 + day01/part1.hs | 8 ++++++++ day01/part2.hs | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 .gitignore create mode 100644 day01/part1.hs create mode 100644 day01/part2.hs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f9177e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +input diff --git a/day01/part1.hs b/day01/part1.hs new file mode 100644 index 0000000..e8941d7 --- /dev/null +++ b/day01/part1.hs @@ -0,0 +1,8 @@ +main = do + contents <- getContents + print . sum $ map toInt (lines contents) + +toInt :: String -> Int +toInt (x:xs) = if x == '+' + then read xs :: Int + else -(read xs :: Int) diff --git a/day01/part2.hs b/day01/part2.hs new file mode 100644 index 0000000..5899316 --- /dev/null +++ b/day01/part2.hs @@ -0,0 +1,18 @@ +import qualified Data.Set as S + +main = do + contents <- getContents + let freqs = cycle $ map toInt (lines contents) + print $ findDouble (S.empty :: S.Set Int) 0 freqs + +toInt :: String -> Int +toInt (x:xs) = if x == '+' + then read xs :: Int + else -(read xs :: Int) + +findDouble :: S.Set Int -> Int -> [Int] -> Int +findDouble history total (x:xs) + | S.member total history = total + | otherwise = findDouble nextHist nextTotal xs + where nextHist = S.insert total history + nextTotal = total + x -- 2.38.5