From c4a55f955b9541113145cb33d0c658417ab8a013 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Sun, 25 Dec 2022 06:45:17 -0500 Subject: [PATCH] Day 25: Full of Hot Air --- README.md | 1 + hs/aoc2022.cabal | 3 +++ hs/app/Main.hs | 2 ++ hs/bench/Main.hs | 4 ++++ hs/src/Day25.hs | 20 ++++++++++++++++++++ hs/test/Day25Spec.hs | 30 ++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+) create mode 100644 hs/src/Day25.hs create mode 100644 hs/test/Day25Spec.hs diff --git a/README.md b/README.md index 1cb32bc..87a2235 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,4 @@ Development occurs in language-specific directories: |[Day22.hs](hs/src/Day22.hs)|[Day22.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day22.kt)|[day22.py](py/aoc2022/day22.py)|[day22.rs](rs/src/day22.rs)| |[Day23.hs](hs/src/Day23.hs)|[Day23.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day23.kt)|[day23.py](py/aoc2022/day23.py)|[day23.rs](rs/src/day23.rs)| |[Day24.hs](hs/src/Day24.hs)|[Day24.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day24.kt)|[day24.py](py/aoc2022/day24.py)|[day24.rs](rs/src/day24.rs)| +|[Day25.hs](hs/src/Day25.hs)| diff --git a/hs/aoc2022.cabal b/hs/aoc2022.cabal index 1efbb03..eeddbe0 100644 --- a/hs/aoc2022.cabal +++ b/hs/aoc2022.cabal @@ -35,6 +35,7 @@ data-files: , day22.txt , day23.txt , day24.txt + , day25.txt extra-source-files: README.md @@ -66,6 +67,7 @@ library , Day22 , Day23 , Day24 + , Day25 build-depends: array ^>=0.5.4.0 , base ^>=4.16.0.0 @@ -132,6 +134,7 @@ test-suite aoc2022-test , Day22Spec , Day23Spec , Day24Spec + , Day25Spec hs-source-dirs: test default-language: GHC2021 build-tool-depends: diff --git a/hs/app/Main.hs b/hs/app/Main.hs index 80166b6..daadfce 100644 --- a/hs/app/Main.hs +++ b/hs/app/Main.hs @@ -26,6 +26,7 @@ import Day21 (day21a, day21b) import Day22 (day22a, day22b) import Day23 (day23a, day23b) import Day24 (day24a, day24b) +import Day25 (day25) import Control.Monad ((<=<), ap, when) import Data.Function (on) @@ -79,3 +80,4 @@ main = do run 22 print [day22a, day22b] run 23 print [day23a, day23b] run 24 (maybe (fail "(⊥)") print) [day24a, day24b] + run 25 TIO.putStrLn [day25] diff --git a/hs/bench/Main.hs b/hs/bench/Main.hs index 96134f3..0d6caae 100644 --- a/hs/bench/Main.hs +++ b/hs/bench/Main.hs @@ -29,6 +29,7 @@ import Day21 (day21a, day21b) import Day22 (day22a, day22b) import Day23 (day23a, day23b) import Day24 (day24a, day24b) +import Day25 (day25) import Paths_aoc2022 (getDataFileName) import System.Environment.Blank (getEnv, setEnv, unsetEnv) @@ -141,4 +142,7 @@ main = defaultMain [ bench "part 1" $ nf day24a input , bench "part 2" $ nf day24b input ] + , env (getDayInput 25) $ \input -> bgroup "Day 24" + [ bench "part 1" $ nf day25 input + ] ] diff --git a/hs/src/Day25.hs b/hs/src/Day25.hs new file mode 100644 index 0000000..bda4c3e --- /dev/null +++ b/hs/src/Day25.hs @@ -0,0 +1,20 @@ +{-| +Module: Day25 +Description: +-} +module Day25 (day25) where + +import Data.Char (digitToInt, intToDigit) +import Data.Text (Text) +import qualified Data.Text as T (foldl', lines, unfoldr, reverse) + +day25 :: Text -> Text +day25 = T.reverse . T.unfoldr g . sum . map (T.foldl' f 0) . T.lines where + f k '=' = 5 * k - 2 + f k '-' = 5 * k - 1 + f k c = 5 * k + digitToInt c + g 0 = Nothing + g n = Just $ case n `divMod` 5 of + (q, 3) -> ('=', q + 1) + (q, 4) -> ('-', q + 1) + (q, r) -> (intToDigit r, q) diff --git a/hs/test/Day25Spec.hs b/hs/test/Day25Spec.hs new file mode 100644 index 0000000..2c5c943 --- /dev/null +++ b/hs/test/Day25Spec.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE OverloadedStrings #-} +module Day25Spec (spec) where + +import Data.Text (Text) +import qualified Data.Text as T (unlines) +import Day25 (day25) +import Test.Hspec (Spec, describe, it, shouldBe) + +example :: Text +example = T.unlines + [ "1=-0-2" + , "12111" + , "2=0=" + , "21" + , "2=01" + , "111" + , "20012" + , "112" + , "1=-1=" + , "1-12" + , "12" + , "1=" + , "122" + ] + +spec :: Spec +spec = do + describe "part 1" $ do + it "examples" $ do + day25 example `shouldBe` "2=-1=0"