-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.js
39 lines (36 loc) · 1.15 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
console.log('\n\n\n\n###################################3');
import { readLines } from 'https://deno.land/[email protected]/io/bufio.ts';
import { range } from 'https://deno.land/x/[email protected]/range.mjs';
import { slidingWindows } from 'https://deno.land/[email protected]/collections/mod.ts';
import count from 'https://deno.land/x/[email protected]/src/collection/count.ts';
import { writeAllSync } from 'https://deno.land/std/streams/conversion.ts';
let input = await Deno.readTextFile('./resources/input10_demo.txt');
input = await Deno.readTextFile('./resources/input10.txt');
// input = `noop
// addx 3
// addx -5`;
const prg = input
.trim()
.split('\n')
.map((s) => s.trim().split(' '));
let ops = [];
for (let [i, v] of prg) {
if (i === 'addx') {
ops.push(['noop']);
ops.push(['addx', parseInt(v, 10)]);
}
if (i === 'noop') ops.push(['noop']);
}
console.log({ prg, ops });
const reg = { X: 1 };
let res = 0;
let cycle = 0;
for (let [i, v] of ops) {
cycle++;
if ([20, 60, 100, 140, 180, 220].includes(cycle)) {
console.log({ cycle, reg }, cycle * reg.X);
res += cycle * reg.X;
}
if (i === 'addx') reg.X += v;
}
console.log({ reg, res });