repl.it
Nodejs

No description

fork
loading
Files
  • index.js
  • input.txt
  • part1.js
  • part2.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const fs = require("fs");
const text = fs.readFileSync("./input.txt", "utf-8");

let memory = text.split(',').map(opcode => parseInt(opcode));

memory[1] = 12;
memory[2] = 2;

for (let i = 0; i < memory.length; i+=4) {
	if (memory[i] == 1) {
		memory[memory[i+3]] = memory[memory[i+1]] + memory[memory[i+2]];
	} else if (memory[i] == 2) {
		memory[memory[i+3]] = memory[memory[i+1]] * memory[memory[i+2]];
	} else if (memory[i] == 99) {
		break;
	}
}

console.log(memory[0]);
?