| Task: | Tulkki |
| Sender: | j-l |
| Submission time: | 2025-11-01 18:47:59 +0200 |
| Language: | Node.js |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 12 |
| #2 | ACCEPTED | 32 |
| #3 | ACCEPTED | 56 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.11 s | 1, 2, 3 | details |
| #2 | ACCEPTED | 0.11 s | 1, 2, 3 | details |
| #3 | ACCEPTED | 0.11 s | 1, 2, 3 | details |
| #4 | ACCEPTED | 0.11 s | 1, 2, 3 | details |
| #5 | ACCEPTED | 0.11 s | 1, 2, 3 | details |
| #6 | ACCEPTED | 0.11 s | 1, 2, 3 | details |
| #7 | ACCEPTED | 0.11 s | 2, 3 | details |
| #8 | ACCEPTED | 0.11 s | 2, 3 | details |
| #9 | ACCEPTED | 0.11 s | 2, 3 | details |
| #10 | ACCEPTED | 0.12 s | 2, 3 | details |
| #11 | ACCEPTED | 0.11 s | 2, 3 | details |
| #12 | ACCEPTED | 0.11 s | 2, 3 | details |
| #13 | ACCEPTED | 0.11 s | 3 | details |
| #14 | ACCEPTED | 0.11 s | 3 | details |
| #15 | ACCEPTED | 0.14 s | 3 | details |
| #16 | ACCEPTED | 0.11 s | 3 | details |
| #17 | ACCEPTED | 0.20 s | 3 | details |
| #18 | ACCEPTED | 0.14 s | 3 | details |
Code
const fs = require('fs');
const input = fs.readFileSync(0, 'utf-8'); // Lukee stdin:istä
// Poistaa kommentit ja pilkkoo tokenit
function tokenize(code) {
const lines = code
.split('\n')
.map(line => line.split('#')[0]) // Poista kommentit
.join('\n')
.replace(/\(/g, ' ( ')
.replace(/\)/g, ' ) ')
.split(/\s+/)
.filter(Boolean);
return lines;
}
// Parsii tokenit komentorakenteiksi
function parse(tokens) {
function parseBlock() {
const block = [];
while (tokens.length > 0) {
const token = tokens.shift();
if (token === 'CLEAR') {
block.push({ type: 'CLEAR', var: tokens.shift() });
} else if (token === 'INCREASE') {
block.push({ type: 'INCREASE', var: tokens.shift() });
} else if (token === 'PRINT') {
block.push({ type: 'PRINT', var: tokens.shift() });
} else if (token === 'REPEAT') {
const varName = tokens.shift();
tokens.shift(); // TIMES
tokens.shift(); // (
const inner = parseBlock();
block.push({ type: 'REPEAT', var: varName, block: inner });
} else if (token === ')') {
break;
}
}
return block;
}
return parseBlock();
}
// Suorittaa ohjelman
function execute(program, variables) {
const output = [];
function run(block) {
for (const cmd of block) {
if (cmd.type === 'CLEAR') {
variables[cmd.var] = 0;
} else if (cmd.type === 'INCREASE') {
variables[cmd.var] += 1;
} else if (cmd.type === 'PRINT') {
output.push(variables[cmd.var]);
} else if (cmd.type === 'REPEAT') {
const count = variables[cmd.var];
for (let i = 0; i < count; i++) {
run(cmd.block);
}
}
}
}
run(program);
return output;
}
// Pääohjelma
const tokens = tokenize(input);
const program = parse(tokens);
const variables = Object.fromEntries(
Array.from({ length: 26 }, (_, i) => [String.fromCharCode(65 + i), 0])
);
const result = execute(program, variables);
console.log(result.join(' '));Test details
Test 1 (public)
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| PRINT X INCREASE X PRINT X INCREASE X PRINT X ... |
| correct output |
|---|
| 0 1 2 0 |
| user output |
|---|
| 0 1 2 0 |
Test 2 (public)
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE X # aybabtu PRINT X INCREASE # test ... |
| correct output |
|---|
| 1 3 |
| user output |
|---|
| 1 3 |
Test 3 (public)
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| # Create number 3 INCREASE X INCREASE X INCREASE X ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 4 (public)
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A PRINT A INCREASE B PRINT B INCREASE C ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
Test 5 (public)
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE X INCREASE X INCREASE X INCREASE X INCREASE X ... |
| correct output |
|---|
| 999 |
| user output |
|---|
| 999 |
Test 6 (public)
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| PRINT X PRINT X PRINT X PRINT X PRINT X ... |
| correct output |
|---|
| 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
| user output |
|---|
| 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
Test 7 (public)
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE A INCREASE A INCREASE A ... |
| correct output |
|---|
| 5 5 5 5 5 |
| user output |
|---|
| 5 5 5 5 5 |
Test 8 (public)
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE A INCREASE A INCREASE A ... |
| correct output |
|---|
| 0 0 0 0 0 |
| user output |
|---|
| 0 0 0 0 0 |
Test 9 (public)
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE A INCREASE A INCREASE A ... |
| correct output |
|---|
| 6 7 8 9 10 |
| user output |
|---|
| 6 7 8 9 10 |
Test 10 (public)
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE A INCREASE A INCREASE A ... |
| correct output |
|---|
| 5 5 |
| user output |
|---|
| 5 5 |
Test 11 (public)
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE A INCREASE A INCREASE A ... |
| correct output |
|---|
| 20 |
| user output |
|---|
| 20 |
Test 12 (public)
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE B INCREASE B ... |
| correct output |
|---|
| 42 |
| user output |
|---|
| 42 |
Test 13 (public)
Group: 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE A INCREASE A INCREASE A ... |
| correct output |
|---|
| 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 |
| user output |
|---|
| 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 |
Test 14 (public)
Group: 3
Verdict: ACCEPTED
| input |
|---|
| # Create number 3 INCREASE A INCREASE A INCREASE... |
| correct output |
|---|
| 12 |
| user output |
|---|
| 12 |
Test 15 (public)
Group: 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE X INCREASE X INCREASE X INCREASE X INCREASE X ... |
| correct output |
|---|
| 531441 |
| user output |
|---|
| 531441 |
Test 16 (public)
Group: 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A INCREASE A INCREASE A INCREASE A ... |
| correct output |
|---|
| 1337 |
| user output |
|---|
| 1337 |
Test 17 (public)
Group: 3
Verdict: ACCEPTED
| input |
|---|
| INCREASE A INCREASE A REPEAT A TIMES ( REPEAT A TIMES ( ... |
| correct output |
|---|
| 1 2 1 2 1 1 3 4 3 4 3 4 3 4 3 ... |
| user output |
|---|
| 1 2 1 2 1 1 3 4 3 4 3 4 3 4 3 ... |
Test 18 (public)
Group: 3
Verdict: ACCEPTED
| input |
|---|
| # Efficient algorithm for find... |
| correct output |
|---|
| 2 3 5 7 11 13 17 19 23 29 31 3... |
| user output |
|---|
| 2 3 5 7 11 13 17 19 23 29 31 3... |
