-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
174 lines (149 loc) · 5.52 KB
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
import AoC.Grid
import Data.Bifunctor (first)
import Data.Foldable
import Data.List.Split (splitOn)
import Data.Maybe (catMaybes, isJust)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Set (Set)
import qualified Data.Set as Set
type N = Int
data Dir = U | D | L | R
deriving Show
type Robot = (Int, Int)
type Input = (MapGrid Char, Robot, [Dir])
parseInstr :: String -> [Dir]
parseInstr = map f . filter (`elem` "^v<>")
where f = \case '^' -> U
'v' -> D
'>' -> R
'<' -> L
parseAll :: String -> Input
parseAll input =
let [grid, instr] = splitOn "\n\n" input
g = parseMapGrid id grid
Just (rpos, _) = find (\(_, v) -> v == '@') $ HashMap.toList g
g' = HashMap.insert rpos '.' g
in (g', rpos, parseInstr instr)
ray :: MapGrid Char -> (Int, Int) -> (Int, Int) -> [((Int, Int), Char)]
ray g (x, y) (dx, dy) =
catMaybes . takeWhile isJust . map sequence $ map (\pos -> (pos, g HashMap.!? pos)) [ (x + i*dx, y + i*dy) | i <- [1..] ]
toDir :: Dir -> (Int, Int)
toDir = \case D -> ( 0, 1)
U -> ( 0, -1)
L -> (-1, 0)
R -> ( 1, 0)
step :: MapGrid Char -> (Int, Int) -> Dir -> (MapGrid Char, (Int, Int))
step g robot instr =
let d = toDir instr
xs = ray g robot d
in case (xs, dropWhile ((== 'O') . snd) xs) of
((robot', '.'):_, _) -> (g, robot')
((robot', 'O'):_, (empty, '.'):_) -> ( HashMap.insert empty 'O' $ HashMap.insert robot' '.' g
, robot'
)
_ -> (g, robot)
gps1 :: MapGrid Char -> Int
gps1 =
sum
. map (\(x, y) -> x + 100 * y)
. HashMap.keys
. HashMap.filter (== 'O')
part1 :: Input -> Int
part1 (g, robot, instr) =
let (final, _) = foldl' (uncurry step) (g, robot) instr
in gps1 final
widen :: Input -> Input
widen (g, (rx, ry), instr) = (g', (2*rx, ry), instr)
where g' = toMapGrid . map (concatMap f) $ fromMapGrid g
f = \case
'#' -> "##"
'O' -> "[]"
'.' -> ".."
o -> error $ "what: " ++ show o
(|+|) :: (Num a, Num b) => (a, b) -> (a, b) -> (a, b)
(x, y) |+| (dx, dy) = (x + dx, y + dy)
shiftBox :: (Int, Int) -> [((Int, Int), Char)] -> MapGrid Char -> MapGrid Char
shiftBox d xs g =
let boxes = takeWhile ((`elem` "[]") . snd) xs
shifted = map (first (|+| d)) boxes
empty = (fst $ head xs, '.')
in HashMap.union (HashMap.fromList (empty:shifted)) g
collectAffected :: MapGrid Char
-> (Int, Int)
-> (Int, Int)
-> Char
-> Maybe [(Int, Int)]
collectAffected g d firstCell firstCellChar = go Set.empty initial
where go :: Set (Int, Int) -> [(Int, Int)] -> Maybe [(Int, Int)]
go seen = \case [] -> Just (toList seen)
(x:xs)
| x `Set.member` seen -> go seen (filter (not . (`Set.member` seen)) xs)
| otherwise ->
let !x' = (fst x + fst d, snd x + snd d) -- |+| d
!seen' = Set.insert x seen
in case g HashMap.!? x' of
Just '#' -> Nothing
Just '.' -> go seen' xs
Just (!c) -> go seen' (x':extend x' c:xs)
Nothing -> error "Cannot happen"
initial = [firstCell, extend firstCell firstCellChar]
extend pos = \case '[' -> pos |+| ( 1, 0)
']' -> pos |+| (-1, 0)
shiftAll :: MapGrid Char -> (Int, Int) -> [(Int, Int)] -> MapGrid Char
shiftAll g d cells =
let shifted = zip (map (|+| d) cells) (map (g HashMap.!) cells) -- TODO: Redundant to read cells twice
empties = map (, '.') cells
in HashMap.unions [ HashMap.fromList shifted
, HashMap.fromList empties
, g
]
step2 :: MapGrid Char -> Robot -> Dir -> (MapGrid Char, Robot)
step2 g robot instr = case instr of
U -> goY
D -> goY
L -> goX
R -> goX
where d = toDir instr
goX = let xs = ray g robot d
in case (xs, dropWhile ((`elem` "[]") . snd) xs) of
((robot', '.'):_, _) -> (g, robot')
((robot', c):_, (_, '.'):_)
| c `elem` "[]" -> (shiftBox d xs g, robot')
_ -> (g, robot)
goY =
let robot' = robot |+| d
in case g HashMap.! robot' of
c | c `elem` "[]"
, Just cells <- collectAffected g d robot' c
-> (shiftAll g d cells, robot')
'.' -> (g, robot')
_ -> (g, robot)
printGrid :: (MapGrid Char, Robot) -> IO ()
printGrid (g, r) =
putStrLn $ ppMapGrid id $ HashMap.insert r '@' g
gps2 :: MapGrid Char -> Int
gps2 =
sum
. map (\(x, y) -> x + 100 * y)
. HashMap.keys
. HashMap.filter (== '[')
part2 :: Input -> Int
part2 input =
let (g, r, i) = widen input
(final, _) = foldl' (uncurry step2) (g, r) i
in gps2 final
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)