copied solution from github
This commit is contained in:
parent
97051e9327
commit
174a54ff98
5
day01/CHANGELOG.md
Normal file
5
day01/CHANGELOG.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Revision history for day01
|
||||
|
||||
## 0.1.0.0 -- YYYY-mm-dd
|
||||
|
||||
* First version. Released on an unsuspecting world.
|
||||
9
day01/app/Main.hs
Normal file
9
day01/app/Main.hs
Normal file
@ -0,0 +1,9 @@
|
||||
module Main where
|
||||
|
||||
import Day01 (day01Part1, day01Part2)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- readFile "./data/input.txt"
|
||||
putStrLn $ "Part 1: " <> day01Part1 input
|
||||
putStrLn $ "Part 2: " <> day01Part2 input
|
||||
2000
day01/data/input.txt
Normal file
2000
day01/data/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
42
day01/day01.cabal
Normal file
42
day01/day01.cabal
Normal file
@ -0,0 +1,42 @@
|
||||
cabal-version: 3.6
|
||||
name: day01
|
||||
version: 0.1.0.0
|
||||
|
||||
-- A short (one-line) description of the package.
|
||||
-- synopsis:
|
||||
|
||||
-- A longer description of the package.
|
||||
-- description:
|
||||
|
||||
-- A URL where users can report bugs.
|
||||
-- bug-reports:
|
||||
|
||||
-- The license under which the package is released.
|
||||
-- license:
|
||||
author: Andrew Maier
|
||||
maintainer: andrew@maier.name
|
||||
|
||||
-- A copyright notice.
|
||||
-- copyright:
|
||||
-- category:
|
||||
extra-source-files: CHANGELOG.md
|
||||
|
||||
executable day01
|
||||
main-is: Main.hs
|
||||
|
||||
-- Modules included in this executable, other than Main.
|
||||
-- other-modules:
|
||||
|
||||
-- LANGUAGE extensions used by modules in this package.
|
||||
-- other-extensions:
|
||||
build-depends: base ^>=4.14.3.0
|
||||
, day01
|
||||
hs-source-dirs: app
|
||||
default-language: Haskell2010
|
||||
|
||||
library
|
||||
build-depends: base ^>=4.14.3.0
|
||||
hs-source-dirs: lib
|
||||
exposed-modules: Day01
|
||||
default-language: Haskell2010
|
||||
|
||||
38
day01/lib/Day01.hs
Normal file
38
day01/lib/Day01.hs
Normal file
@ -0,0 +1,38 @@
|
||||
module Day01 where
|
||||
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Text.Read (readMaybe)
|
||||
|
||||
day01Part1 :: String -> String
|
||||
day01Part1 s = case depths s of
|
||||
Right ds -> show . countIncreases . changes $ ds
|
||||
Left e -> e
|
||||
|
||||
depths :: String -> Either String [Int]
|
||||
depths s = case maybeDepths s of
|
||||
Just ds -> Right ds
|
||||
Nothing -> Left "Error processing data"
|
||||
where
|
||||
maybeDepths :: String -> Maybe [Int]
|
||||
maybeDepths = mapM readMaybe . lines
|
||||
|
||||
data ChangeType = Increase | Decrease | Level deriving (Show, Eq)
|
||||
|
||||
changes :: [Int] -> [ChangeType]
|
||||
changes s = zipWith zipper (tail s) s
|
||||
where
|
||||
zipper :: Int -> Int -> ChangeType
|
||||
zipper n2 n1 | n1 < n2 = Increase
|
||||
| n1 > n2 = Decrease
|
||||
| otherwise = Level
|
||||
|
||||
countIncreases :: [ChangeType] -> Int
|
||||
countIncreases = sum . map (\ch -> if ch == Increase then 1 else 0)
|
||||
|
||||
day01Part2 :: String -> String
|
||||
day01Part2 s = case depths s of
|
||||
Right ds -> show . countIncreases . changes . averages $ ds
|
||||
Left e -> e
|
||||
|
||||
averages :: [Int] -> [Int]
|
||||
averages s = zipWith3 (\a b c -> a + b + c) (drop 2 s) (tail s) s
|
||||
1
project.cabal
Normal file
1
project.cabal
Normal file
@ -0,0 +1 @@
|
||||
packages: ./day*/*.cabal
|
||||
Loading…
x
Reference in New Issue
Block a user