-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.elm
350 lines (276 loc) · 9.04 KB
/
day12.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
module Main exposing (..)
import Array
import Regex
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
, registers : Array.Array Int
, pc : Int
}
problemDay : Int
problemDay =
12
init : ( Model, Cmd msg )
init =
( { problemDay = problemDay
, input = Array.get (problemDay - 1) problemInputs |> Maybe.withDefault ""
, solutionPart1 = ""
, solutionPart2 = ""
, registers = Array.fromList [ 0, 0, 0, 0 ]
, pc = 0
}
, Cmd.none
)
regNameToIndex : String -> Int
regNameToIndex string =
case string of
"a" ->
0
"b" ->
1
"c" ->
2
"d" ->
3
_ ->
Debug.crash ("Invalid register: " ++ string)
getInstructionParamsRegInt : String -> ( Int, Int )
getInstructionParamsRegInt string =
case Regex.find (Regex.AtMost 1) (Regex.regex "\\w+ ([a-z]) (-?\\d+)") string of
m :: [] ->
case m.submatches of
reg :: int :: [] ->
let
regValue =
reg |> Maybe.withDefault "" |> regNameToIndex
intValue =
int |> Maybe.withDefault "" |> String.toInt |> Result.withDefault 0
in
( regValue, intValue )
_ ->
Debug.crash "Invalid RegInt params"
_ ->
Debug.crash ("Invalid RegInt params: " ++ string)
getInstructionParamsIntReg : String -> ( Int, Int )
getInstructionParamsIntReg string =
case Regex.find (Regex.AtMost 1) (Regex.regex "\\w+ (-?\\d+) ([a-z])") string of
m :: [] ->
case m.submatches of
int :: reg :: [] ->
let
intValue =
int |> Maybe.withDefault "" |> String.toInt |> Result.withDefault 0
regValue =
reg |> Maybe.withDefault "" |> regNameToIndex
in
( intValue, regValue )
_ ->
Debug.crash ("Invalid IntReg params: " ++ string)
_ ->
Debug.crash ("Invalid IntReg params: " ++ string)
getInstructionParamsIntInt : String -> ( Int, Int )
getInstructionParamsIntInt string =
case Regex.find (Regex.AtMost 1) (Regex.regex "\\w+ (-?\\d+) (-?\\d+)") string of
m :: [] ->
case m.submatches of
int1 :: int2 :: [] ->
let
intValue =
int1 |> Maybe.withDefault "" |> String.toInt |> Result.withDefault 0
intValue2 =
int2 |> Maybe.withDefault "" |> String.toInt |> Result.withDefault 0
in
( intValue, intValue2 )
_ ->
Debug.crash ("Invalid IntInt params: " ++ string)
_ ->
Debug.crash ("Invalid IntInt params: " ++ string)
getInstructionParamsRegReg : String -> ( Int, Int )
getInstructionParamsRegReg string =
case Regex.find (Regex.AtMost 1) (Regex.regex "\\w+ ([a-z]) ([a-z])") string of
m :: [] ->
case m.submatches of
i1 :: i2 :: [] ->
let
regValue1 =
i1 |> Maybe.withDefault "" |> regNameToIndex
regValue2 =
i2 |> Maybe.withDefault "" |> regNameToIndex
in
( regValue1, regValue2 )
_ ->
Debug.crash "Invalid RegReg params"
_ ->
Debug.crash ("Invalid RegReg params: " ++ string)
getInstructionParamReg : String -> Int
getInstructionParamReg string =
case Regex.find (Regex.AtMost 1) (Regex.regex "\\w+ ([a-z])") string of
m :: [] ->
case m.submatches of
i :: [] ->
i |> Maybe.withDefault "" |> regNameToIndex
_ ->
Debug.crash "Invalid Reg params"
_ ->
Debug.crash ("Invalid Reg param: " ++ string)
type Instruction
= COPY ( Int, Int )
| COPYREG ( Int, Int )
| INC Int
| DEC Int
| JNZ ( Int, Int )
| JNZREG ( Int, Int )
parseInstruction : String -> Instruction
parseInstruction string =
if String.contains "cpy" string then
if (Regex.contains (Regex.regex "cpy [a-z] [a-z]") string) then
COPYREG (getInstructionParamsRegReg string)
else
COPY (getInstructionParamsIntReg string)
else if String.contains "inc" string then
INC (getInstructionParamReg string)
else if String.contains "dec" string then
DEC (getInstructionParamReg string)
else if String.contains "jnz" string then
if (Regex.contains (Regex.regex "jnz [a-z] (-?\\d+)") string) then
JNZREG (getInstructionParamsRegInt string)
else
JNZ (getInstructionParamsIntInt string)
else
Debug.crash "Unknown instruction"
setRegister : Int -> Int -> Model -> Model
setRegister register value model =
let
registers =
Array.set register value model.registers
in
{ model | registers = registers }
getRegister : Int -> Model -> Int
getRegister register model =
case Array.get register model.registers of
Nothing ->
Debug.crash "Unknown register"
Just value ->
value
incPC : Model -> Model
incPC model =
{ model | pc = model.pc + 1 }
executeProgram : Array.Array Instruction -> Model -> Model
executeProgram program model =
case Array.get model.pc program of
Nothing ->
model
Just i ->
let
newModel =
executeInstruction i model
in
executeProgram program newModel
executeInstruction : Instruction -> Model -> Model
executeInstruction instruction model =
case instruction of
COPY ( value, reg ) ->
model
|> setRegister reg value
|> incPC
COPYREG ( origin, dest ) ->
let
regValue =
getRegister origin model
in
model
|> setRegister dest regValue
|> incPC
INC reg ->
let
regValue =
getRegister reg model
in
model
|> setRegister reg (regValue + 1)
|> incPC
DEC reg ->
let
regValue =
getRegister reg model
in
model
|> setRegister reg (regValue - 1)
|> incPC
JNZ ( value, jump ) ->
if value == 0 then
model |> incPC
else
{ model | pc = model.pc + jump }
JNZREG ( reg, jump ) ->
let
value =
getRegister reg model
in
if value == 0 then
model |> incPC
else
{ model | pc = model.pc + jump }
solverPart1 : Model -> String
solverPart1 model =
let
program =
model.input
|> String.split ("\n")
|> List.map String.trim
|> List.map parseInstruction
|> Array.fromList
in
(executeProgram program model).registers
|> Array.get 0
|> toString
solverPart2 : Model -> String
solverPart2 model =
let
program =
model.input
|> String.split ("\n")
|> List.map String.trim
|> List.map parseInstruction
|> Array.fromList
initializedModel =
{ model | registers = Array.fromList [ 0, 0, 1, 0 ] }
in
(executeProgram program initializedModel).registers
|> Array.get 0
|> toString
view : Model -> Html.Html Msg
view model =
div []
[ renderProblem model ( Solve, Solve2 )
, div [] [ text (toString model.registers) ]
]