From 001b947a5e659d9478d686f0fbcb658514af940b Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Thu, 1 Dec 2022 10:07:52 -0500 Subject: [PATCH] simple runner for 2022 --- 2022/aoc.cabal | 2 +- 2022/app/Main.hs | 32 +++++++++----------------------- 2022/src/Aoc/Day1.hs | 24 ++++++++++++++++++++++++ 2022/src/Lib.hs | 6 ------ 4 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 2022/src/Aoc/Day1.hs delete mode 100644 2022/src/Lib.hs diff --git a/2022/aoc.cabal b/2022/aoc.cabal index f8eafda..41b5d48 100644 --- a/2022/aoc.cabal +++ b/2022/aoc.cabal @@ -25,7 +25,7 @@ source-repository head library exposed-modules: - Lib + Aoc.Day1 other-modules: Paths_aoc hs-source-dirs: diff --git a/2022/app/Main.hs b/2022/app/Main.hs index af0ad0a..0570781 100644 --- a/2022/app/Main.hs +++ b/2022/app/Main.hs @@ -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 diff --git a/2022/src/Aoc/Day1.hs b/2022/src/Aoc/Day1.hs new file mode 100644 index 0000000..ac68712 --- /dev/null +++ b/2022/src/Aoc/Day1.hs @@ -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 diff --git a/2022/src/Lib.hs b/2022/src/Lib.hs deleted file mode 100644 index d36ff27..0000000 --- a/2022/src/Lib.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Lib - ( someFunc - ) where - -someFunc :: IO () -someFunc = putStrLn "someFunc"