-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.linq
218 lines (201 loc) · 7.09 KB
/
19.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
206
207
208
209
210
211
212
213
214
215
216
217
218
<Query Kind="Program" />
void Main()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
string inputString = File.ReadAllLines("19.txt").First();
long[] memory = inputString.Split(',').Select(i => long.Parse(i)).ToArray();
$"P1: {Part1(memory)}".Dump();
$"P2: {Part2(memory)}".Dump();
}
long Part1(long[] memory)
{
long counter = 0;
for (long y = 0; y < 50; y++)
{
for (long x = 0; x < 50; x++)
{
var machine = new Machine(memory, new int[] { (int)x, (int)y });
machine.Calc();
if (machine.Output == 1) counter++;
}
}
return counter;
}
long Part2(long[] memory)
{
Dictionary<long, (long x1, long x2)> set = new Dictionary<long, (long x1, long x2)>();
(long x, long y) left = (0, 0);
(long x, long y) right = (0, 0);
long y = 6, x = 8, minX = 0;
bool haveHit = false;
int side = 100;
while (true)
{
var machine = new Machine(memory, new int[] { (int)x, (int)y });
machine.Calc();
long output = machine.Output;
if (output == 1)
{
if (!haveHit)
{
left = (x, y);
haveHit = true;
minX = x;
}
x++;
}
else if (output == 0)
{
if (!haveHit)
{
x++;
}
else if (haveHit)
{
right = (x - 1, y);
x = minX;
y++;
haveHit = false;
if (right.x >= left.x + side - 1)
{
long lookAtY = left.y - side + 1;
set.Add(right.y, (left.x, right.x));
if (!set.ContainsKey(lookAtY)) continue;
var previousRow = set[lookAtY];
var startX = left.x;
var startY = lookAtY;
if (previousRow.x1 <= startX && previousRow.x2 >= startX + side - 1)
{
return (startX * 10000 + startY);
}
}
}
}
else throw new Exception();
}
}
public enum Mode
{
Position = 0,
Immediate = 1,
Relative = 2
}
public class Machine
{
private int[] inputArray;
private int inputPointer = 0;
public Machine(long[] initialMemory, int[] input = null)
{
for (int i = 0; i < initialMemory.Length; i++)
{
Memory[i] = initialMemory[i];
}
inputArray = input != null ? input : null;
}
public Machine(Machine machineToClone)
{
Memory = machineToClone.Memory.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
IP = machineToClone.IP;
}
public long IP { get; private set; } = 0;
private long RelativeBase = 0;
private Dictionary<long, long> Memory = new Dictionary<long, long>();
public bool Complete { get; private set; }
public long Output { get; private set; } = 0;
private long GetMemory(long i)
{
if (i < 0) throw new Exception();
if (Memory.ContainsKey(i)) return Memory[i];
else return 0;
}
private long GetLocation(Mode m, int i)
{
if (m == Mode.Position) return Memory[IP + i];
else if (m == Mode.Immediate) return IP + i;
else if (m == Mode.Relative) return Memory[IP + i] + RelativeBase;
else throw new Exception("Error!");
}
public void Calc(int? input = null)
{
while (true)
{
string instruction = Memory[IP].ToString();
int opcode = instruction.Length >= 2 ? int.Parse(instruction.Substring(instruction.Length - 2)) : int.Parse(instruction);
Mode aMode = instruction.Length >= 3 ? (Mode)int.Parse(instruction.Substring(instruction.Length - 3, 1)) : 0;
Mode bMode = instruction.Length >= 4 ? (Mode)int.Parse(instruction.Substring(instruction.Length - 4, 1)) : 0;
Mode cMode = instruction.Length >= 5 ? (Mode)int.Parse(instruction.Substring(instruction.Length - 5, 1)) : 0;
switch (opcode)
{
case 1:
case 2:
{
bool multiply = opcode == 2;
long a = GetLocation(aMode, 1);
long b = GetLocation(bMode, 2);
long c = GetLocation(cMode, 3);
if (multiply) Memory[c] = GetMemory(a) * GetMemory(b);
else Memory[c] = GetMemory(a) + GetMemory(b);
IP += 4;
}
break;
case 3:
{
long a = GetLocation(aMode, 1);
int inputValue = 0;
if (input.HasValue) inputValue = input.Value;
else if (inputArray != null && inputPointer < inputArray.Length)
{
Memory[a] = inputArray[inputPointer];
inputPointer++;
}
IP += 2;
}
break;
case 4:
{
long a = GetLocation(aMode, 1);
Output = GetMemory(a);
IP += 2;
return;
}
case 5:
case 6:
{
bool checkEqual = opcode == 6;
long a = GetLocation(aMode, 1);
long b = GetLocation(bMode, 2);
if (checkEqual && GetMemory(a) == 0 || !checkEqual && GetMemory(a) != 0) IP = GetMemory(b);
else IP += 3;
}
break;
case 7:
case 8:
{
bool checkEqual = opcode == 8;
long a = GetLocation(aMode, 1);
long b = GetLocation(bMode, 2);
long c = GetLocation(cMode, 3);
if (checkEqual && GetMemory(a) == GetMemory(b) || !checkEqual && GetMemory(a) < GetMemory(b)) Memory[c] = 1;
else Memory[c] = 0;
IP += 4;
}
break;
case 9:
{
long a = GetLocation(aMode, 1);
RelativeBase += GetMemory(a);
IP += 2;
}
break;
case 99:
{
Complete = true;
return;
}
default:
throw new Exception("Error!");
}
}
throw new Exception("Error!");
}
}