Skip to content

Commit a2b7c98

Browse files
committed
smaller
1 parent 439d1c2 commit a2b7c98

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

solutions/src/2024/21.hs

+7-20
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ module Main (main) where
2525
import Advent (getInputLines)
2626
import Advent.Coord (Coord(..), coordLines)
2727
import Advent.Memo (memo2)
28-
import Data.Map (Map)
28+
import Data.Map (Map, (!))
2929
import Data.Map qualified as Map
30-
import Data.Set (Set)
31-
import Data.Set qualified as Set
3230

3331
-- | >>> :main
3432
-- 177814
@@ -40,21 +38,11 @@ main =
4038
print (sum (map (score 2) codes))
4139
print (sum (map (score 25) codes))
4240

43-
data Pad = Pad (Set Coord) (Map Char Coord)
41+
type Pad = Map Char Coord
4442

4543
-- | Turn a list of lines into a 'Pad'. Spaces are removed.
4644
padFromList :: [String] -> Pad
47-
padFromList strs = Pad (Set.fromList (Map.elems buttons)) buttons
48-
where
49-
buttons = Map.fromList [(c, p) | (p, c) <- coordLines strs, c /= ' ']
50-
51-
-- | Find the coordinate of a button.
52-
padCoord :: Pad -> Char -> Coord
53-
padCoord (Pad _ m) c = m Map.! c
54-
55-
-- | Test if a coordinate is contained within the pad.
56-
inPad :: Pad -> Coord -> Bool
57-
inPad (Pad s _) x = Set.member x s
45+
padFromList strs = Map.fromList [(c, p) | (p, c) <- coordLines strs]
5846

5947
-- | The 4-direction pad used to control a robot
6048
robotPad :: Pad
@@ -73,7 +61,6 @@ shortDoorCode ::
7361
shortDoorCode n =
7462
sum . map (minimum . map (shortRobotCode n)) . route doorPad
7563

76-
7764
-- | The length of the shortest input sequence that enters the given
7865
-- robot directional code via a given number of robot layers.
7966
shortRobotCode ::
@@ -93,9 +80,9 @@ shortRobotCode = memo2 \case
9380
-- >>> route doorPad "029A"
9481
-- [["<A"],["^A"],["^^>A",">^^A"],["vvvA"]]
9582
route :: Pad -> String -> [[String]]
96-
route pad str = zipWith (walk pad) (padCoord pad 'A' : absolutes) absolutes
83+
route pad str = zipWith (walk pad) (pad ! 'A' : absolutes) absolutes
9784
where
98-
absolutes = map (padCoord pad) str
85+
absolutes = map (pad !) str
9986

10087
-- | Find the unique, shortest paths to move from one location to
10188
-- another on a pad.
@@ -107,6 +94,6 @@ walk pad (C y1 x1) (C y2 x2) =
10794
replicate (y2 - y1) 'v' ++
10895
replicate (x2 - x1) '>' ++
10996
replicate (x1 - x2) '<'
110-
, keys <- [ rawKeys | inPad pad (C y2 x1) ]
111-
++ [reverse rawKeys | y1 /= y2, x1 /= x2, inPad pad (C y1 x2)]
97+
, keys <- [ rawKeys | pad ! ' ' /= C y2 x1]
98+
++ [reverse rawKeys | y1 /= y2, x1 /= x2, pad ! ' ' /= C y1 x2]
11299
]

0 commit comments

Comments
 (0)