10.1 and 10.2

This commit is contained in:
John Shaver 2022-12-10 09:28:13 -08:00
parent 35fb3ba9b1
commit cd6e5dc535
3 changed files with 189 additions and 0 deletions

23
2022/10/index.js Normal file
View file

@ -0,0 +1,23 @@
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);
})();

22
2022/10/index2.js Normal file
View file

@ -0,0 +1,22 @@
const fs = require('fs/promises');
(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));
const cycleValues = simplified.reduce((vals, change, i) => {
vals.push(vals[i] + change);
return vals;
}, [1]);
console.log(cycleValues);
while(cycleValues.length) {
const row = cycleValues.splice(0, 40);
console.log(row.map((pos, i) =>
pos >= i - 1 && pos <= i + 1 ? "X" : "."
).join(""))
}
})();

144
2022/10/input.txt Normal file
View file

@ -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