-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-1.linq
139 lines (118 loc) · 2.97 KB
/
16-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
<Query Kind="Program" />
List<Action<int, int, int>> opcodes = new List<Action<int, int, int>>();
int[] registers;
void Main()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
var inputLines = File.ReadAllLines("16-1.txt");
opcodes.Add(addr);
opcodes.Add(addi);
opcodes.Add(mulr);
opcodes.Add(muli);
opcodes.Add(banr);
opcodes.Add(bani);
opcodes.Add(borr);
opcodes.Add(bori);
opcodes.Add(setr);
opcodes.Add(seti);
opcodes.Add(gtir);
opcodes.Add(gtri);
opcodes.Add(gtrr);
opcodes.Add(eqir);
opcodes.Add(eqri);
opcodes.Add(eqrr);
int totalWithThreeOrMore = 0;
for (int i = 0; i < inputLines.Length; i = i + 4)
{
int matchingOps = 0;
foreach (var op in opcodes)
{
registers = new int[4];
registers[0] = inputLines[i][9] - 48;
registers[1] = inputLines[i][12] - 48;
registers[2] = inputLines[i][15] - 48;
registers[3] = inputLines[i][18] - 48;
int[] instructions = inputLines[i + 1].Split(' ').Select(n => int.Parse(n)).ToArray();
op(instructions[1], instructions[2], instructions[3]);
if (registers[0] == inputLines[i + 2][9] - 48 &&
registers[1] == inputLines[i + 2][12] - 48 &&
registers[2] == inputLines[i + 2][15] - 48 &&
registers[3] == inputLines[i + 2][18] - 48)
{
matchingOps++;
}
}
if (matchingOps >= 3) totalWithThreeOrMore++;
}
totalWithThreeOrMore.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;
}