Ben Ashton
1 year ago
2 changed files with 199 additions and 0 deletions
@ -0,0 +1,144 @@ |
|||||||
|
noop |
||||||
|
addx 5 |
||||||
|
noop |
||||||
|
addx 3 |
||||||
|
addx -2 |
||||||
|
addx 4 |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx 7 |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx 5 |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx 5 |
||||||
|
addx -10 |
||||||
|
addx 2 |
||||||
|
addx 14 |
||||||
|
noop |
||||||
|
addx -38 |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx 5 |
||||||
|
addx 2 |
||||||
|
addx 2 |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
addx 20 |
||||||
|
addx -19 |
||||||
|
addx 28 |
||||||
|
addx -21 |
||||||
|
addx 2 |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
addx 2 |
||||||
|
addx -4 |
||||||
|
addx 5 |
||||||
|
addx 2 |
||||||
|
addx -4 |
||||||
|
addx 11 |
||||||
|
addx -27 |
||||||
|
addx 28 |
||||||
|
addx -38 |
||||||
|
addx 5 |
||||||
|
addx 2 |
||||||
|
addx -1 |
||||||
|
noop |
||||||
|
addx 6 |
||||||
|
addx 3 |
||||||
|
addx -2 |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx 7 |
||||||
|
addx 2 |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx 5 |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
addx 2 |
||||||
|
addx -11 |
||||||
|
addx 6 |
||||||
|
addx 8 |
||||||
|
noop |
||||||
|
addx 3 |
||||||
|
addx -37 |
||||||
|
addx 1 |
||||||
|
addx 5 |
||||||
|
addx 2 |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx -5 |
||||||
|
addx 13 |
||||||
|
addx 2 |
||||||
|
addx -8 |
||||||
|
addx 9 |
||||||
|
addx 4 |
||||||
|
noop |
||||||
|
addx 5 |
||||||
|
addx -2 |
||||||
|
addx -14 |
||||||
|
addx 21 |
||||||
|
addx 1 |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx -38 |
||||||
|
addx 2 |
||||||
|
addx 5 |
||||||
|
addx 2 |
||||||
|
addx 3 |
||||||
|
addx -2 |
||||||
|
noop |
||||||
|
addx 11 |
||||||
|
addx -6 |
||||||
|
addx 5 |
||||||
|
addx 2 |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
addx 2 |
||||||
|
addx -10 |
||||||
|
addx 15 |
||||||
|
noop |
||||||
|
addx -24 |
||||||
|
addx 17 |
||||||
|
addx 10 |
||||||
|
noop |
||||||
|
addx 3 |
||||||
|
addx -38 |
||||||
|
addx 5 |
||||||
|
addx 2 |
||||||
|
addx 3 |
||||||
|
addx -2 |
||||||
|
addx 2 |
||||||
|
addx 7 |
||||||
|
addx 1 |
||||||
|
addx -1 |
||||||
|
noop |
||||||
|
addx 5 |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
noop |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
addx -21 |
||||||
|
addx 28 |
||||||
|
addx 1 |
||||||
|
noop |
||||||
|
addx 2 |
||||||
|
noop |
||||||
|
addx 3 |
||||||
|
noop |
||||||
|
noop |
@ -0,0 +1,55 @@ |
|||||||
|
import { readFileSync } from 'node:fs'; |
||||||
|
|
||||||
|
const input = readFileSync('input', 'utf-8'); |
||||||
|
|
||||||
|
const cycles = input |
||||||
|
.split('\n') |
||||||
|
.filter(line => line) |
||||||
|
.flatMap((() => { |
||||||
|
let x = 1; |
||||||
|
return (line) => { |
||||||
|
if (line.startsWith('noop')) { |
||||||
|
return [x]; |
||||||
|
} |
||||||
|
if (line.startsWith('addx')) { |
||||||
|
const add = parseInt(line.substring(5)); |
||||||
|
if (isNaN(add)) { |
||||||
|
throw new Error('Invalid value for addx'); |
||||||
|
} |
||||||
|
const result = [x, x]; |
||||||
|
x += add; |
||||||
|
return result; |
||||||
|
} |
||||||
|
}; |
||||||
|
})()); |
||||||
|
|
||||||
|
const sumOfSignalStrengths = [20, 60, 100, 140, 180, 220] |
||||||
|
.map(c => { |
||||||
|
const x = cycles[c - 1]; |
||||||
|
const strength = c * x; |
||||||
|
return strength; |
||||||
|
}) |
||||||
|
.reduce((total, strength) => total + strength); |
||||||
|
|
||||||
|
console.log( |
||||||
|
`The sum of observed signal strengths is: ${sumOfSignalStrengths}` |
||||||
|
); |
||||||
|
|
||||||
|
const render = cycles |
||||||
|
.map((x, c) => { |
||||||
|
const scanPos = c % 40; |
||||||
|
return [x - 1, x, x + 1].some(pixelPos => pixelPos === scanPos); |
||||||
|
}) |
||||||
|
.reduce((display, pixelVisible, c) => { |
||||||
|
const scanLine = Math.floor(c / 40); |
||||||
|
if (!display[scanLine]) { |
||||||
|
display.push([]); |
||||||
|
} |
||||||
|
display[scanLine].push(pixelVisible ? '#' : ' '); |
||||||
|
return display; |
||||||
|
}, []) |
||||||
|
.map(scanLine => scanLine.join('')) |
||||||
|
.join('\n'); |
||||||
|
|
||||||
|
console.log('Display output:'); |
||||||
|
console.log(render); |
Loading…
Reference in new issue