From 0d8293833fbee3c8bcc5d712ab38c6913222a1c2 Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Sun, 4 Dec 2022 08:33:19 -0500 Subject: [PATCH] solve 2022 day 3 pt 1 & 2 --- 2022/README.md | 3 ++- 2022/aoc.cabal | 7 +++++++ 2022/app/Main.hs | 2 ++ 2022/package.yaml | 2 ++ 2022/src/Aoc/Day3.hs | 29 +++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 2022/src/Aoc/Day3.hs diff --git a/2022/README.md b/2022/README.md index f532b69..92bcc1a 100644 --- a/2022/README.md +++ b/2022/README.md @@ -2,7 +2,7 @@ | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :--: | -| | | | | [1] | [2] | 3 | +| | | | | [1] | [2] | [3] | | 4 | 5 | 6 | 7 | 8 | 9 | 10 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | @@ -10,3 +10,4 @@ [1]: ./src/Aoc/Day1.hs [2]: ./src/Aoc/Day2.hs +[3]: ./src/Aoc/Day3.hs diff --git a/2022/aoc.cabal b/2022/aoc.cabal index 3166d85..a1ce7eb 100644 --- a/2022/aoc.cabal +++ b/2022/aoc.cabal @@ -27,6 +27,7 @@ library exposed-modules: Aoc.Day1 Aoc.Day2 + Aoc.Day3 other-modules: Paths_aoc hs-source-dirs: @@ -34,6 +35,8 @@ library ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints build-depends: base >=4.7 && <5 + , containers + , split , text default-language: Haskell2010 @@ -47,6 +50,8 @@ executable aoc-exe build-depends: aoc , base >=4.7 && <5 + , containers + , split , text default-language: Haskell2010 @@ -61,5 +66,7 @@ test-suite aoc-test build-depends: aoc , base >=4.7 && <5 + , containers + , split , text default-language: Haskell2010 diff --git a/2022/app/Main.hs b/2022/app/Main.hs index d6bf0ab..2e437b4 100644 --- a/2022/app/Main.hs +++ b/2022/app/Main.hs @@ -4,6 +4,7 @@ module Main (main) where import System.Environment (getArgs) import qualified Aoc.Day1 import qualified Aoc.Day2 +import qualified Aoc.Day3 main :: IO () main = do @@ -15,6 +16,7 @@ main = do let f = case day of 1 -> Aoc.Day1.solve 2 -> Aoc.Day2.solve + 3 -> Aoc.Day3.solve _ -> error "unknown day" putStrLn $ show $ f part contents diff --git a/2022/package.yaml b/2022/package.yaml index c85a109..d0fcbc5 100644 --- a/2022/package.yaml +++ b/2022/package.yaml @@ -22,6 +22,8 @@ description: Please see the README on GitHub at = 4.7 && < 5 - text +- containers +- split ghc-options: - -Wall diff --git a/2022/src/Aoc/Day3.hs b/2022/src/Aoc/Day3.hs new file mode 100644 index 0000000..f8eccf4 --- /dev/null +++ b/2022/src/Aoc/Day3.hs @@ -0,0 +1,29 @@ +module Aoc.Day3 (solve, halve, shared, charToInt) where + +import Data.Char +import qualified Data.Set as Set +import Data.List.Split (chunksOf) + +halve :: String -> [String] +halve xs = [front, back] + where + midpoint = length xs `div` 2 + + front = take midpoint xs + back = drop midpoint xs + +shared :: [String] -> String +shared xs = Set.toList $ foldr1 Set.intersection $ map Set.fromList xs + +charToInt :: Char -> Int +charToInt c + | 'a' <= c && c <= 'z' = ord c - ord 'a' + 1 + | 'A' <= c && c <= 'Z' = ord c - ord 'A' + 27 + | otherwise = -1 + +solve :: Integer -> String -> Integer +solve 1 input = sum $ map (toInteger . charToInt . head . shared . halve) $ lines input + +solve 2 input = sum $ map (toInteger . charToInt . head . shared) $ chunksOf 3 $ lines input + +solve _ _ = 0