-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18_part02.fs
184 lines (155 loc) · 6.89 KB
/
day18_part02.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
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
module day18_part02
open AdventOfCode_2017.Modules
open AdventOfCode_Utilities
open System.Collections.Generic
type InstructionKind =
| Snd of register: string
| SndByVal of value: int64
| SetByReg of target: string * from: string
| SetByVal of target:string * value: int64
| AddByReg of target: string * from: string
| AddByVal of target:string * value: int64
| MulByReg of target: string * from: string
| MulByVal of target:string * value: int64
| ModByReg of target: string * from: string
| ModByVal of target:string * value: int64
| Rcv of register: string
| JgzByReg of checker: string * steps: string
| JgzByRegWithVal of checker: string * steps: int64
| JgzByVal of checker:int64 * steps: int64
| JgzByValWithReg of checker:int64 * steps: string
type TheProgram = {
Registers: Dictionary<string, int64>
Pointer: int64
Frequencies: Queue<int64>
}
let parseContent(lines: string array) =
lines
|> Array.map (fun line ->
let parts = line.Split(' ')
let instruction = parts[0]
match instruction with
| "snd" ->
if parts[1] |> Utilities.isNumeric then
SndByVal(int64 parts[1])
else
Snd(parts[1])
| "set" ->
if parts[2] |> Utilities.isNumeric then
SetByVal(parts[1], int64 parts[2])
else
SetByReg(parts[1], parts[2])
| "add" ->
if parts[2] |> Utilities.isNumeric then
AddByVal(parts[1], int64 parts[2])
else
AddByReg(parts[1], parts[2])
| "mul" ->
if parts[2] |> Utilities.isNumeric then
MulByVal(parts[1], int64 parts[2])
else
MulByReg(parts[1], parts[2])
| "mod" ->
if parts[2] |> Utilities.isNumeric then
ModByVal(parts[1], int64 parts[2])
else
ModByReg(parts[1], parts[2])
| "rcv" -> Rcv(parts[1])
| "jgz" ->
match (parts[1] |> Utilities.isNumeric, parts[2] |> Utilities.isNumeric) with
| (true, true) -> JgzByVal(int64 parts[1], int64 parts[2])
| (true, false) -> JgzByValWithReg(int64 parts[1], parts[2])
| (false, true) -> JgzByRegWithVal(parts[1], int64 parts[2])
| (false, false) -> JgzByReg(parts[1], parts[2])
| _ -> failwith "Invalid instruction"
)
let performOp (program: TheProgram) (instructions: InstructionKind array) = // (ins: InstructionKind) (registers: Dictionary<string, int64>) (currentfrecuency: int64) =
let ins = instructions[(int)program.Pointer]
match ins with
| Snd(register) ->
({ program with Pointer = program.Pointer + 1L }, Some(program.Registers[register]))
| SndByVal(value) ->
({ program with Pointer = program.Pointer + 1L }, Some(value))
| SetByReg(target, from) ->
program.Registers[target] <- program.Registers[from]
({ program with Pointer = program.Pointer + 1L }, None)
| SetByVal(target, value) ->
program.Registers[target] <- value
({ program with Pointer = program.Pointer + 1L }, None)
| AddByReg(target, from) ->
program.Registers[target] <- program.Registers[target] + program.Registers[from]
({ program with Pointer = program.Pointer + 1L }, None)
| AddByVal(target, value) ->
program.Registers[target] <- program.Registers[target] + value
({ program with Pointer = program.Pointer + 1L }, None)
| MulByReg(target, from) ->
program.Registers[target] <- program.Registers[target] * program.Registers[from]
({ program with Pointer = program.Pointer + 1L }, None)
| MulByVal(target, value) ->
program.Registers[target] <- program.Registers[target] * value
({ program with Pointer = program.Pointer + 1L }, None)
| ModByReg(target, from) ->
program.Registers[target] <- program.Registers[target] % program.Registers[from]
({ program with Pointer = program.Pointer + 1L }, None)
| ModByVal(target, value) ->
program.Registers[target] <- program.Registers[target] % value
({ program with Pointer = program.Pointer + 1L }, None)
| Rcv(register) ->
if program.Frequencies.Count > 0 then
program.Registers[register] <- program.Frequencies.Dequeue()
({ program with Pointer = program.Pointer + 1L }, None)
else
({ program with Pointer = program.Pointer }, None)
| JgzByReg(checker, steps) ->
if program.Registers[checker] > 0L then
({ program with Pointer = program.Pointer + program.Registers[steps] }, None)
else
({ program with Pointer = program.Pointer + 1L }, None)
| JgzByRegWithVal(checker, steps) ->
if program.Registers[checker] > 0L then
({ program with Pointer = program.Pointer + steps }, None)
else
({ program with Pointer = program.Pointer + 1L }, None)
| JgzByVal(checker, steps) ->
if checker > 0L then
({ program with Pointer = program.Pointer + steps }, None)
else
({ program with Pointer = program.Pointer + 1L }, None)
| JgzByValWithReg(checker, steps) ->
if checker > 0L then
({ program with Pointer = program.Pointer + program.Registers[steps] }, None)
else
({ program with Pointer = program.Pointer + 1L }, None)
let runInstructions(instructions: InstructionKind array) =
let registers0 = new Dictionary<string, int64>()
[|'a'..'z'|]
|> Array.iter (fun c -> registers0.Add(c.ToString(), 0))
let registers1 = new Dictionary<string, int64>()
[|'a'..'z'|]
|> Array.iter (fun c -> registers1.Add(c.ToString(), 0))
registers1["p"] <- 1L
let program0 = { Registers = registers0; Pointer = 0; Frequencies = Queue<int64>() }
let program1 = { Registers = registers1; Pointer = 0; Frequencies = Queue<int64>() }
let mutable currentindex0 = 0L
let mutable previndex0 = -1L
let mutable currentindex1 = 0L
let mutable previndex1 = -1L
let mutable sentBy1 = 0
while currentindex0 <> previndex0 || currentindex1 <> previndex1 do
let (p0, freq) = performOp ({program0 with Pointer = currentindex0 }) instructions
previndex0 <- currentindex0
currentindex0 <- p0.Pointer
if freq.IsSome then
program1.Frequencies.Enqueue(freq.Value)
let (p1, freq) = performOp ({program1 with Pointer = currentindex1}) instructions
previndex1 <- currentindex1
currentindex1 <- p1.Pointer
if freq.IsSome then
sentBy1 <- sentBy1 + 1
program0.Frequencies.Enqueue(freq.Value)
sentBy1
let execute() =
let path = "day18/day18_input.txt"
let content = LocalHelper.GetLinesFromFile path
let instructions = parseContent content
runInstructions instructions