-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.elm
386 lines (308 loc) · 10.3 KB
/
day10.elm
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
module Main exposing (..)
import Array
import Regex
import Dict
import Html exposing (program, text, textarea, div, button, pre)
import View exposing (renderProblem)
import Inputs exposing (problemInputs)
main : Program Never Model Msg
main =
program { init = init, view = view, update = update, subscriptions = subscriptions }
subscriptions : Model -> Sub msg
subscriptions model =
Sub.none
type Msg
= Solve
| Solve2
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
Solve ->
let
solution =
solverPart1 model
in
( { model | solutionPart1 = solution }, Cmd.none )
Solve2 ->
let
solution =
solverPart2 model
in
( { model | solutionPart2 = solution }, Cmd.none )
type alias Model =
{ problemDay : Int
, input : String
, solutionPart1 : String
, solutionPart2 : String
, bots : Dict.Dict Int Robot
, output : Dict.Dict Int Int
, foundBot : Int
}
problemDay : Int
problemDay =
10
init : ( Model, Cmd msg )
init =
( { problemDay =
problemDay
, input = Array.get (problemDay - 1) problemInputs |> Maybe.withDefault ""
, solutionPart1 = ""
, solutionPart2 = ""
, bots = Dict.empty
, output = Dict.empty
, foundBot = 0
}
, Cmd.none
)
type alias Robot =
{ number : Int
, payload : ( Maybe Int, Maybe Int )
, ordersHigh : ( OutputType, Int )
, ordersLow : ( OutputType, Int )
}
type OutputType
= Bot
| Output
toOutput : String -> OutputType
toOutput output =
if output == "bot" then
Bot
else
Output
parseValue : String -> Instruction
parseValue string =
let
match =
Regex.find (Regex.AtMost 1) (Regex.regex "value (\\d+) goes to bot (\\d+)") string
in
case match of
m :: [] ->
m.submatches
|> List.map (Maybe.withDefault "")
|> List.map (String.toInt)
|> List.map (Result.withDefault 0)
|> (\l ->
case l of
value :: bot :: [] ->
Value ( value, bot )
_ ->
Debug.crash "Invalid input"
)
_ ->
Debug.crash "Invalid input"
parseMovement : String -> Instruction
parseMovement string =
let
match =
Regex.find (Regex.AtMost 1) (Regex.regex "bot (\\d+) gives low to (bot|output) (\\d+) and high to (bot|output) (\\d+)") string
in
case match of
m :: [] ->
m.submatches
|> List.map (Maybe.withDefault "")
|> (\l ->
case l of
bot :: lowTo :: lowToNum :: highTo :: highToNum :: [] ->
let
botNumber =
bot |> String.toInt |> Result.withDefault 0
lowToType =
toOutput lowTo
lowToNumber =
lowToNum |> String.toInt |> Result.withDefault 0
highToType =
toOutput highTo
highToNumber =
highToNum |> String.toInt |> Result.withDefault 0
in
Movement ( botNumber, lowToType, lowToNumber, highToType, highToNumber )
_ ->
Debug.crash ("Invalid input" ++ toString l)
)
_ ->
Debug.crash ("Invalid input" ++ string)
type Instruction
= Value ( Int, Int )
| Movement ( Int, OutputType, Int, OutputType, Int )
updateBotPayload : Int -> Maybe Robot -> Int -> ( Int, Robot )
updateBotPayload botNumber maybeRobot value =
case maybeRobot of
Nothing ->
( -1
, { number = botNumber
, payload = ( Just value, Nothing )
, ordersLow = ( Bot, 0 )
, ordersHigh = ( Bot, 0 )
}
)
Just robot ->
case robot.payload of
( Nothing, Nothing ) ->
( -1, { robot | payload = ( Just value, Nothing ) } )
( Just v, Nothing ) ->
let
foundBot =
if v == 61 && value == 17 then
robot.number
else
-1
in
if v > value then
( foundBot, { robot | payload = ( Just v, Just value ) } )
else
( foundBot, { robot | payload = ( Just value, Just v ) } )
_ ->
Debug.crash "Overloading bot!!"
updateBotOrders botNumber maybeRobot ( _, lowTo, lowToNumber, highTo, highToNumber ) =
let
ordersLow =
( lowTo, lowToNumber )
ordersHigh =
( highTo, highToNumber )
in
case maybeRobot of
Nothing ->
{ number = botNumber
, payload = ( Nothing, Nothing )
, ordersLow = ordersLow
, ordersHigh = ordersHigh
}
Just robot ->
{ robot | ordersLow = ordersLow, ordersHigh = ordersHigh }
updateFoundBot : Int -> Model -> Model
updateFoundBot foundBot model =
if foundBot >= 0 then
{ model | foundBot = foundBot }
else
model
executeInstruction : Model -> Instruction -> Model
executeInstruction model instruction =
case instruction of
Value ( value, bot ) ->
let
existingBot =
Dict.get bot model.bots
( foundBot, updatedBot ) =
updateBotPayload bot existingBot value
in
{ model | bots = Dict.insert bot updatedBot model.bots }
|> updateFoundBot foundBot
Movement orders ->
let
( bot, _, _, _, _ ) =
orders
existingBot =
Dict.get bot model.bots
in
{ model | bots = Dict.insert bot (updateBotOrders bot existingBot orders) model.bots }
executeHightOrder : Robot -> Model -> Model
executeHightOrder robot model =
let
( high, _ ) =
robot.payload
hValue =
high |> Maybe.withDefault 0
in
case robot.ordersHigh of
( Bot, bot ) ->
let
existingBot =
Dict.get bot model.bots
( foundBot, updatedBot ) =
updateBotPayload bot existingBot hValue
in
{ model | bots = Dict.insert bot updatedBot model.bots }
|> updateFoundBot foundBot
( Output, v ) ->
{ model | output = Dict.insert v hValue model.output }
executeLowOrder : Robot -> Model -> Model
executeLowOrder robot model =
let
( _, low ) =
robot.payload
lValue =
low |> Maybe.withDefault 0
in
case robot.ordersLow of
( Bot, bot ) ->
let
existingBot =
Dict.get bot model.bots
( foundBot, updatedBot ) =
updateBotPayload bot existingBot lValue
in
{ model | bots = Dict.insert bot updatedBot model.bots }
|> updateFoundBot foundBot
( Output, v ) ->
{ model | output = Dict.insert v lValue model.output }
emptyPayload : Robot -> Robot
emptyPayload robot =
{ robot | payload = ( Nothing, Nothing ) }
parseInput : String -> Instruction
parseInput string =
if String.contains "value" string then
parseValue string
else
parseMovement string
runRobot : Int -> Robot -> Model -> Model
runRobot robotNumber robot model =
let
executedInstructions =
model
|> executeLowOrder robot
|> executeHightOrder robot
in
{ executedInstructions | bots = Dict.insert robotNumber (emptyPayload robot) executedInstructions.bots }
runSimulation : Model -> Model
runSimulation model =
let
nextRobot =
model.bots
|> Dict.toList
|> List.filter
(\( botNumber, robot ) ->
case robot.payload of
( Just h, Just l ) ->
True
_ ->
False
)
|> List.head
in
case nextRobot of
Nothing ->
model
Just ( k, robot ) ->
runSimulation (runRobot k robot model)
solverPart1 : Model -> String
solverPart1 model =
let
settedUpRobots =
model.input
|> String.split ("\n")
|> List.map String.trim
|> List.map parseInput
|> (List.foldl (\i m -> executeInstruction m i) model)
in
(runSimulation settedUpRobots).foundBot
|> toString
solverPart2 : Model -> String
solverPart2 model =
let
settedUpRobots =
model.input
|> String.split ("\n")
|> List.map String.trim
|> List.map parseInput
|> (List.foldl (\i m -> executeInstruction m i) model)
in
(runSimulation settedUpRobots).output
|> Dict.filter (\k _ -> k == 0 || k == 1 || k == 2)
|> Dict.values
|> List.product
|> toString
view : Model -> Html.Html Msg
view model =
div []
[ renderProblem model ( Solve, Solve2 )
]