solutions for day 10

This commit is contained in:
Eric Wagoner 2022-12-10 00:51:44 -05:00
parent b69c454c4c
commit d5574c1167
3 changed files with 353 additions and 0 deletions

68
10/code.js Normal file
View File

@ -0,0 +1,68 @@
const fs = require('fs');
//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
// Part One
let x = 1;
let cycle = 0;
let samples = [];
for (i in inputArray) {
cycle++;
getSample();
const contents = inputArray[i].split(" ");
const instruction = contents[0];
if (instruction === 'addx') {
cycle++;
getSample();
x += parseInt(contents[1], 10);
}
}
const countedSamples = samples.slice(0, 7);
console.log(countedSamples.reduce((partialSum, a) => partialSum + a, 0));
// Part Two
x = 1;
cycle = 0;
let pixels = [];
for (i in inputArray) {
cycle++;
setPixel();
const contents = inputArray[i].split(" ");
const instruction = contents[0];
if (instruction === 'addx') {
cycle++;
setPixel();
x += parseInt(contents[1], 10);
}
}
const screen = [];
for (let i = 0; i < 6; i++) {
const line = pixels.slice(i * 40, (i + 1) * 40).join('');
screen.push(line)
}
console.log(screen);
// functions
function getSample() {
if (cycle === 20 || (cycle - 20) % 40 === 0) {
samples.push(cycle * x);
}
}
function setPixel() {
const position = cycle % 40;
if (position >= x && position <= x + 2) {
pixels.push('#');
} else {
pixels.push('.');
}
}

139
10/input.txt Normal file
View File

@ -0,0 +1,139 @@
noop
noop
noop
addx 5
noop
addx 1
addx 2
addx 5
addx 2
addx 1
noop
addx 5
noop
addx -1
noop
addx 5
noop
noop
addx 5
addx 1
noop
noop
addx 3
addx 2
noop
addx -38
noop
addx 3
addx 2
addx -5
addx 12
addx 2
addx 27
addx -40
addx 19
addx 2
addx 19
addx -18
addx 2
addx 5
addx 2
addx -23
addx 22
addx 4
addx -34
addx -1
addx 5
noop
addx 2
addx 1
addx 20
addx -17
noop
addx 25
addx -17
addx -2
noop
addx 3
addx 19
addx -12
addx 3
addx -2
addx 3
addx 1
noop
addx 5
noop
noop
addx -37
addx 3
addx 4
noop
addx 24
addx -6
addx -15
addx 2
noop
addx 6
addx -2
addx 6
addx -12
addx -2
addx 19
noop
noop
noop
addx 3
noop
addx 7
addx -2
addx -24
addx -11
addx 4
addx 3
addx -2
noop
addx 7
addx -2
addx 2
noop
addx 3
addx 7
noop
addx -2
addx 5
addx 2
addx 5
noop
noop
noop
addx 3
addx -35
addx 35
addx -21
addx -14
noop
addx 5
addx 2
addx 33
addx -7
addx -23
addx 5
addx 2
addx 1
noop
noop
addx 5
addx -1
noop
addx 3
addx -23
addx 30
addx 1
noop
addx 4
addx -17
addx 11
noop
noop

146
10/sample.txt Normal file
View File

@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop