Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 26, 2024
1 parent 5888242 commit 419bc55
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions solutions/src/2024/21.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
]

0 comments on commit 419bc55

Please sign in to comment.