-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23.swift
156 lines (117 loc) · 2.99 KB
/
Day23.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
import Foundation
import Tools
final class Day23Solver: DaySolver {
let dayNumber: Int = 23
struct Input {
let program: [Int]
}
private final class Computer {
let address: Int
var intcode: IntcodeProcessor
var inputBuffer: [Int] = []
var ouputBuffer: [Int] = []
var inputBufferFinished: Bool = false
init(address: Int, intcode: IntcodeProcessor) {
self.address = address
self.intcode = intcode
inputBuffer.append(address)
inputBuffer.append(-1)
}
func pushInputValue(_ value: Int) {
inputBuffer.append(value)
inputBufferFinished = false
}
func nextInputValue() -> Int? {
if let value = inputBuffer.first {
inputBuffer.removeFirst()
return value
} else {
inputBufferFinished = true
return -1
}
}
}
func solvePart1(withInput input: Input) -> Int {
var computers: [Computer] = []
for address in 0 ..< 50 {
computers.append(Computer(address: address, intcode: IntcodeProcessor(program: input.program)))
}
while true {
for i in 0 ..< 50 {
let computer = computers[i]
// make sure the buffer sends once the input finished signal
computer.pushInputValue(-1)
var outputBuffer: [Int] = []
while true {
if let output = computer.intcode.continueProgramTillOutputOrInput(input: &computer.inputBuffer) {
outputBuffer.append(output)
if outputBuffer.count == 3 {
let id = outputBuffer[0]
let x = outputBuffer[1]
let y = outputBuffer[2]
if id == 255 {
return y
}
computers[id].pushInputValue(x)
computers[id].pushInputValue(y)
outputBuffer.removeAll()
}
} else {
break
}
}
}
}
}
func solvePart2(withInput input: Input) -> Int {
var computers: [Computer] = []
for address in 0 ..< 50 {
computers.append(Computer(address: address, intcode: IntcodeProcessor(program: input.program)))
}
var natX = 0
var natY = 0
var lastSentY = 0
while true {
var isIdle = true
for i in 0 ..< 50 {
let computer = computers[i]
// make sure the buffer sends once the input finished signal
computer.pushInputValue(-1)
var outputBuffer: [Int] = []
while true {
if let output = computer.intcode.continueProgramTillOutputOrInput(input: &computer.inputBuffer) {
outputBuffer.append(output)
if outputBuffer.count == 3 {
let id = outputBuffer[0]
let x = outputBuffer[1]
let y = outputBuffer[2]
if id == 255 {
natX = x
natY = y
continue
} else {
isIdle = false
}
computers[id].pushInputValue(x)
computers[id].pushInputValue(y)
outputBuffer.removeAll()
}
} else {
break
}
}
}
if isIdle {
if lastSentY == natY {
return natY
}
lastSentY = natY
computers[0].pushInputValue(natX)
computers[0].pushInputValue(natY)
}
}
}
func parseInput(rawString: String) -> Input {
return .init(program: rawString.parseCommaSeparatedInts())
}
}