mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-26 18:10:05 +00:00
refactor: day 3 to contain the logic inside a Program type
This commit is contained in:
parent
43e288bd69
commit
a9573f2747
1 changed files with 29 additions and 24 deletions
53
src/Day3.hs
53
src/Day3.hs
|
|
@ -4,38 +4,43 @@ import Data.Maybe (mapMaybe)
|
||||||
import Text.Read (readMaybe)
|
import Text.Read (readMaybe)
|
||||||
import Text.Regex.TDFA
|
import Text.Regex.TDFA
|
||||||
|
|
||||||
-- TODO: Refactor to parse operations to a type and then be executed by a "execute" function
|
data ProgramState = Enabled | Disabled
|
||||||
|
|
||||||
|
data Program = Program ProgramState Int
|
||||||
|
|
||||||
|
data Instruction = Do | Dont | Mul Int Int
|
||||||
|
|
||||||
partOne :: String -> Int
|
partOne :: String -> Int
|
||||||
partOne text = sum $ map (uncurry (*)) $ mapMaybe parseOperation matches
|
partOne text = programValue $ foldl runInstruction initProgram $ parseInstructions matches
|
||||||
where
|
where
|
||||||
matches = getAllTextMatches (text =~ "mul\\(([0-9]{1,3}),([0-9]{1,3})\\)")
|
matches = getAllTextMatches (text =~ "mul\\(([0-9]{1,3}),([0-9]{1,3})\\)")
|
||||||
|
|
||||||
partTwo :: String -> Int
|
partTwo :: String -> Int
|
||||||
partTwo text =
|
partTwo text = programValue $ foldl runInstruction initProgram $ parseInstructions matches
|
||||||
sum $
|
|
||||||
map (uncurry (*)) $
|
|
||||||
mapMaybe parseOperation $
|
|
||||||
filterDisabledOperations matches
|
|
||||||
where
|
where
|
||||||
matches = getAllTextMatches (text =~ "do\\(\\)|don't\\(\\)|mul\\([0-9]{1,3},[0-9]{1,3}\\)")
|
matches = getAllTextMatches (text =~ "do\\(\\)|don't\\(\\)|mul\\([0-9]{1,3},[0-9]{1,3}\\)")
|
||||||
|
|
||||||
data ExecutionState = Enabled | Disabled
|
initProgram :: Program
|
||||||
|
initProgram = Program Enabled 0
|
||||||
|
|
||||||
filterDisabledOperations :: [String] -> [String]
|
programValue :: Program -> Int
|
||||||
filterDisabledOperations operations = aux Enabled operations []
|
programValue (Program _ value) = value
|
||||||
|
|
||||||
|
runInstruction :: Program -> Instruction -> Program
|
||||||
|
runInstruction (Program _ value) Do = Program Enabled value
|
||||||
|
runInstruction (Program _ value) Dont = Program Disabled value
|
||||||
|
runInstruction (Program Disabled value) _ = Program Disabled value
|
||||||
|
runInstruction (Program Enabled value) (Mul x y) = Program Enabled (value + (x * y))
|
||||||
|
|
||||||
|
parseInstructions :: [String] -> [Instruction]
|
||||||
|
parseInstructions = mapMaybe parseInstruction
|
||||||
where
|
where
|
||||||
aux _ [] acc = acc
|
parseInstruction "do()" = Just Do
|
||||||
aux _ ("don't()" : xs) acc = aux Disabled xs acc
|
parseInstruction "don't()" = Just Dont
|
||||||
aux _ ("do()" : xs) acc = aux Enabled xs acc
|
parseInstruction x =
|
||||||
aux Enabled (op : xs) acc = aux Enabled xs (op : acc)
|
case getAllTextMatches (x =~ "[0-9]{1,3}") of
|
||||||
aux Disabled (_ : xs) acc = aux Disabled xs acc
|
[left, right] -> do
|
||||||
|
leftOp <- readMaybe left :: Maybe Int
|
||||||
parseOperation :: String -> Maybe (Int, Int)
|
rightOp <- readMaybe right :: Maybe Int
|
||||||
parseOperation x =
|
return (Mul leftOp rightOp)
|
||||||
case getAllTextMatches (x =~ "[0-9]{1,3}") of
|
_ -> Nothing
|
||||||
[left, right] -> do
|
|
||||||
leftOp <- readMaybe left :: Maybe Int
|
|
||||||
rightOp <- readMaybe right :: Maybe Int
|
|
||||||
return (leftOp, rightOp)
|
|
||||||
_ -> Nothing
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue