-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.linq
158 lines (146 loc) · 5.49 KB
/
25.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
<Query Kind="Program" />
static void Main()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
string inputString = System.IO.File.ReadAllLines("25.txt").First();
long[] memory = inputString.Split(',').Select(i => long.Parse(i)).ToArray();
Machine m = new Machine(memory);
while (!m.Complete)
{
m.Calc();
}
}
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);
if (inputArray != null && inputPointer >= inputArray.Length)
{
inputArray = null;
inputPointer = 0;
}
if (inputArray == null)
{
string text = Console.ReadLine();
List<int> list = text.ToCharArray().Select(c => (int)c).ToList();
list.Add((int)'\n');
inputArray = list.ToArray();
};
int inputValue = inputArray[inputPointer];
Memory[a] = inputValue;
inputPointer++;
IP += 2;
}
break;
case 4:
{
long a = GetLocation(aMode, 1);
Output = GetMemory(a);
Console.Write((char)Output);
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!");
}
}