diff --git a/deno.json b/deno.json index 75abcd8..39cf29b 100644 --- a/deno.json +++ b/deno.json @@ -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" } } diff --git a/src/10_Cathode-RayTube/Cathode-RayTube.ts b/src/10_Cathode-RayTube/Cathode-RayTube.ts new file mode 100644 index 0000000..d80ac30 --- /dev/null +++ b/src/10_Cathode-RayTube/Cathode-RayTube.ts @@ -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): Array { + const instructions: Array = []; + 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, register = 0, valX = 1): Generator { + 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); +} diff --git a/src/10_Cathode-RayTube/input.txt b/src/10_Cathode-RayTube/input.txt new file mode 100644 index 0000000..6328cc8 --- /dev/null +++ b/src/10_Cathode-RayTube/input.txt @@ -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