-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.linq
77 lines (69 loc) · 5.25 KB
/
5.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
<Query Kind="Program" />
void Main()
{
int p1 = Compute(1);
int p2 = Compute(5);
$"P1: {p1}".Dump();
$"P2: {p2}".Dump();
}
const int ImmediateMode = 1;
int Compute(int input)
{
string inputString = "3,225,1,225,6,6,1100,1,238,225,104,0,101,71,150,224,101,-123,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,2,205,209,224,1001,224,-3403,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1101,55,24,224,1001,224,-79,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1,153,218,224,1001,224,-109,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1002,201,72,224,1001,224,-2088,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,1102,70,29,225,102,5,214,224,101,-250,224,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1101,12,52,225,1101,60,71,225,1001,123,41,224,1001,224,-111,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1102,78,66,224,1001,224,-5148,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1101,29,77,225,1102,41,67,225,1102,83,32,225,1101,93,50,225,1102,53,49,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1107,677,677,224,1002,223,2,223,1005,224,329,101,1,223,223,7,677,677,224,1002,223,2,223,1005,224,344,1001,223,1,223,7,226,677,224,102,2,223,223,1006,224,359,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,389,1001,223,1,223,1108,226,677,224,1002,223,2,223,1006,224,404,101,1,223,223,1107,677,226,224,102,2,223,223,1006,224,419,101,1,223,223,1007,677,677,224,1002,223,2,223,1005,224,434,101,1,223,223,7,677,226,224,102,2,223,223,1006,224,449,1001,223,1,223,1008,226,677,224,1002,223,2,223,1006,224,464,101,1,223,223,8,677,677,224,1002,223,2,223,1006,224,479,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,494,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,554,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,569,101,1,223,223,107,677,226,224,102,2,223,223,1005,224,584,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,599,101,1,223,223,1108,677,226,224,1002,223,2,223,1006,224,614,101,1,223,223,8,677,226,224,102,2,223,223,1005,224,629,1001,223,1,223,1008,677,677,224,102,2,223,223,1006,224,644,101,1,223,223,1007,226,677,224,102,2,223,223,1005,224,659,101,1,223,223,108,226,677,224,102,2,223,223,1006,224,674,101,1,223,223,4,223,99,226";
int[] memory = inputString.Split(',').Select(i => int.Parse(i)).ToArray();
int ip = 0;
int output = int.MinValue;
while (ip >= 0 && ip < memory.Length)
{
string instruction = memory[ip].ToString();
int opcode = instruction.Length >= 2 ? int.Parse(instruction.Substring(instruction.Length - 2)): int.Parse(instruction);
int aMode = instruction.Length >= 3 ? int.Parse(instruction.Substring(instruction.Length - 3, 1)) : 0;
int bMode = instruction.Length >= 4 ? int.Parse(instruction.Substring(instruction.Length - 4, 1)) : 0;
int cMode = instruction.Length >= 5 ? int.Parse(instruction.Substring(instruction.Length - 5, 1)) : 0;
switch (opcode) {
case 1:
case 2: {
bool multiply = opcode == 2;
int a = aMode == ImmediateMode ? memory[ip + 1] : memory[memory[ip + 1]];
int b = bMode == ImmediateMode ? memory[ip + 2] : memory[memory[ip + 2]];
int c = cMode == ImmediateMode ? ip+3 : memory[ip+3];
if (multiply) memory[c] = a * b;
else memory[c] = a + b;
ip += 4;
} break;
case 3: {
memory[memory[ip+1]] = input;
ip += 2;
} break;
case 4: {
output = memory[memory[ip+1]];
ip += 2;
} break;
case 5:
case 6: {
bool checkEqual = opcode == 6;
int a = aMode == ImmediateMode ? memory[ip + 1] : memory[memory[ip + 1]];
int b = bMode == ImmediateMode ? memory[ip + 2] : memory[memory[ip + 2]];
if (checkEqual && a == 0 || !checkEqual && a != 0) ip = b;
else ip += 3;
} break;
case 7:
case 8: {
bool checkEqual = opcode == 8;
int a = aMode == ImmediateMode ? memory[ip + 1] : memory[memory[ip + 1]];
int b = bMode == ImmediateMode ? memory[ip + 2] : memory[memory[ip + 2]];
int c = cMode == ImmediateMode ? ip+3 : memory[ip+3];
if (checkEqual && a == b || !checkEqual && a < b) memory[c] = 1;
else memory[c] = 0;
ip += 4;
} break;
case 99: {
return output;
}
default: {
throw new Exception("Halt!");
}
}
}
return output;
}