day1 done

This commit is contained in:
Nicole Dresselhaus 2022-12-04 23:15:09 +01:00
parent 8d1416daa7
commit 5ce1fcfad1
4 changed files with 2342 additions and 0 deletions

37
AoC2022.cabal Normal file
View File

@ -0,0 +1,37 @@
cabal-version: 2.4
name: AoC2022
version: 0.1.0.0
-- A short (one-line) description of the package.
synopsis: Solutions for Advent of Code 2022
-- 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: CC-BY-NC-ND-4.0
author: Stefan Dresselhaus
maintainer: stefan@dresselhaus.cloud
-- A copyright notice.
-- copyright:
-- category:
extra-source-files:
CHANGELOG.md
README.md
executable Day1
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
, safe
hs-source-dirs: day1
default-language: Haskell2010

5
CHANGELOG.md Normal file
View File

@ -0,0 +1,5 @@
# Revision history for AoC2022
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.

50
day1/Main.hs Normal file
View File

@ -0,0 +1,50 @@
{-# LANGUAGE LambdaCase #-}
module Main where
import Data.List as L
import Data.Maybe
import Safe
data InputOptions = Elf1
| Elf3
| Quit
getInput :: IO InputOptions
getInput = do
putStrLn "Only get top Elf or sum of top 3 Elf? (1/3/q)"
getLine >>= \case
"1" -> return Elf1
"3" -> return Elf3
"q" -> return Quit
_ -> putStrLn "not understood" >> getInput
main :: IO ()
main = getInput >>= \case
Quit -> putStrLn "bye!"
Elf1 -> interact $
(<>"\n")
. show
. maximum
. fmap (sum . mapMaybe (readMay :: String -> Maybe Integer))
. splitOn ""
. lines
Elf3 -> interact $
(<>"\n")
. show
. sum
. take 3
. sortOn negate
. fmap (sum . mapMaybe (readMay :: String -> Maybe Integer))
. splitOn ""
. lines
-- | Split a list into sublists delimited by the given element.
--
-- From: https://hackage.haskell.org/package/haskell-gi-0.26.2/docs/src/Data.GI.CodeGen.Util.html#splitOn
splitOn :: Eq a => a -> [a] -> [[a]]
splitOn x xs = go xs []
where go [] acc = [reverse acc]
go (y : ys) acc = if x == y
then reverse acc : go ys []
else go ys (y : acc)

2250
day1/input Normal file

File diff suppressed because it is too large Load Diff