diff --git a/2023/haskell/aoc2023.cabal b/2023/haskell/aoc2023.cabal index 740bb9a..d1a8c9a 100644 --- a/2023/haskell/aoc2023.cabal +++ b/2023/haskell/aoc2023.cabal @@ -28,6 +28,7 @@ library Day08 Day09 Day10 + Day15 other-modules: Misc diff --git a/2023/haskell/app/Main.hs b/2023/haskell/app/Main.hs index 5111c35..bd2e3ed 100644 --- a/2023/haskell/app/Main.hs +++ b/2023/haskell/app/Main.hs @@ -11,7 +11,8 @@ import Utils (readInput) --import qualified Day07 --import qualified Day08 --import qualified Day09 -import qualified Day10 +--import qualified Day10 +import qualified Day15 run :: (Show b, Show c) @@ -40,4 +41,5 @@ main = do --run 07 Day07.parse Day07.solveA Day07.solveB --run 08 Day08.parse Day08.solveA Day08.solveB --run 09 Day09.parse Day09.solveA Day09.solveB - run 10 Day10.parse Day10.solveA Day10.solveB + --run 10 Day10.parse Day10.solveA Day10.solveB + run 15 Day15.parse Day15.solveA Day15.solveA diff --git a/2023/haskell/solutions/Day15.hs b/2023/haskell/solutions/Day15.hs new file mode 100644 index 0000000..c4c1eb5 --- /dev/null +++ b/2023/haskell/solutions/Day15.hs @@ -0,0 +1,29 @@ +{-# LANGUAGE OverloadedStrings #-} +module Day15 (parse, solveA) where + +import Data.Text (Text) +import qualified Data.Text as T +import Data.Char (ord, isSpace) + + +input :: Text +input = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7" + + +parse :: Text -> Either String [Text] +parse = Right . T.splitOn "," . T.filter (not . isSpace) + +hash :: Text -> Int +hash = foldl step 0 . T.unpack + where + step :: Int -> Char -> Int + step curr = (`rem` 256) . (17 *) . (curr +) . ord + +solveA :: [Text] -> Int +solveA = sum . map hash + + + +-- >>> solveA <$> parse input +-- Right 1320 + diff --git a/2023/haskell/tests/Main.hs b/2023/haskell/tests/Main.hs index 28e10bf..695f253 100644 --- a/2023/haskell/tests/Main.hs +++ b/2023/haskell/tests/Main.hs @@ -13,6 +13,7 @@ import qualified Day07 import qualified Day08 import qualified Day09 import qualified Day10 +import qualified Day15 day01 :: Test @@ -275,6 +276,18 @@ day10 = \L.L7LFJ|||||FJL7||LJ\n\ \L7JLJL-JLJLJL--JLJ.L\n" +day15 :: Test +day15 = + let parsedA1 = Day15.parse inputA1 + parsedA2 = Day15.parse inputA2 + in TestList + [ TestCase $ assertEqual "A" (Right 52) (Day15.solveA <$> parsedA1) + , TestCase $ assertEqual "A" (Right 1320) (Day15.solveA <$> parsedA2) + ] + where + inputA1 = "HASH" + inputA2 = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7" + tests :: Test tests = TestList [ TestLabel "01" day01 @@ -287,6 +300,7 @@ tests = TestList , TestLabel "08" day08 , TestLabel "09" day09 , TestLabel "10" day10 + , TestLabel "15" day15 ] main :: IO ()