From 419bc558a311c89c86320b1adc255cdfb23ad09c Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Thu, 26 Dec 2024 16:45:34 -0600 Subject: [PATCH] cleanup --- solutions/src/2024/21.hs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/solutions/src/2024/21.hs b/solutions/src/2024/21.hs index 99c505b..8de6d70 100644 --- a/solutions/src/2024/21.hs +++ b/solutions/src/2024/21.hs @@ -36,13 +36,10 @@ import Data.Set qualified as Set main :: IO () main = do codes <- getInputLines 2024 21 - let score n x = (read (init x) * answer n x) + let score n x = (read (init x) * doorInputs n x) print (sum (map (score 2) codes)) print (sum (map (score 25) codes)) -answer :: Int -> String -> Int -answer n str = minimum (map (robotLength n) (doorInputs str)) - data Pad = Pad (Set Coord) (Map Char Coord) padFromList :: [(Coord, Char)] -> Pad @@ -74,12 +71,13 @@ doorPad = padFromList , (C 0 0 , 'A') ] -doorInputs :: String -> [String] -doorInputs str = - [ keys +doorInputs :: Int -> String -> Int +doorInputs n str = + minimum + [ sum (map (robotLength n) keys) | let deltas = padDeltas doorPad str - , keys <- concat <$> traverse deltaToKeys deltas - , validate doorPad keys + , keys <- traverse deltaToKeys deltas + , validate doorPad (concat keys) ] robotLength :: Int -> String -> Int @@ -112,9 +110,9 @@ deltaToKeys :: Coord -> [String] deltaToKeys (C y x) = [ keys ++ "A" | let rawKeys = - (if y < 0 then (replicate (-y) '^' ++) else id) $ - (if x > 0 then (replicate x '>' ++) else id) $ - (if y > 0 then (replicate y 'v' ++) else id) $ - (if x < 0 then replicate (-x) '<' else "") + replicate (-y) '^' ++ + replicate x '>' ++ + replicate y 'v' ++ + replicate (-x) '<' , keys <- rawKeys : [reverse rawKeys | y /= 0, x /= 0] ]