advent-of-code/2022/10/index.js

24 lines
539 B
JavaScript
Raw Normal View History

2022-12-10 17:28:13 +00:00
const fs = require('fs/promises');
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
(async () => {
const input = await fs.readFile("./input.txt", 'utf8');
const simplified = input.replace(/noop/g, '0').replace(/addx (.*)/g, '0\n$1').split("\n").map(s => parseFloat(s));
simplified.unshift(1);
const targets = [20, 60, 100, 140, 180, 220];
const cycles = targets.map(t => simplified.slice(0, t));
console.log(cycles);
const total = sum(cycles.map(c => sum(c) * c.length));
console.log(total);
})();