-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.js
84 lines (71 loc) · 2.04 KB
/
day10.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { parseLinesFromFile, writeAnswer } from './helpers.js';
function isSignalCycle(x) {
return x === 20 || (x - 20) % 40 === 0;
}
function addSignalStrenth(cycle, val, signals) {
if (isSignalCycle(cycle)) {
signals.push(cycle * val);
}
return signals;
}
function addPixel(cycle, val, pixels) {
pixels.push(getPixel(cycle, val));
return pixels;
}
function getPixel(cycle, x) {
const pos = (cycle - 1) % 40;
if (pos >= (x-1) && pos <= (x+1)) {
return '#';
}
return '.';
}
function generateState(input, updateFn) {
const state = input.reduce((all, instruction) => {
let startingCycle = all.cycle;
let nextCycle = instruction === 'noop' ? startingCycle + 1 : startingCycle + 2;
let nextX = all.x;
if (instruction !== 'noop') {
const [_, valStr] = instruction.split(' ');
const val = parseInt(valStr, 10);
nextX = all.x + val;
updateFn(startingCycle+1, all.x, all.results);
}
updateFn(nextCycle, all.x, all.results);
all.x = nextX;
all.cycle = nextCycle;
return all;
}, {
x: 1,
cycle: 0,
results: [],
});
return state.results;
}
function determineSignals(input) {
return generateState(input, addSignalStrenth);
}
function generateScreen(input) {
return generateState(input, addPixel);
}
function drawScreen(pixels) {
for (let i=0; i<pixels.length; i=i+40) {
console.log(pixels.slice(i, i+40).join(''));
}
}
function getAnswer(signals) {
return signals.reduce((cur, signal) => cur + signal, 0);
}
function solveProblem1(filename) {
const input = parseLinesFromFile(filename);
const signals = determineSignals(input);
const answer = getAnswer(signals);
writeAnswer(answer);
}
function solveProblem2(filename) {
const input = parseLinesFromFile(filename);
const pixels = generateScreen(input);
drawScreen(pixels);
}
const filename = './day10.puzzle.txt';
solveProblem1(filename);
solveProblem2(filename);