1
0
Fork 0

simple runner for 2022

This commit is contained in:
Sloane Perrault 2022-12-01 10:07:52 -05:00
parent b649bd5030
commit 001b947a5e
4 changed files with 34 additions and 30 deletions

View file

@ -25,7 +25,7 @@ source-repository head
library library
exposed-modules: exposed-modules:
Lib Aoc.Day1
other-modules: other-modules:
Paths_aoc Paths_aoc
hs-source-dirs: hs-source-dirs:

View file

@ -2,32 +2,18 @@
module Main (main) where module Main (main) where
import System.Environment (getArgs) import System.Environment (getArgs)
import Data.Ord import qualified Aoc.Day1
import Data.List (sortBy)
import Data.Text (pack, splitOn, unpack)
parseInput :: String -> [[Integer]]
parseInput = (map (map read . words . unpack)) . splitOn "\n\n" . pack
inputSums :: String -> [Integer]
inputSums = map sum . parseInput
part1 :: String -> Integer
part1 = maximum . inputSums
part2 :: String -> Integer
part2 = sum . take 3 . sortDesc . inputSums
where
sortDesc = sortBy (comparing Down)
main :: IO () main :: IO ()
main = do main = do
args <- getArgs (dayS:partS:_) <- getArgs
contents <- getContents let day = read dayS :: Integer
let part = read partS :: Integer
let f = case args of ("1":_) -> part1 contents <- readFile ("input/" ++ (show day) ++ ".txt")
("2":_) -> part2
_ -> error "unknown part"
putStrLn $ show $ f contents let f = case day of 1 -> Aoc.Day1.solve
_ -> error "unknown day"
putStrLn $ show $ f part contents

24
2022/src/Aoc/Day1.hs Normal file
View file

@ -0,0 +1,24 @@
{-# LANGUAGE OverloadedStrings #-}
module Aoc.Day1 (solve) where
import Data.Ord
import Data.List (sortBy)
import Data.Text (pack, splitOn, unpack)
parseInput :: String -> [[Integer]]
parseInput = (map (map read . words . unpack)) . splitOn "\n\n" . pack
inputSums :: String -> [Integer]
inputSums = map sum . parseInput
part1 :: String -> Integer
part1 = maximum . inputSums
part2 :: String -> Integer
part2 = sum . take 3 . sortDesc . inputSums
where
sortDesc = sortBy (comparing Down)
solve :: Integer -> String -> Integer
solve 1 input = part1 input
solve 2 input = part2 input

View file

@ -1,6 +0,0 @@
module Lib
( someFunc
) where
someFunc :: IO ()
someFunc = putStrLn "someFunc"