-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.js
119 lines (100 loc) · 2.83 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const fs = require('fs');
const day10a = () => {
let answer = 0;
// const filename = './data/day10test.txt'; // 13140
const filename = './data/day10.txt';
const data = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' }).split('\n');
// every addx operation takes 2 clock cycles so let's inject a no-op header.
let instructions = [];
let register = 1;
for (let item of data) {
if (item.includes('addx')) {
instructions.push('noop');
}
instructions.push(item);
}
let cycle = 0;
const arr = [20, 60, 100, 140, 180, 220];
while(instructions.length > 0) {
const op = instructions.shift();
const [command, valStr] = op.split(' ');
let val;
cycle += 1;
// read the data during the middle of the cycle,
// so we are interested in the register value from the previous cycle
if (arr.includes(cycle)) {
answer += (cycle * register);
}
switch (command) {
case 'noop': break;
case 'addx':
val = +valStr;
register += val;
break;
}
}
console.log('day10a = ', answer);
}
const day10b = () => {
let answer = 0;
//const filename = './data/day10test.txt'; // 13140
const filename = './data/day10.txt';
const data = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' }).split('\n');
// every addx operation takes 2 clock cycles so let's inject a no-op header.
let instructions = [];
let register = 1;
for (let item of data) {
if (item.includes('addx')) {
instructions.push('noop');
}
instructions.push(item);
}
// CRT: 40 wide and 6 high.
// to simplify, lets just use a 1-d array first.
const CRT = [
Array.from(Array(40).keys()).map(x => ' '),
Array.from(Array(40).keys()).map(x => ' '),
Array.from(Array(40).keys()).map(x => ' '),
Array.from(Array(40).keys()).map(x => ' '),
Array.from(Array(40).keys()).map(x => ' '),
Array.from(Array(40).keys()).map(x => ' '),
];
let cycle = 0;
let sprite = [0,1,2];
while(instructions.length > 0) {
const op = instructions.shift();
const [command, valStr] = op.split(' ');
let val;
cycle += 1;
// the cycle number is the location of the center of the 3-length sprite.
let column = (cycle-1)%40;
let row = Math.floor((cycle-1) / 40);
sprite = [column-1, column, column+1];
if (sprite.includes(register)) {
CRT[row][column] = '#';
}
switch (command) {
case 'noop': break;
case 'addx':
val = +valStr;
register += val;
break;
}
}
console.log('day10b = ');
prettyPrint(CRT);
}
// print into 40 x 6 CRT display.
const prettyPrint = (crt) => {
let strLine = '';
for (let line of crt) {
strLine = '';
for (let char of line) {
strLine += char;
}
console.log(strLine);
}
}
module.exports = {day10a, day10b};