## 10033 - Interpreter

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

hardcode
New poster
Posts: 4
Joined: Thu Jul 05, 2007 2:58 am
Exactly, so if 125 is not a valid instruction, how can it be counted towards the instruction count(that adds up to 5) and therefore make the program stop? I too am just using the 1st digit to conclude it is a halt,but isnt it necessary from the table that, 1 be followed by 2 zeroes?

thanks you for your prompt reply and time. I hope my questions are clear!

Thanks!

tgoulart
New poster
Posts: 42
Joined: Sat Oct 21, 2006 8:37 am
Location: Alegrete, Brazil
It doesn't even get passed the 125, it stops right there and just skips the following lines.

If you want to make sure the other two digits are zeroes I guess you won't have any problems, since 125 isn't supposed to appear anyway... Keep in mind that this input is wrong according to the description.
Thiago Sonego Goulart - UFMG/Brazil

hardcode
New poster
Posts: 4
Joined: Thu Jul 05, 2007 2:58 am
Thanks for your reply.I see what you're saying. The input itself is wrong. So what is the output then?

If 125,175 and 183 are being skipped, and then 256, 012 and 100 are being accessed,(012 doesn't produce a jump, since contents of register 2 is 0), Shouldn't the output be 7?

456 - 1st instruction
789 - 2nd instruction
234 - 3rd instruction
453 - 4th instruction
125(skip)
175(skip)
183(skip)
256 - 5th instruction
012 - 6th instruction
100 - 7th instruction

tgoulart
New poster
Posts: 42
Joined: Sat Oct 21, 2006 8:37 am
Location: Alegrete, Brazil

456 - 1st instruction
789 - 2nd instruction
234 - 3rd instruction
453 - 4th instruction
125 - 5th instruction (halt)
175(skip)
183(skip)
256(skip)
012(skip)
100(skip)

That's why i'm telling you, this input is wrong. You don't need to verify if the instruction is valid. In my program, 125 is interpreted as the halt function because it starts with 1.
Thiago Sonego Goulart - UFMG/Brazil

hardcode
New poster
Posts: 4
Joined: Thu Jul 05, 2007 2:58 am
Hey tgoulart...I got it! thanks for your replies:)

alirezanoori
New poster
Posts: 26
Joined: Fri Jan 02, 2009 12:41 am

### Re: 10033 - Interpreter

Well, I think I solved this problem and I tested my program with every test data in this forum. Buy I get Wrong Answer!!! Could you please take a look at my program?

Code: Select all

``````//Removed after AC
``````
Last edited by alirezanoori on Fri Jan 09, 2009 9:19 pm, edited 1 time in total.

ExUCI
New poster
Posts: 14
Joined: Sat Aug 12, 2006 3:31 am
Location: USA

### Re: 10033 - Interpreter

There is an extra end-of-line at the end of the output, that's all!!! ....the empty lines are between cases

Remove your code after AC
Remove your code after AC

evandrix
New poster
Posts: 8
Joined: Wed Oct 03, 2007 11:07 am

### Re: 10033 - Interpreter

ExUCI wrote:There is an extra end-of-line at the end of the output, that's all!!! ....the empty lines are between cases
Remove your code after AC
Yea, so the AC code is as follows:

Code: Select all

``````< < < removed by request of author on 9 Jan 09 23 09 > > >
``````
Last edited by evandrix on Sat Jan 10, 2009 1:09 am, edited 1 time in total.

ExUCI
New poster
Posts: 14
Joined: Sat Aug 12, 2006 3:31 am
Location: USA

### Re: 10033 - Interpreter

Exactly, now just remove your code so no one steals it
Remove your code after AC

alirezanoori
New poster
Posts: 26
Joined: Fri Jan 02, 2009 12:41 am

### Re: 10033 - Interpreter

Thank you guys. It's very silly! Just take a look at the bug!!!! I can't believe it. I think online judges should take care of these kinds of problem. Don't you think so?
And please remove my code because I can't remove the code you posted!

kg4yed
New poster
Posts: 1
Joined: Tue Feb 10, 2009 11:37 pm

### Re: 10033 - Interpreter

Hi all.

All the code I have seen has been in C++, but I am trying this one in Java. For some reason, my code passes all the tests that I could find, but the judge says Wrong Answer.

Any ideas? Thanks in advance.

Code: Select all

``````import java.util.*;

class Main {

private int[] registers;
private String[] memory;
private int counter;
private int instructions;
private String currentInstruction;
private boolean halt;

public int cases = 0;

public Main(){
registers = new int[10];
memory = new String[1000];
counter = 0;
instructions = 0;
currentInstruction = "";
halt = false;
}

public void reset(){
counter = 0;
instructions = 0;
currentInstruction = "";
halt = false;
for(int i=0;i<registers.length;i++){  //Reseting Registers
registers[i] = 0;
}
for(int i=0;i<memory.length;i++){  //Reseting RAM
memory[i] = "000";
}
}

public void loadInstructions(Scanner in){
int tempCount = 0;
String tempString = "";
while(in.hasNext()){
tempString = in.nextLine();
if(!tempString.equals("")){
memory[tempCount] = tempString;
}
else{
break;
}
tempCount++;
}
}

this.currentInstruction = memory[counter];
}

public void run(){
while(!halt){
int opcode = Integer.parseInt(String.valueOf(this.currentInstruction.charAt(0)));
int paramA = Integer.parseInt(String.valueOf(this.currentInstruction.charAt(1)));
int paramB = Integer.parseInt(String.valueOf(this.currentInstruction.charAt(2)));

switch(opcode){
case 0:
instructions++;
if(registers[paramB] != 0){
counter = registers[paramA];
}
else{
counter++;
}
break;
case 1:
instructions++;
if(paramA == 0 && paramB ==0)
this.halt = true;
counter++;
break;
case 2:
instructions++;
registers[paramA] = paramB;
counter++;
break;
case 3:
instructions++;
registers[paramA] = (registers[paramA] + paramB) % 1000;
counter++;
break;
case 4:
instructions++;
registers[paramA] = (registers[paramA] * paramB) % 1000;
counter++;
break;
case 5:
instructions++;
registers[paramA] = registers[paramB] % 1000;
counter++;
break;
case 6:
instructions++;
registers[paramA] = (registers[paramA] + registers[paramB]) % 1000;
counter++;
break;
case 7:
instructions++;
registers[paramA] = (registers[paramA] * registers[paramB]) % 1000;
counter++;
break;
case 8:
instructions++;
registers[paramA] = Integer.parseInt((memory[registers[paramA]])) % 1000;
counter++;
break;
case 9:
instructions++;
memory[registers[paramB]] = Integer.toString((registers[paramA] % 1000));
counter++;
break;
}
}
answers += (String.valueOf(this.instructions) + " ");
}

public static void main(String[] args){
Main m = new Main();
m.reset();
Scanner input = new Scanner(System.in);
m.cases = Integer.parseInt((input.nextLine()));
input.nextLine();
for(int inputCase = 0;inputCase < m.cases;inputCase++){
m.run();
m.reset();
}
input.close();
}

}
``````

williamxu
New poster
Posts: 1
Joined: Sun Apr 26, 2009 8:36 am

### why my code is tle?

why my code is tle?
i just simulate it.

Code: Select all

``````#include <iostream>
#include <string>

using namespace std;

int main()
{
int num_of_case;

cin >> num_of_case;
while (num_of_case--)
{
int ram[1000] = {0};
int reg[10] = {0};

string input_instruction;
int ram_index = 0;

// eat blank line.
cin.ignore();
getline(cin, input_instruction);

// initial ram.
while(!cin.eof())
{
getline(cin, input_instruction);
if (input_instruction == "")
{
break;
}
else
{
ram[ram_index] = atoi(input_instruction.c_str());
++ram_index;
}
}

// execute instruction.
int num_of_instruction_execute = 0;
int current_address = 0;
bool ishalt = false;

while (!ishalt)
{
int first = ram[current_address] % 10;
int second = ram[current_address] % 100 / 10;
int third = ram[current_address] % 1000 / 100;

++num_of_instruction_execute;

switch(third)
{
case 1:
if (first == 0 && second == 0)
{
ishalt = true;
}
break;

case 2:
reg[second] = first;
break;

case 3:
reg[second] = (reg[second] + first) % 1000;
break;

case 4:
reg[second] = (reg[second] * first) % 1000;
break;

case 5:
reg[second] = reg[first];
break;

case 6:
reg[second] = (reg[second] + reg[first]) % 1000;
break;

case 7:
reg[second] = (reg[second] * reg[first]) % 1000;
break;

case 8:
reg[second] = ram[reg[first]];
break;

case 9:
ram[reg[first]] = reg[second];
break;

case 0:
if (reg[first] != 0)
{
}
break;

default:
break;
}
}

cout << num_of_instruction_execute << endl;
if (num_of_case > 0)
{
cout << endl;
}
}

return 0;
}
``````

WingletE
New poster
Posts: 35
Joined: Sun Aug 13, 2006 1:34 pm
Location: Taipei, Taiwan
Contact:

### Re: 10033 - Interpreter

Since I didn't find any testcase with instructions beginning with 8 or 9, I made some and hope that will be helpful to those who make the same mistakes as I did.

Input:

Code: Select all

``````2

221
422
425
000
311
712
712
913
031

299
233
255
990
803
301
050
100
``````
Output:

Code: Select all

``````10

26
``````
to williamxu:
I didn't know if you've solved the problem or not, but I think the problem is that you didn't take care of the instructions starting with 0 properly. Because of this line:

Code: Select all

``         ++current_address;``

panteluke
New poster
Posts: 2
Joined: Sun Jun 28, 2009 6:22 pm

### Re: 10033 - Interpreter

Hi all, this is my first post as it seems I cannot pass this problem.

I have used all the test input data successfully.
My only doubt is about
299
492
279
283
078
100
123
345
which I don't even know which should be the correct answer.

My code is as follows:

Code: Select all

``````class myStuff implements Runnable{

int[] registers = new int[10];
int[] code = new int[1000];

int cases;

private void clear() {
for (int i=0; i<10; i++) {
registers[i] = 0;
}
for (int i=0; i<1000; i++) {
code[i] = 0;
}

}

private void setRegister(int i, int n) {
registers[i] = n;
registers[i] = registers[i] % 1000;

}

private void addRegister(int i, int n) {
registers[i] += n;
registers[i] = registers[i] % 1000;
}

private void multiplyRegister(int i, int n) {
registers[i] *= n;
registers[i] = registers[i] % 1000;
}

private void equalRegister(int i, int j) {
registers[i] = registers[j];
}

private void addRegisters(int i, int j) {
registers[i] += registers[j];
registers[i] = registers[i] % 1000;
}

private void multiplyRegisters(int i, int j) {
registers[i] *= registers[j];
registers[i] = registers[i] % 1000;
}

private void setRegisterRam(int i, int j){
registers[i] = code[registers[j]] % 1000;
}

private void setRamRegister(int i, int j){
code[registers[j]] = registers[i];
}

public void run(){
String input;
StringTokenizer idata;

input = Main.ReadLn (255);
idata = new StringTokenizer (input);
cases = Integer.parseInt (idata.nextToken());
// empty line

for (int counter = 1; counter < cases + 1; counter++){
int CounterCode = 0;
clear();
while ((input = Main.ReadLn (255)) != null)  {
boolean blank = ( input.length() == 0);
if (!blank) {
idata = new StringTokenizer (input);
code[CounterCode] = Integer.parseInt(idata.nextToken());
CounterCode++;
}
else {
boolean halted = false;
int steps = 0;
int executionPoint = 0;

while (!halted) {
int command = code[executionPoint] / 100;
int register = (code[executionPoint] - command * 100) / 10;
int value = (code[executionPoint] - command * 100 - register * 10);

steps++;

switch (command) {
case 0:
if (registers[value] != 0) {
executionPoint = registers[register];
}
else{
executionPoint++;
}

break;
case 1:
halted = true;
break;
case 2:
setRegister(register, value);
executionPoint++;
break;
case 3:
executionPoint++;
break;
case 4:
multiplyRegister(register, value);
executionPoint++;
break;
case 5:
equalRegister(register, value);
executionPoint++;
break;
case 6:
executionPoint++;
break;
case 7:
multiplyRegisters(register, value);
executionPoint++;
break;
case 8:
setRegisterRam(register, value);
executionPoint++;
break;
case 9:
setRamRegister(register, value);
executionPoint++;
break;
}
// for the strange case ??
if (executionPoint == 1000) halted = true;

}

System.out.println(steps);
if (counter != cases) {
System.out.println();
}
break;
}
}
}
}
``````
Thank you in advance.

carokhan9
New poster
Posts: 1
Joined: Thu Jul 16, 2009 1:00 am

### Re: 10033 - Interpreter

Hi panteluke

This is also my first post. My AC code gives a Segfault to your input... So it must be an incorrect input.