-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.ts
61 lines (49 loc) · 1.76 KB
/
b.ts
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
import { assertEquals } from "https://deno.land/std@0.167.0/testing/asserts.ts";
function solution(input: string) {
const lines = input.split("\n");
const ops = [];
let valueOfRegister = 1;
for (let i = 0; i < lines.length; i++) {
ops.push(0);
const [command, arg] = lines[i].split(" ");
if (command === "addx") {
ops.push(Number(arg));
}
}
const image = Array.from(
{ length: 6 },
() => Array.from({ length: 40 }, () => "."),
);
ops.forEach((n, index) => {
const [x, y] = [index % 40, Math.floor(index / 40)];
const havePixel = x === (valueOfRegister - 1) || x === valueOfRegister ||
x === (valueOfRegister + 1);
if (havePixel) {
image[y][x] = "#";
}
valueOfRegister += n;
});
return image.map((line) => line.join("")).join("\n");
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./10/example.txt");
const actual = solution(input);
const expected = "##..##..##..##..##..##..##..##..##..##..\n" +
"###...###...###...###...###...###...###.\n" +
"####....####....####....####....####....\n" +
"#####.....#####.....#####.....#####.....\n" +
"######......######......######......####\n" +
"#######.......#######.......#######.....";
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./10/input.txt");
const actual = solution(input);
const expected = "####.#..#.###..####.###....##..##..#....\n" +
"#....#..#.#..#....#.#..#....#.#..#.#....\n" +
"###..####.#..#...#..#..#....#.#....#....\n" +
"#....#..#.###...#...###.....#.#.##.#....\n" +
"#....#..#.#....#....#....#..#.#..#.#....\n" +
"####.#..#.#....####.#.....##...###.####.";
assertEquals(actual, expected);
});