Skip to content

Commit

Permalink
Day 25: Full of Hot Air
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 25, 2022
1 parent 339d296 commit c4a55f9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)|
3 changes: 3 additions & 0 deletions hs/aoc2022.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ data-files:
, day22.txt
, day23.txt
, day24.txt
, day25.txt

extra-source-files:
README.md
Expand Down Expand Up @@ -66,6 +67,7 @@ library
, Day22
, Day23
, Day24
, Day25
build-depends:
array ^>=0.5.4.0
, base ^>=4.16.0.0
Expand Down Expand Up @@ -132,6 +134,7 @@ test-suite aoc2022-test
, Day22Spec
, Day23Spec
, Day24Spec
, Day25Spec
hs-source-dirs: test
default-language: GHC2021
build-tool-depends:
Expand Down
2 changes: 2 additions & 0 deletions hs/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
4 changes: 4 additions & 0 deletions hs/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
]
]
20 changes: 20 additions & 0 deletions hs/src/Day25.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{-|
Module: Day25
Description: <https://adventofcode.com/2022/day/25 Day 25: Full of Hot Air>
-}
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)
30 changes: 30 additions & 0 deletions hs/test/Day25Spec.hs
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit c4a55f9

Please sign in to comment.