-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.1.linq
205 lines (186 loc) · 3.52 KB
/
day18.1.linq
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
<Query Kind="Program" />
void Main()
{
var input = ReadInput();
//input.Dump();
Processor proc = new Processor();
long instructionPointer = 0;
while (instructionPointer >= 0 && instructionPointer < input.Length)
{
var instruction = input[instructionPointer];
switch (instruction.Command)
{
case "snd":
{
proc.Snd(instruction.X);
instructionPointer++;
break;
}
case "set":
{
proc.Set(instruction.X, instruction.Y);
instructionPointer++;
break;
}
case "add":
{
proc.Add(instruction.X, instruction.Y);
instructionPointer++;
break;
}
case "mul":
{
proc.Mul(instruction.X, instruction.Y);
instructionPointer++;
break;
}
case "mod":
{
proc.Mod(instruction.X, instruction.Y);
instructionPointer++;
break;
}
case "rcv":
{
long? data = proc.Rcv(instruction.X);
if (data.HasValue)
{
Console.WriteLine("RCV value: " + data.Value);
return;
}
instructionPointer++;
break;
}
case "jgz":
{
long? offset = proc.Jgz(instruction.X, instruction.Y);
if (offset.HasValue)
{
instructionPointer += offset.Value;
}
else
{
instructionPointer++;
}
break;
}
default:
throw new ApplicationException("Unknown instruction " + instruction.Command);
}
}
}
public class Processor
{
public Processor()
{
registers = new Dictionary<string, long>();
}
public Dictionary<string, long> registers;
public long LastPlayedSound;
public void Snd(string X)
{
long val = Resolve(X);
LastPlayedSound = val;
}
private void EnsureRegister(string register)
{
if (!registers.ContainsKey(register))
{
registers[register] = 0L;
}
}
public void Set(string X, string Y)
{
EnsureRegister(X);
registers[X] = Resolve(Y);
}
public void Add(string X, string Y)
{
EnsureRegister(X);
registers[X] += Resolve(Y);
}
public void Mul(string X, string Y)
{
EnsureRegister(X);
registers[X] *= Resolve(Y);
}
public void Mod(string X, string Y)
{
EnsureRegister(X);
registers[X] = registers[X] % Resolve(Y);
}
public long? Rcv(string X)
{
EnsureRegister(X);
if (registers[X] != 0L)
{
return LastPlayedSound;
}
return null;
}
public long? Jgz(string X, string Y)
{
EnsureRegister(X);
if (registers[X] > 0)
{
return Resolve(Y);
}
return null;
}
private long Resolve(string item)
{
long val;
if (long.TryParse(item, out val))
{
return val;
}
return GetRegisterValue(item);
}
public long GetRegisterValue(string register)
{
long val;
if (registers.TryGetValue(register, out val))
{
return val;
}
return registers[register] = 0;
}
}
struct Instruction
{
public string Command { get; set; }
public string X { get; set; }
public string Y { get; set; }
}
Instruction[] ReadInput()
{
var instructions = File.ReadAllLines("Q:\\git\\advent\\day18_input.txt");
Regex reInstruct = new Regex(@"
^
(?<command>[a-z]{3})
\s
(?<X>[a-z0-9]+)
(
\s
(?<Y>[a-z0-9\-]+)
)?
", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);
List<Instruction> list = new List<UserQuery.Instruction>();
foreach (var instruction in instructions)
{
Match m = reInstruct.Match(instruction);
string command = m.Groups["command"].Value;
string x = m.Groups["X"].Value;
string y = String.Empty;
if (m.Groups["Y"].Success)
{
y = m.Groups["Y"].Value;
}
list.Add(new Instruction{
Command = command,
X = x,
Y = y,
});
}
return list.ToArray();
}