@@ -36,13 +36,10 @@ import Data.Set qualified as Set
36
36
main :: IO ()
37
37
main =
38
38
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)
40
40
print (sum (map (score 2 ) codes))
41
41
print (sum (map (score 25 ) codes))
42
42
43
- answer :: Int -> String -> Int
44
- answer n str = minimum (map (robotLength n) (doorInputs str))
45
-
46
43
data Pad = Pad (Set Coord ) (Map Char Coord )
47
44
48
45
padFromList :: [(Coord , Char )] -> Pad
@@ -74,12 +71,13 @@ doorPad = padFromList
74
71
, (C 0 0 , ' A' )
75
72
]
76
73
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)
80
78
| 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)
83
81
]
84
82
85
83
robotLength :: Int -> String -> Int
@@ -112,9 +110,9 @@ deltaToKeys :: Coord -> [String]
112
110
deltaToKeys (C y x) =
113
111
[ keys ++ " A"
114
112
| 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) ' <'
119
117
, keys <- rawKeys : [reverse rawKeys | y /= 0 , x /= 0 ]
120
118
]
0 commit comments