Day 10
This commit is contained in:
parent
d74ab6cf5a
commit
8ca6967bb4
3 changed files with 202 additions and 1 deletions
|
@ -33,6 +33,7 @@
|
|||
"Day06": "cd ./src/06_TuningTrouble && deno run --allow-read=./input.txt TuningTrouble.ts",
|
||||
"Day07": "cd ./src/07_NoSpaceLeftOnDevice && deno run --allow-read=./input.txt NoSpaceLeftOnDevice.ts",
|
||||
"Day08": "cd ./src/08_TreetopTreeHouse && deno run --allow-read=./input.txt TreetopTreeHouse.ts",
|
||||
"Day09": "cd ./src/09_RopeBridge && deno run --allow-read=./input.txt RopeBridge.ts"
|
||||
"Day09": "cd ./src/09_RopeBridge && deno run --allow-read=./input.txt RopeBridge.ts",
|
||||
"Day10": "cd ./src/10_Cathode-RayTube && deno run --allow-read=./input.txt Cathode-RayTube.ts"
|
||||
}
|
||||
}
|
||||
|
|
60
src/10_Cathode-RayTube/Cathode-RayTube.ts
Normal file
60
src/10_Cathode-RayTube/Cathode-RayTube.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
const input = Deno.readTextFileSync('./input.txt').trimEnd().split('\n');
|
||||
|
||||
type State = { register: number; valX: number };
|
||||
|
||||
/**
|
||||
* Returns Instruction list
|
||||
* n=0: noop
|
||||
* n!=0: addx n
|
||||
*/
|
||||
function getInstructions(input: Array<string>): Array<number> {
|
||||
const instructions: Array<number> = [];
|
||||
for (const line of input) {
|
||||
const [type, value] = line.split(' ');
|
||||
if (type === 'noop') {
|
||||
instructions.push(0);
|
||||
} else instructions.push(parseInt(value));
|
||||
}
|
||||
return instructions;
|
||||
}
|
||||
|
||||
function getScreenChar(state: State): string {
|
||||
let screenPos = (state.register % 40);
|
||||
// Fix line wrap caused by modulo (as index starts at 1)
|
||||
if (screenPos === 0) screenPos = 40;
|
||||
|
||||
if (screenPos >= state.valX && screenPos <= state.valX + 2) {
|
||||
return '#';
|
||||
}
|
||||
return '.';
|
||||
}
|
||||
|
||||
function* runCycles(instructions: Array<number>, register = 0, valX = 1): Generator<State> {
|
||||
for (const instruction of instructions) {
|
||||
register++;
|
||||
yield { register: register, valX: valX };
|
||||
if (instruction === 0) continue;
|
||||
register++;
|
||||
yield { register: register, valX: valX };
|
||||
valX += instruction;
|
||||
}
|
||||
}
|
||||
|
||||
const instructions = getInstructions(input);
|
||||
const checkCycles = new Set([20, 60, 100, 140, 180, 220]);
|
||||
let p1 = 0;
|
||||
let screen = '';
|
||||
for (const state of runCycles(instructions)) {
|
||||
// Relevant cycles Solution Part 1
|
||||
if (checkCycles.has(state.register)) {
|
||||
p1 += state.register * state.valX;
|
||||
}
|
||||
// Generate screen char for Part 2
|
||||
screen += getScreenChar(state);
|
||||
}
|
||||
|
||||
console.log('Solution Part 1:', p1);
|
||||
console.log('Solution Part 2:');
|
||||
for (const line of screen.match(/.{40}/g)!) {
|
||||
console.log(line);
|
||||
}
|
140
src/10_Cathode-RayTube/input.txt
Normal file
140
src/10_Cathode-RayTube/input.txt
Normal file
|
@ -0,0 +1,140 @@
|
|||
addx 1
|
||||
noop
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -21
|
||||
addx 26
|
||||
addx -6
|
||||
addx 8
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -37
|
||||
addx 13
|
||||
addx -6
|
||||
addx -2
|
||||
addx 5
|
||||
addx 25
|
||||
addx 2
|
||||
addx -24
|
||||
addx 2
|
||||
addx 5
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 7
|
||||
addx -2
|
||||
noop
|
||||
addx -8
|
||||
addx 9
|
||||
addx -36
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 6
|
||||
noop
|
||||
addx 25
|
||||
addx -24
|
||||
addx 3
|
||||
addx -2
|
||||
noop
|
||||
addx 3
|
||||
addx 6
|
||||
noop
|
||||
addx 9
|
||||
addx -8
|
||||
addx 5
|
||||
addx 2
|
||||
addx -7
|
||||
noop
|
||||
addx 12
|
||||
addx -10
|
||||
addx 11
|
||||
addx -38
|
||||
addx 22
|
||||
addx -15
|
||||
addx -3
|
||||
noop
|
||||
addx 32
|
||||
addx -25
|
||||
addx -7
|
||||
addx 11
|
||||
addx 5
|
||||
addx 10
|
||||
addx -9
|
||||
addx 17
|
||||
addx -12
|
||||
addx 2
|
||||
noop
|
||||
addx 2
|
||||
addx -15
|
||||
addx 22
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -35
|
||||
addx 7
|
||||
addx 21
|
||||
addx -25
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 9
|
||||
addx -4
|
||||
addx -2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -2
|
||||
noop
|
||||
addx 7
|
||||
addx 2
|
||||
addx -39
|
||||
addx 2
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 5
|
||||
addx 24
|
||||
addx -20
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
noop
|
Loading…
Reference in a new issue