-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10033.cpp
93 lines (88 loc) · 1.87 KB
/
P10033.cpp
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
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
int readLine(int *ram) {
std::string line;
if(!std::getline(std::cin, line))
return 0;
std::stringstream ss; ss << line;
int ret = 0;
while(ss >> ram[ret])
++ret;
return ret;
}
int countInstructions(int *ram) {
int registers[10];
for(int i = 0; i < 10; ++i)
registers[i] = 0;
int ret = 1;
int ramIndex = 0;
int val;
while(ramIndex < 1000 && (val = ram[ramIndex++]) != 100) {
++ret;
int d1 = val/100;
int d2 = (val/10)%10;
int d3 = val%10;
switch(d1) {
case 2:
registers[d2] = d3;
break;
case 3:
registers[d2] = (d3+registers[d2])%1000;
break;
case 4:
registers[d2] = (d3*registers[d2])%1000;
break;
case 5:
registers[d2] = registers[d3];
break;
case 6:
registers[d2] = (registers[d3]+registers[d2])%1000;
break;
case 7:
registers[d2] = (registers[d3]*registers[d2])%1000;
break;
case 8:
registers[d2] = ram[registers[d3]];
break;
case 9:
ram[registers[d3]] = registers[d2];
break;
case 0:
if(registers[d3] != 0) {
ramIndex = registers[d2];
//std::cerr << "Goto " << ramIndex << std::endl;
}
break;
default:
break;
}
}
return ret;
}
/*
10 registers
1000 ram
3 digit int 0-999, mod 1000
Initial address: 0
*/
int main() {
int cases, ram[1001];
readLine(ram);
cases = ram[0];
readLine(ram); // Read empty line
for(int cas = 0; cas < cases; ++cas) {
if(cas != 0)
std::cout << std::endl;
int line = 0;
int readNumbers;
while((readNumbers = readLine(&ram[line])) > 0)
line += readNumbers;
//std::cerr << "Program size: " << line << std::endl;
for(; line < 1000; ++line)
ram[line] = 0;
std::cout << countInstructions(ram) << std::endl;
}
return 0;
}