simple runner for 2022
This commit is contained in:
parent
b649bd5030
commit
001b947a5e
4 changed files with 34 additions and 30 deletions
|
@ -25,7 +25,7 @@ source-repository head
|
|||
|
||||
library
|
||||
exposed-modules:
|
||||
Lib
|
||||
Aoc.Day1
|
||||
other-modules:
|
||||
Paths_aoc
|
||||
hs-source-dirs:
|
||||
|
|
|
@ -2,32 +2,18 @@
|
|||
module Main (main) where
|
||||
|
||||
import System.Environment (getArgs)
|
||||
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)
|
||||
import qualified Aoc.Day1
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
contents <- getContents
|
||||
(dayS:partS:_) <- getArgs
|
||||
let day = read dayS :: Integer
|
||||
let part = read partS :: Integer
|
||||
|
||||
let f = case args of ("1":_) -> part1
|
||||
("2":_) -> part2
|
||||
_ -> error "unknown part"
|
||||
contents <- readFile ("input/" ++ (show day) ++ ".txt")
|
||||
|
||||
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
24
2022/src/Aoc/Day1.hs
Normal 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
|
|
@ -1,6 +0,0 @@
|
|||
module Lib
|
||||
( someFunc
|
||||
) where
|
||||
|
||||
someFunc :: IO ()
|
||||
someFunc = putStrLn "someFunc"
|
Loading…
Reference in a new issue