diff --git a/aoc.cabal b/aoc.cabal index 806cec6..97382bf 100644 --- a/aoc.cabal +++ b/aoc.cabal @@ -11,7 +11,10 @@ executable aoc default-language: GHC2024 default-extensions: BlockArguments + DuplicateRecordFields MultiWayIf + NoFieldSelectors + OverloadedRecordDot ViewPatterns ghc-options: -Wall diff --git a/app/Main.hs b/app/Main.hs index 8bcec71..18351bb 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,39 +2,58 @@ module Main (main) where import Control.Monad.State import Data.Bifunctor -import Data.Traversable import Text.Read (readMaybe) main :: IO () main = do - Just input <- - traverse - ( \case - 'L' : (readMaybe -> Just i) -> Just (L, Inc i) - 'R' : (readMaybe -> Just i) -> Just (R, Inc i) - _ -> Nothing - ) - . lines - <$> readFile "inputs/examples/1" - print - . sum - . flip evalState 50 - $ for input \(d, i) -> state \p -> - let (_, p') = step i d p - in (Count if p' == 0 then 1 else 0, p') - print - . sum - . flip evalState 50 - $ for input \(d, i) -> state \p -> - let (c, p') = step i d p - c' = case d of - R -> abs c - L -> - if - | p == 0 -> abs c - 1 - | p' == 0 -> abs c + 1 - | otherwise -> abs c - in (c', p') + runPuzzle puzzle1 + +runPuzzle :: Puzzle a -> IO () +runPuzzle p = do + Just input <- p.parse <$> readFile ("inputs/examples/" <> show p.number) + putStrLn $ p.part1 input + putStrLn $ p.part2 input +data Puzzle input = Puzzle + { number :: Word + , parse :: String -> Maybe input + , part1 :: input -> String + , part2 :: input -> String + } + +puzzle1 :: Puzzle [(Direction, Inc)] +puzzle1 = + Puzzle + { number = 1 + , parse = + traverse + ( \case + 'L' : (readMaybe -> Just i) -> Just (L, Inc i) + 'R' : (readMaybe -> Just i) -> Just (R, Inc i) + _ -> Nothing + ) + . lines + , part1 = + show + . sum + . flip evalState 50 + . traverse \(d, i) -> state \p -> + let (_, p') = step i d p + in (Count if p' == 0 then 1 else 0, p') + , part2 = + show + . sum + . flip evalState 50 + . traverse \(d, i) -> state \p -> + let (c, p') = step i d p + c' = case d of + R -> abs c + L -> + if + | p == 0 -> abs c - 1 + | p' == 0 -> abs c + 1 + | otherwise -> abs c + in (c', p') + } data Direction = L | R deriving (Eq, Ord, Show)