From 873455cab884fb90e5948bf2094907c63e7ed5ef Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Sat, 1 Dec 2018 16:04:55 -0600 Subject: [PATCH] Day 1, parts 1 & 2: Initial solutions. --- .gitignore | 3 + Day1/Part1.hs | 8 + Day1/Part2.hs | 18 + Day1/input | 959 ++++++++++++++++++++++++++++++++++++++++++++++++++ stack.yaml | 4 + 5 files changed, 992 insertions(+) create mode 100644 .gitignore create mode 100755 Day1/Part1.hs create mode 100755 Day1/Part2.hs create mode 100644 Day1/input create mode 100644 stack.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5077d58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.stack-work/ +*.hi +*.o diff --git a/Day1/Part1.hs b/Day1/Part1.hs new file mode 100755 index 0000000..c0d21f9 --- /dev/null +++ b/Day1/Part1.hs @@ -0,0 +1,8 @@ +#! /usr/bin/env stack +-- stack --resolver lts-12.20 --install-ghc script +module Main where + +main :: IO () +main = interact $ show . \input -> + let changes = fmap (read . dropWhile (=='+')) $ lines input + in (sum changes :: Int) diff --git a/Day1/Part2.hs b/Day1/Part2.hs new file mode 100755 index 0000000..2e1c54b --- /dev/null +++ b/Day1/Part2.hs @@ -0,0 +1,18 @@ +#! /usr/bin/env stack +-- stack --resolver lts-12.20 --install-ghc script +module Main where + +import Data.List (scanl') +import qualified Data.Set as S + +loopRight :: b -> (b -> Either a b) -> a +loopRight b f = case f b of + Left a -> a + Right b' -> loopRight b' f + +main :: IO () +main = interact $ show . \input -> + let changes = fmap (read . dropWhile (=='+')) $ lines input + freq = scanl' (+) 0 (cycle changes) + in loopRight (S.empty, freq) $ \(s, f:fs) -> + if S.member f s then Left (f :: Int) else Right (S.insert f s, fs) diff --git a/Day1/input b/Day1/input new file mode 100644 index 0000000..7401a29 --- /dev/null +++ b/Day1/input @@ -0,0 +1,959 @@ +-2 +-6 ++2 +-10 +-4 +-7 ++8 +-10 +-15 +-4 ++20 ++16 ++20 ++5 +-2 ++16 ++8 +-10 +-2 +-21 ++13 ++6 ++5 +-14 +-13 +-17 +-8 +-7 ++19 +-21 +-16 ++19 ++7 ++3 ++17 ++6 ++8 +-4 ++2 ++21 ++8 ++11 ++5 ++8 +-15 +-8 ++1 ++17 +-7 +-7 +-17 ++10 ++6 ++6 ++12 ++16 ++1 ++11 +-10 +-13 +-2 ++12 +-14 ++13 ++13 ++13 ++6 +-4 ++1 ++6 +-1 +-18 ++14 ++3 ++18 +-8 ++15 +-18 +-21 ++4 +-8 +-7 +-16 +-10 ++2 +-12 +-15 ++13 +-11 +-9 +-5 +-20 ++6 +-18 +-15 +-1 ++8 ++12 ++15 ++15 ++20 ++11 ++15 ++17 ++10 +-8 +-7 ++1 ++12 ++7 ++11 ++3 ++5 +-2 ++14 +-8 ++15 +-3 +-5 +-17 ++15 +-3 ++23 ++14 +-11 +-10 ++14 +-15 ++6 ++10 +-9 ++10 +-3 +-4 ++10 ++8 ++16 +-18 +-2 ++11 ++14 +-16 ++6 +-8 ++16 +-18 +-16 +-4 +-6 ++2 +-3 ++2 +-8 +-7 +-5 ++13 ++2 +-12 +-5 +-23 ++13 +-26 +-16 +-1 +-9 +-8 ++21 +-1 +-16 +-21 +-16 ++14 +-10 ++13 ++18 ++17 ++3 ++18 +-10 ++24 ++10 +-20 ++36 +-11 +-19 ++18 ++43 ++2 ++5 ++12 ++14 +-15 +-14 ++5 ++11 ++18 +-15 ++13 +-4 ++8 +-5 +-6 ++17 ++1 +-3 ++4 ++11 ++16 ++16 ++11 ++11 ++13 +-12 ++14 +-16 +-7 +-9 ++4 +-10 +-13 +-19 ++11 ++5 +-3 +-9 ++17 ++6 ++9 +-12 ++17 +-11 ++16 ++13 ++14 ++10 +-5 ++2 +-16 ++18 +-1 ++8 ++17 ++11 ++13 +-18 ++10 +-1 ++4 ++9 ++16 ++13 +-18 ++1 ++10 +-7 ++11 +-12 +-12 ++7 +-20 ++18 +-13 ++3 ++21 ++17 ++5 ++14 +-2 +-14 ++13 ++4 +-11 +-19 +-15 +-7 ++18 +-1 ++10 ++9 +-8 ++2 +-16 +-13 +-9 ++3 ++10 ++15 ++18 ++9 +-18 +-13 ++5 ++10 ++12 +-10 ++17 ++16 +-12 ++23 ++9 ++18 ++4 +-28 ++20 +-4 ++56 +-19 +-52 +-28 +-8 +-35 +-20 +-3 +-5 ++16 +-19 +-8 ++3 ++9 ++19 ++22 ++10 +-9 ++44 ++4 +-17 +-12 +-48 ++7 +-14 +-22 ++9 +-4 +-16 ++5 ++12 +-3 +-16 ++12 ++2 +-15 ++12 +-26 +-7 +-12 +-18 +-16 ++6 +-15 ++17 +-9 +-4 +-8 +-7 ++12 +-16 ++8 +-5 ++3 ++7 ++8 +-14 +-8 +-13 ++10 ++14 ++26 ++14 +-6 ++22 +-13 +-1 ++11 +-17 ++15 ++6 ++18 ++13 +-5 ++8 ++1 +-5 +-19 +-3 +-3 ++14 +-10 +-17 ++5 ++16 ++7 ++8 ++5 ++16 +-20 ++19 ++4 +-30 +-1 +-20 ++5 +-32 +-12 ++20 +-14 ++3 +-11 +-18 +-21 ++4 ++1 +-19 +-17 ++13 ++12 ++13 +-1 +-17 +-19 ++13 +-6 ++20 +-17 +-14 +-22 ++9 +-3 ++18 ++24 +-7 ++25 +-38 +-96 ++13 ++37 ++25 ++58 +-13 ++42 ++28 ++8 ++159 +-43 +-572 +-62794 ++19 ++8 +-17 +-9 ++3 +-1 ++5 ++4 ++16 ++11 ++4 +-18 ++5 +-10 ++9 ++15 ++16 +-5 ++17 +-16 +-4 +-11 +-18 ++3 +-12 ++3 +-18 +-7 +-7 ++5 ++3 +-17 ++8 +-15 +-2 ++11 +-6 ++17 +-12 +-9 ++6 +-4 ++10 ++7 +-1 +-7 +-16 ++3 +-1 +-9 +-4 +-10 ++2 +-13 ++4 +-19 +-7 +-5 ++1 +-17 +-3 +-13 ++15 +-3 ++9 ++14 +-9 ++4 +-1 +-9 ++14 +-6 +-3 +-17 ++19 +-8 +-15 +-9 +-18 ++9 ++19 +-3 ++16 +-20 ++3 +-19 ++10 ++14 ++11 +-3 ++10 ++14 ++12 +-14 ++15 +-10 +-10 ++15 ++9 ++9 ++4 +-2 +-9 +-8 ++11 ++5 ++16 +-8 ++16 +-11 ++9 +-18 +-11 ++17 ++15 +-16 ++2 +-23 ++14 ++4 ++14 ++1 ++6 ++9 +-19 +-3 +-17 +-6 +-16 +-17 ++16 ++15 +-9 ++32 ++11 +-5 +-22 +-14 ++5 +-30 +-13 +-10 +-11 +-15 +-5 +-3 +-7 ++3 ++9 ++5 +-1 +-17 +-17 ++13 ++14 +-7 +-16 ++18 +-16 ++17 +-9 ++10 +-17 +-10 ++19 ++10 +-16 +-15 ++16 +-6 +-6 +-9 +-17 ++16 ++3 +-7 ++17 +-16 +-16 +-9 +-9 ++8 +-1 +-6 +-14 ++17 +-12 +-13 ++12 +-1 ++13 ++2 ++10 +-4 +-5 +-8 ++14 +-18 ++16 ++7 ++8 ++11 +-14 +-8 +-15 +-21 +-18 ++10 +-3 ++8 ++8 +-6 +-13 +-11 ++6 +-2 ++14 +-16 +-16 +-17 ++10 ++11 +-13 +-18 +-5 ++4 ++9 +-6 ++5 ++16 ++13 ++8 +-15 ++20 +-7 ++19 ++7 ++15 +-7 +-7 +-18 +-14 ++9 +-6 ++13 ++11 +-8 ++6 +-5 +-3 +-26 +-9 ++6 +-8 ++15 +-18 ++12 +-2 +-8 +-25 ++15 +-16 +-1 +-7 +-18 ++14 ++6 ++10 +-15 +-17 ++12 +-5 ++8 +-7 +-10 +-5 +-18 ++1 ++14 +-2 ++11 ++4 ++18 +-10 +-19 +-18 +-19 ++10 +-14 +-12 ++14 +-15 ++10 ++2 ++12 +-8 ++14 ++19 +-20 ++11 +-8 ++10 +-21 ++25 ++21 ++1 ++20 ++17 ++6 ++11 +-13 +-17 +-20 ++4 +-1 +-19 +-11 ++14 ++12 +-18 ++5 +-11 ++33 ++13 ++8 ++3 ++9 +-10 ++30 +-18 +-3 ++23 ++42 ++4 +-7 ++10 +-14 ++20 +-5 ++30 ++1 +-15 ++19 ++12 +-3 +-6 ++7 ++21 +-3 ++27 ++17 ++15 +-18 +-4 +-17 ++22 +-14 +-9 ++12 ++2 +-1 ++7 +-10 ++6 ++3 ++17 +-4 +-6 ++13 +-34 +-33 +-2 ++18 +-11 +-17 +-20 +-3 ++15 +-5 +-6 +-48 ++12 +-22 +-12 +-17 +-25 +-9 +-24 +-15 +-22 +-15 ++17 ++5 +-14 ++22 ++3 +-1 +-18 +-37 +-3 +-8 ++4 ++9 ++7 +-5 +-19 +-19 +-4 ++12 +-2 ++35 +-28 ++8 ++11 ++80 ++20 ++12 +-17 ++16 +-40 +-43 +-11 +-19 +-8 +-2 ++20 +-33 ++9 +-71 +-6 ++1 +-128 ++1 ++5 ++28 +-27 ++16 ++40 +-9 ++265 ++54 +-9 ++43 +-39 +-63256 ++12 ++18 +-14 ++13 ++15 +-10 ++13 ++5 ++1 ++14 ++15 +-7 ++18 +-2 +-7 +-14 +-6 +-10 +-15 +-3 ++12 ++17 +-19 ++12 ++8 ++12 +-19 ++4 +-18 +-14 +-20 +-6 +-19 ++12 +-11 +-19 ++2 ++19 ++4 +-12 ++3 ++19 ++11 ++9 +-17 +-11 ++1 ++13 ++11 +-18 +-12 +-2 +-14 +-13 +-6 ++7 ++2 +-10 +-14 +-18 ++1 ++14 ++11 +-6 +-6 +-3 +-18 +-7 ++127054 diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..6af939c --- /dev/null +++ b/stack.yaml @@ -0,0 +1,4 @@ +flags: {} +packages: [] +extra-deps: [] +resolver: lts-12.20