]> localhost Git - adventofcode2018.git/commitdiff
Complete Day 1
authorJack Kinsey <j.jameskinsey@gmail.com>
Sat, 1 Dec 2018 21:50:21 +0000 (16:50 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Sat, 1 Dec 2018 21:50:21 +0000 (16:50 -0500)
.gitignore [new file with mode: 0644]
day01/part1.hs [new file with mode: 0644]
day01/part2.hs [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..3f9177e
--- /dev/null
@@ -0,0 +1 @@
+input
diff --git a/day01/part1.hs b/day01/part1.hs
new file mode 100644 (file)
index 0000000..e8941d7
--- /dev/null
@@ -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 (file)
index 0000000..5899316
--- /dev/null
@@ -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