/
IntcodeComputer.cs
203 lines (151 loc) · 6.52 KB
/
IntcodeComputer.cs
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
using System;
using System.Linq;
namespace Advent
{
public class IntcodeComputer
{
private enum ParameterMode
{
Position,
Immediate,
Relative
}
private readonly long[] _memory;
private readonly long _phase;
private bool _setPhase;
private long _instructionPointer;
private long _output;
private long _relativeBase;
private InputDelegate _onInput;
public bool Halted { get; private set; }
public delegate long InputDelegate();
public IntcodeComputer(string memoryString, long phase, InputDelegate onInput = null)
{
_memory = new long[5000];
memoryString.Split(',').Select(long.Parse).ToArray().CopyTo(_memory, 0);
_phase = phase;
_onInput = onInput;
}
private long GetValue(long parameter, ParameterMode mode)
{
switch (mode)
{
case ParameterMode.Position:
return _memory[parameter];
case ParameterMode.Immediate:
return parameter;
case ParameterMode.Relative:
return _memory[_relativeBase + parameter];
default:
throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
}
}
private void SetValue(long value, long location, ParameterMode mode)
{
switch (mode)
{
case ParameterMode.Position:
_memory[location] = value;
break;
case ParameterMode.Relative:
_memory[_relativeBase + location] = value;
break;
default:
throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
}
}
public long Execute(long input)
{
while (!Halted)
{
var (mode1, mode2, mode3, opCode) = ParseOpCode(_memory[_instructionPointer]);
switch (opCode)
{
case 1:
{
var newValue = GetValue(_memory[_instructionPointer + 1], mode1) + GetValue(_memory[_instructionPointer + 2], mode2);
SetValue(newValue, _memory[_instructionPointer + 3], mode3);
_instructionPointer += 4;
break;
}
case 2:
{
var newValue = GetValue(_memory[_instructionPointer + 1], mode1) * GetValue(_memory[_instructionPointer + 2], mode2);
SetValue(newValue, _memory[_instructionPointer + 3], mode3);
_instructionPointer += 4;
break;
}
case 3:
var inputValue = _setPhase ? input : _phase;
_setPhase = true;
if (_onInput != null)
inputValue = _onInput();
SetValue(inputValue, _memory[_instructionPointer + 1], mode1);
_instructionPointer += 2;
break;
case 4:
_output = GetValue(_memory[_instructionPointer + 1], mode1);
_instructionPointer += 2;
return _output;
case 5:
{
var value = GetValue(_memory[_instructionPointer + 1], mode1);
if (value != 0)
_instructionPointer = GetValue(_memory[_instructionPointer + 2], mode2);
else
_instructionPointer += 3;
}
break;
case 6:
{
var value = GetValue(_memory[_instructionPointer + 1], mode1);
if (value == 0)
_instructionPointer = GetValue(_memory[_instructionPointer + 2], mode2);
else
_instructionPointer += 3;
}
break;
case 7:
{
var value1 = GetValue(_memory[_instructionPointer + 1], mode1);
var value2 = GetValue(_memory[_instructionPointer + 2], mode2);
if (value1 < value2)
SetValue(1, _memory[_instructionPointer + 3], mode3);
else
SetValue(0, _memory[_instructionPointer + 3], mode3);
_instructionPointer += 4;
}
break;
case 8:
{
var value1 = GetValue(_memory[_instructionPointer + 1], mode1);
var value2 = GetValue(_memory[_instructionPointer + 2], mode2);
if (value1 == value2)
SetValue(1, _memory[_instructionPointer + 3], mode3);
else
SetValue(0, _memory[_instructionPointer + 3], mode3);
_instructionPointer += 4;
}
break;
case 9:
_relativeBase += GetValue(_memory[_instructionPointer + 1], mode1);
_instructionPointer += 2;
break;
case 99:
Halted = true;
return _output;
}
}
return _output;
}
private static (ParameterMode mode1, ParameterMode mode2, ParameterMode mode3, int opCode) ParseOpCode(long fullOpCode)
{
var codeString = fullOpCode.ToString("00000").ToCharArray();
var mode1 = Enum.Parse<ParameterMode>(codeString[2].ToString());
var mode2 = Enum.Parse<ParameterMode>(codeString[1].ToString());
var mode3 = Enum.Parse<ParameterMode>(codeString[0].ToString());
var opCode = int.Parse(codeString[3].ToString() + codeString[4]);
return (mode1, mode2, mode3, opCode);
}
}
}