Skip to content

Commit 419bc55

Browse files
committed
cleanup
1 parent 5888242 commit 419bc55

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

solutions/src/2024/21.hs

+11-13
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@ import Data.Set qualified as Set
3636
main :: IO ()
3737
main =
3838
do codes <- getInputLines 2024 21
39-
let score n x = (read (init x) * answer n x)
39+
let score n x = (read (init x) * doorInputs n x)
4040
print (sum (map (score 2) codes))
4141
print (sum (map (score 25) codes))
4242

43-
answer :: Int -> String -> Int
44-
answer n str = minimum (map (robotLength n) (doorInputs str))
45-
4643
data Pad = Pad (Set Coord) (Map Char Coord)
4744

4845
padFromList :: [(Coord, Char)] -> Pad
@@ -74,12 +71,13 @@ doorPad = padFromList
7471
, (C 0 0 , 'A')
7572
]
7673

77-
doorInputs :: String -> [String]
78-
doorInputs str =
79-
[ keys
74+
doorInputs :: Int -> String -> Int
75+
doorInputs n str =
76+
minimum
77+
[ sum (map (robotLength n) keys)
8078
| let deltas = padDeltas doorPad str
81-
, keys <- concat <$> traverse deltaToKeys deltas
82-
, validate doorPad keys
79+
, keys <- traverse deltaToKeys deltas
80+
, validate doorPad (concat keys)
8381
]
8482

8583
robotLength :: Int -> String -> Int
@@ -112,9 +110,9 @@ deltaToKeys :: Coord -> [String]
112110
deltaToKeys (C y x) =
113111
[ keys ++ "A"
114112
| let rawKeys =
115-
(if y < 0 then (replicate (-y) '^' ++) else id) $
116-
(if x > 0 then (replicate x '>' ++) else id) $
117-
(if y > 0 then (replicate y 'v' ++) else id) $
118-
(if x < 0 then replicate (-x) '<' else "")
113+
replicate (-y) '^' ++
114+
replicate x '>' ++
115+
replicate y 'v' ++
116+
replicate (-x) '<'
119117
, keys <- rawKeys : [reverse rawKeys | y /= 0, x /= 0]
120118
]

0 commit comments

Comments
 (0)