day 10
This commit is contained in:
138
2022/10.input
Normal file
138
2022/10.input
Normal file
@@ -0,0 +1,138 @@
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -1
|
||||
addx 1
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
addx -1
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx -38
|
||||
addx 7
|
||||
addx 10
|
||||
addx -14
|
||||
addx 5
|
||||
addx 30
|
||||
addx -25
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 2
|
||||
addx -21
|
||||
addx 22
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx -39
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx 4
|
||||
addx -5
|
||||
addx 4
|
||||
addx 4
|
||||
noop
|
||||
addx -9
|
||||
addx 12
|
||||
addx 5
|
||||
addx 2
|
||||
addx -1
|
||||
addx 6
|
||||
addx -2
|
||||
noop
|
||||
addx 3
|
||||
addx 3
|
||||
addx 2
|
||||
addx -37
|
||||
addx 39
|
||||
addx -33
|
||||
addx -1
|
||||
addx 1
|
||||
addx 8
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 20
|
||||
addx -19
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 5
|
||||
noop
|
||||
addx 1
|
||||
addx 4
|
||||
addx -21
|
||||
addx 22
|
||||
addx -38
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 32
|
||||
addx -27
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx -39
|
||||
addx 2
|
||||
noop
|
||||
addx 4
|
||||
addx 8
|
||||
addx -8
|
||||
addx 6
|
||||
addx -1
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx 2
|
||||
addx -11
|
||||
addx 12
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 5
|
||||
addx -6
|
||||
noop
|
||||
54
2022/10.js
Normal file
54
2022/10.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const fs = require('fs');
|
||||
|
||||
const CPU = {
|
||||
signalIntegrity: 0,
|
||||
row: '',
|
||||
|
||||
clk: 0,
|
||||
X: 1,
|
||||
}
|
||||
|
||||
const microCode = {
|
||||
'noop': {
|
||||
c: 1,
|
||||
o: null,
|
||||
t: null,
|
||||
},
|
||||
'addx': {
|
||||
c: 2,
|
||||
o: '+',
|
||||
t: 'X',
|
||||
}
|
||||
}
|
||||
|
||||
for (const instruction of fs.readFileSync('10.input').toString().split('\n')) {
|
||||
const operator = instruction.split(' ')[0];
|
||||
const operand = instruction.split(' ')[1] ?? 0;
|
||||
const inst = microCode[operator];
|
||||
|
||||
for (let cycle = 0; cycle < inst.c; cycle++) {
|
||||
CPU.clk++;cycle
|
||||
if (CPU.clk % 40 == 20) CPU.signalIntegrity += CPU.clk * CPU.X;
|
||||
|
||||
const column = (CPU.clk - 1) % 40;
|
||||
CPU.row += CPU.X - 1 <= column && column <= CPU.X + 1 ? '█' : ' ';
|
||||
if(column == 39) {
|
||||
console.log(CPU.row);
|
||||
CPU.row = "";
|
||||
}
|
||||
|
||||
// operation is applied on the last cycle of the show
|
||||
if (cycle != inst.c - 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inst.t == 'X') {
|
||||
CPU.X = eval(`CPU.${inst.t} ${inst.o} ${operand}`);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Part 1: ${CPU.signalIntegrity}`);
|
||||
|
||||
|
||||
0
2022/11.input
Normal file
0
2022/11.input
Normal file
0
2022/11.js
Normal file
0
2022/11.js
Normal file
Reference in New Issue
Block a user