-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12_part01.fs
68 lines (60 loc) · 2.24 KB
/
day12_part01.fs
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
module day12_part01
open AdventOfCode_2016.Modules
type operation =
| Copy of value: int * registry: string
| CopyReg of fromreg: string * toreg: string
| Inc of registry: string
| Dec of registry: string
| Jump of value: int * steps: int
| JumpReg of checker: string * steps: int
let parseContent (lines: string array) =
let buildop (line: string) =
let parts = line.Split(" ")
match parts[0] with
| "cpy" ->
match System.Int32.TryParse parts[1] with
| true, v ->
Copy(v, parts[2])
| false, v ->
CopyReg(parts[1], parts[2])
| "inc" -> Inc(parts[1])
| "dec" -> Dec(parts[1])
| "jnz" ->
match System.Int32.TryParse parts[1] with
| true, v ->
Jump(v, (int)parts[2])
| false, v ->
JumpReg(parts[1], (int)parts[2])
| _ -> failwith "op error!"
lines |> Array.map buildop
let performOp (op: operation) (values: Map<string, int>) (pointer: int)=
match op with
| Copy(value, registry) ->
(pointer + 1, Map.add registry value values)
| CopyReg(fromreg, toreg) ->
(pointer + 1, Map.add toreg values[fromreg] values)
| Inc(registry) -> (pointer + 1, Map.add registry (values[registry] + 1) values)
| Dec(registry) -> (pointer + 1, Map.add registry (values[registry] - 1) values)
| Jump(checker, steps) ->
if checker <> 0 then
(pointer + steps, values)
else
(pointer + 1, values)
| JumpReg(checker, steps) ->
if values[checker] <> 0 then
(pointer + steps, values)
else
(pointer + 1, values)
let rec runOp (ops: operation array) (pointer: int) (values: Map<string, int>)=
match ops.Length <= pointer with
| true -> values
| false ->
let (newpointer, newvalues) = performOp ops[pointer] values pointer
runOp ops newpointer newvalues
let execute =
let path = "day12/day12_input.txt"
let content = LocalHelper.GetLinesFromFile path
let operations = parseContent content
let registries = Map[("a", 0); ("b", 0); ("c", 0); ("d", 0)]
let result = runOp operations 0 registries
result["a"]