-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-2b.linq
121 lines (101 loc) · 2.38 KB
/
16-2b.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
<Query Kind="Program" />
Dictionary<int, Action<int, int, int>> opcodes = new Dictionary<int, Action<int, int, int>>();
int[] registers;
void Main()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
var inputLines = File.ReadAllLines("16-2.txt");
opcodes.Add(12, addr);
opcodes.Add(14, addi);
opcodes.Add(7, mulr);
opcodes.Add(3, muli);
opcodes.Add(10, banr);
opcodes.Add(15, bani);
opcodes.Add(5, borr);
opcodes.Add(6, bori);
opcodes.Add(0, setr);
opcodes.Add(9, seti);
opcodes.Add(13, gtir);
opcodes.Add(2, gtri);
opcodes.Add(8, gtrr);
opcodes.Add(4, eqir);
opcodes.Add(11, eqri);
opcodes.Add(1, eqrr);
registers = new int[4];
for (int i = 0; i < inputLines.Length; i++)
{
int[] instructions = inputLines[i].Split(' ').Select(n => int.Parse(n)).ToArray();
opcodes[instructions[0]](instructions[1], instructions[2], instructions[3]);
}
registers[0].Dump();
}
void addr(int a, int b, int c)
{
registers[c] = registers[a] + registers[b];
}
void addi(int a, int b, int c)
{
registers[c] = registers[a] + b;
}
void mulr(int a, int b, int c)
{
registers[c] = registers[a] * registers[b];
}
void muli(int a, int b, int c)
{
registers[c] = registers[a] * b;
}
void banr(int a, int b, int c)
{
registers[c] = registers[a] & registers[b];
}
void bani(int a, int b, int c)
{
registers[c] = registers[a] & b;
}
void borr(int a, int b, int c)
{
registers[c] = registers[a] | registers[b];
}
void bori(int a, int b, int c)
{
registers[c] = registers[a] | b;
}
void setr(int a, int b, int c)
{
registers[c] = registers[a];
}
void seti(int a, int b, int c)
{
registers[c] = a;
}
void gtir(int a, int b, int c)
{
if (a > registers[b]) registers[c] = 1;
else registers[c] = 0;
}
void gtri(int a, int b, int c)
{
if (registers[a] > b) registers[c] = 1;
else registers[c] = 0;
}
void gtrr(int a, int b, int c)
{
if (registers[a] > registers[b]) registers[c] = 1;
else registers[c] = 0;
}
void eqir(int a, int b, int c)
{
if (a == registers[b]) registers[c] = 1;
else registers[c] = 0;
}
void eqri(int a, int b, int c)
{
if (registers[a] == b) registers[c] = 1;
else registers[c] = 0;
}
void eqrr(int a, int b, int c)
{
if (registers[a] == registers[b]) registers[c] = 1;
else registers[c] = 0;
}