-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.swift
224 lines (188 loc) · 5.36 KB
/
Day18.swift
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
import Foundation
import Tools
final class Day18Solver: DaySolver {
let dayNumber: Int = 18
struct Input {
let instructions: [Instruction]
}
enum Operand {
case value(Int)
case register(id: String)
var registerID: String? {
switch self {
case .value:
nil
case .register(let id):
id
}
}
}
enum Instruction {
case snd(a: Operand)
case set(a: Operand, b: Operand)
case add(a: Operand, b: Operand)
case mul(a: Operand, b: Operand)
case mod(a: Operand, b: Operand)
case rcv(a: Operand)
case jgz(a: Operand, b: Operand)
}
private struct CPUPart1 {
let instructions: [Instruction]
var registers: [String: Int] = [:]
var ip: Int = 0
var lastFrequency = 0
var receivedFrequency = 0
private func getValue(of operand: Operand) -> Int {
switch operand {
case .value(let value):
value
case .register(let id):
registers[id, default: 0]
}
}
mutating func executeNextInstruction() -> Bool {
guard let instruction = instructions[safe: ip] else {
return true
}
switch instruction {
case .set(let a, let b):
registers[a.registerID!] = getValue(of: b)
case .add(let a, let b):
registers[a.registerID!] = getValue(of: a) + getValue(of: b)
case .mul(let a, let b):
registers[a.registerID!] = getValue(of: a) * getValue(of: b)
case .mod(let a, let b):
registers[a.registerID!] = getValue(of: a) % getValue(of: b)
case .jgz(let a, let b):
ip += getValue(of: a) > 0 ? (getValue(of: b) - 1) : 0
case .snd(let a):
lastFrequency = getValue(of: a)
case .rcv(let a):
receivedFrequency = getValue(of: a) != 0 ? lastFrequency : receivedFrequency
}
ip += 1
return false
}
}
private struct CPUPart2 {
let instructions: [Instruction]
var registers: [String: Int] = [:]
var ip: Int = 0
var inputQueue: [Int] = []
var sendCount = 0
private func getValue(of operand: Operand) -> Int {
switch operand {
case .value(let value):
value
case .register(let id):
registers[id, default: 0]
}
}
enum State {
case ok
case waitingForInput
case output(value: Int)
case endOfProgram
}
mutating func executeNextInstruction() -> State {
guard let instruction = instructions[safe: ip] else {
return .endOfProgram
}
switch instruction {
case .set(let a, let b):
registers[a.registerID!] = getValue(of: b)
case .add(let a, let b):
registers[a.registerID!] = getValue(of: a) + getValue(of: b)
case .mul(let a, let b):
registers[a.registerID!] = getValue(of: a) * getValue(of: b)
case .mod(let a, let b):
registers[a.registerID!] = getValue(of: a) % getValue(of: b)
case .jgz(let a, let b):
ip += getValue(of: a) > 0 ? (getValue(of: b) - 1) : 0
case .snd(let a):
ip += 1
sendCount += 1
return .output(value: getValue(of: a))
case .rcv(let a):
if inputQueue.isNotEmpty {
registers[a.registerID!] = inputQueue.removeFirst()
} else {
// keep the IP at same spot so we just keep on waiting
return .waitingForInput
}
}
ip += 1
return .ok
}
}
func solvePart1(withInput input: Input) -> Int {
var cpu = CPUPart1(instructions: input.instructions)
while cpu.executeNextInstruction() == false {
if cpu.receivedFrequency != 0 {
return cpu.receivedFrequency
}
}
fatalError()
}
func solvePart2(withInput input: Input) -> Int {
var cpu1 = CPUPart2(instructions: input.instructions)
var cpu2 = CPUPart2(instructions: input.instructions)
cpu1.registers["p"] = 0
cpu2.registers["p"] = 1
executionLoop: while true {
var waitingCount = 0
switch cpu1.executeNextInstruction() {
case .ok:
break
case .output(let value):
cpu2.inputQueue.append(value)
case .waitingForInput:
waitingCount += 1
case .endOfProgram:
break executionLoop
}
switch cpu2.executeNextInstruction() {
case .ok:
break
case .output(let value):
cpu1.inputQueue.append(value)
case .waitingForInput:
waitingCount += 1
case .endOfProgram:
break executionLoop
}
if waitingCount == 2 {
break
}
}
return cpu2.sendCount
}
func parseInput(rawString: String) -> Input {
func parseOperand(_ text: String) -> Operand {
if let value = Int(text) {
.value(value)
} else {
.register(id: text)
}
}
return .init(instructions: rawString.allLines().map { part in
if let values = part.getCapturedValues(pattern: #"set ([a-z]*) (-?[0-9a-z]*)"#) {
.set(a: parseOperand(values[0]), b: parseOperand(values[1]))
} else if let values = part.getCapturedValues(pattern: #"add ([a-z]*) (-?[0-9a-z]*)"#) {
.add(a: parseOperand(values[0]), b: parseOperand(values[1]))
} else if let values = part.getCapturedValues(pattern: #"mul ([a-z]*) (-?[0-9a-z]*)"#) {
.mul(a: parseOperand(values[0]), b: parseOperand(values[1]))
} else if let values = part.getCapturedValues(pattern: #"mod ([a-z]*) (-?[0-9a-z]*)"#) {
.mod(a: parseOperand(values[0]), b: parseOperand(values[1]))
} else if let values = part.getCapturedValues(pattern: #"jgz (-?[0-9a-z]*) (-?[0-9a-z]*)"#) {
.jgz(a: parseOperand(values[0]), b: parseOperand(values[1]))
} else if let values = part.getCapturedValues(pattern: #"rcv ([a-z]*)"#) {
.rcv(a: parseOperand(values[0]))
} else if let values = part.getCapturedValues(pattern: #"snd ([a-z]*)"#) {
.snd(a: parseOperand(values[0]))
} else {
fatalError()
}
})
}
}