d10
This commit is contained in:
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"typescript.tsdk": "node_modules/typescript/lib"
|
||||
}
|
||||
@@ -9,6 +9,6 @@
|
||||
"typescript": "^4.9.3"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "nodemon src/2022/day9/index.ts"
|
||||
"start": "nodemon src/2022/day10/index.ts"
|
||||
}
|
||||
}
|
||||
|
||||
146
src/2022/day10/example.txt
Normal file
146
src/2022/day10/example.txt
Normal 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
|
||||
61
src/2022/day10/index.ts
Normal file
61
src/2022/day10/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import path from 'path';
|
||||
import { readFile, sum } from "../../utils";
|
||||
|
||||
let input = readFile(path.resolve(__dirname, 'input.txt'));
|
||||
|
||||
let reg: number[] = [1];
|
||||
|
||||
input.split("\n").forEach(line => {
|
||||
const [instruction, value] = line.split(" ");
|
||||
|
||||
let addme: number[] = [];
|
||||
let base = reg[reg.length - 1];
|
||||
if (instruction === "addx") {
|
||||
addme.push(base);
|
||||
addme.push(base + Number(value));
|
||||
reg = reg.concat(addme)
|
||||
} else if (instruction === "noop") {
|
||||
reg = reg.concat([base]);
|
||||
}
|
||||
});
|
||||
|
||||
const cycles = [20, 60, 100, 140, 180, 220];
|
||||
|
||||
const p1 = cycles.map(cycle => cycle * reg[cycle - 1]).reduce(sum);
|
||||
console.log("Part1:", p1);
|
||||
|
||||
|
||||
function sliceIntoChunks(arr, chunkSize) {
|
||||
const res: any[] = [];
|
||||
for (let i = 0; i < arr.length; i += chunkSize) {
|
||||
const chunk = arr.slice(i, i + chunkSize);
|
||||
res.push(chunk);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function isPixel(register, cycle) {
|
||||
const range = register - (cycle % 40);
|
||||
return (range <= 0 && range >= -2) ? "#" : ".";
|
||||
}
|
||||
|
||||
const CRT = Array(40 * 5).fill(".");
|
||||
let register = 1;
|
||||
let cycle = 0;
|
||||
|
||||
input.split("\n").forEach((line) => {
|
||||
const [instruction, value] = line.split(" ");
|
||||
|
||||
CRT[cycle] = isPixel(register, ++cycle);
|
||||
|
||||
if (instruction === "addx") {
|
||||
CRT[cycle] = isPixel(register, ++cycle);
|
||||
register = register + Number(value);
|
||||
}
|
||||
})
|
||||
|
||||
//console.log(CRT);
|
||||
console.log(sliceIntoChunks(CRT, 40).map(line => line.join("")));
|
||||
|
||||
145
src/2022/day10/input.txt
Normal file
145
src/2022/day10/input.txt
Normal file
@@ -0,0 +1,145 @@
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 6
|
||||
noop
|
||||
addx -1
|
||||
noop
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 6
|
||||
addx -1
|
||||
noop
|
||||
addx 3
|
||||
addx -13
|
||||
addx -22
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 11
|
||||
addx -4
|
||||
addx 11
|
||||
addx -10
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -2
|
||||
noop
|
||||
addx 7
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -2
|
||||
addx -8
|
||||
addx -27
|
||||
addx 5
|
||||
addx 2
|
||||
addx 21
|
||||
addx -21
|
||||
addx 3
|
||||
addx 5
|
||||
addx 2
|
||||
addx -3
|
||||
addx 4
|
||||
addx 3
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 1
|
||||
addx 6
|
||||
addx -31
|
||||
noop
|
||||
addx -4
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 7
|
||||
noop
|
||||
addx -1
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx -8
|
||||
addx 15
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -28
|
||||
addx 11
|
||||
addx -20
|
||||
noop
|
||||
addx 7
|
||||
addx -2
|
||||
addx 7
|
||||
noop
|
||||
addx -2
|
||||
noop
|
||||
addx -6
|
||||
addx 11
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -16
|
||||
addx -10
|
||||
addx -11
|
||||
addx 27
|
||||
addx -20
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx -14
|
||||
addx 21
|
||||
noop
|
||||
addx -6
|
||||
addx 12
|
||||
noop
|
||||
addx -21
|
||||
addx 24
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
Reference in New Issue
Block a user