24 lines
539 B
JavaScript
24 lines
539 B
JavaScript
|
|
||
|
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);
|
||
|
|
||
|
})();
|