/home/toolbox/public_html/solutions/100/10033/a.c
1 #include <stdio.h>
2
3 #define TRUE (1 == 1)
4 #define FALSE (1 != 1)
5 #define RAMSIZEPLUS 1500
6 #define RAMSIZE 1000
7 #define NUMREGS 10
8 #define NUMREGSPLUS 15
9 #define BUFFERSIZE 256
10 #define MAXVALUE 1000
11
12 //op codes
13 #define HALT 100
14
15
16 void getInput(int ram[])
17 {
18 // BEGIN FUNCTION getInput
19 int numRead = 0;
20 char * res;
21 int i;
22 int i1;
23 int i2;
24 int i3;
25 char buffer [BUFFERSIZE];
26 res = fgets(buffer,BUFFERSIZE,stdin);
27 // printf( "Beginning while loop.\n");
28 while (((NULL != res) && (!feof(stdin))) && (0 != strcmp(buffer,"\n")))
29 {
30 // printf( "%s\n",buffer);
31 //strip(buffer);
32 for (i = 0; ((' ' == buffer[i]) || ('\t' == buffer[i])); i ++) printf(".");
33 i1 = buffer[i++] - '0';
34 i2 = buffer[i++] - '0';
35 i3 = buffer[i] - '0';
36 ram[numRead] = (i1 * 100) + (i2 *10) + i3;
37 numRead++;
38 res = fgets(buffer,BUFFERSIZE,stdin);
39 }
40 } // END FUNCTION getInput
41
42 void dump(int ram[])
43 {
44 int i;
45 for (i = 0; i < RAMSIZEPLUS; i ++)
46 {
47 if(0 != ram[i])
48 {
49 printf("%d: %d\n", i, ram[i]);
50 }
51 }
52 }
53
54
55 int process(int ram[], int regs[])
56 {
57 // BEGIN FUNCTION process
58 int IP = 0;
59 int numInstructions = 1;
60 int opCode;
61 int p1;
62 int p2;
63 int temp;
64
65 while (HALT != ram[IP])
66 {
67 //BEGIN while
68 // for (temp = 0; NUMREGS > temp; temp ++)
69 // {
70 // printf("%d ",regs[temp]);
71 // }
72 // printf("\n");
73 opCode = ram[IP] / 100;
74 p1 = (ram[IP] / 10) % 10;
75 p2 = ram[IP] % 10;
76 // printf( "%d: %d %d %d\n", IP, opCode, p1, p2);
77 IP ++;
78 switch(opCode)
79 {
80 case 2 :
81 regs[p1] = p2;
82 break;
83 case 3 :
84 regs[p1] = (regs[p1] + p2) % MAXVALUE;
85 break;
86 case 4 :
87 regs[p1] = (regs[p1] * p2) % MAXVALUE;
88 break;
89 case 5 :
90 regs[p1] = regs[p2];
91 break;
92 case 6 :
93 regs[p1] = (regs[p1] + regs[p2]) % MAXVALUE;
94 break;
95 case 7 :
96 regs[p1] = (regs[p1] * regs[p2]) % MAXVALUE;
97 break;
98 case 8 :
99 regs[p1] = ram[regs[p2]];
100 break;
101 case 9 :
102 ram[regs[p2]] = regs[p1];
103 break;
104 case 0 :
105 if (0 != regs[p2]) IP = regs[p1];
106 break;
107 // default : printf( "Invalid op code Operation = [%d]\n", ram[IP - 1]);
108 }
109 numInstructions ++;
110 } //END while
111
112 return numInstructions;
113 } // END FUNCTION process
114
115
116 int numCases()
117 {
118 //START numCases()
119 int cases;
120 scanf(" %d ",&cases);
121 return cases;
122 } //END numCases()
123
124
125 void init(int ram[], int regs[])
126 {
127 // START init()
128 int i;
129
130 for ( i = 0; i < NUMREGSPLUS; i ++)
131 {
132 regs[i] = 0;
133 ram[i] = 0;
134 }
135 for ( i = NUMREGSPLUS; i < RAMSIZEPLUS; i ++)
136 {
137 ram[i] = 0;
138 }
139 } // END init()
140
141
142 int main ()
143 {
144 // main
145 int moreToDo;
146 int cases;
147 int currCase;
148 int result;
149 int ram [RAMSIZEPLUS];
150 int regs [NUMREGSPLUS];
151
152 cases = numCases();
153
154 for (currCase = 0; currCase < cases; currCase ++)
155 {
156 // for
157 init(ram, regs);
158 getInput(ram);
159 //dump(ram);
160 result = process(ram, regs);
161 printf("%d\n", result);
162 // printf("result; [%d]\n",result);
163 //printRes(result);
164 //printf("hello\n");
165 } // for
166
167 return 0;
168 } // main
169