summaryrefslogtreecommitdiff
path: root/aoc_2022
diff options
context:
space:
mode:
Diffstat (limited to 'aoc_2022')
-rw-r--r--aoc_2022/day-10/example.test.ts167
-rw-r--r--aoc_2022/day-10/logs/.gitkeep (renamed from aoc_2022/day-10/.gitkeep)0
-rw-r--r--aoc_2022/day-10/logs/out_1.txt7
-rw-r--r--aoc_2022/day-10/logs/out_2.txt12
-rw-r--r--aoc_2022/day-10/part_1.ts45
-rw-r--r--aoc_2022/day-10/part_2.ts59
-rw-r--r--aoc_2022/day-10/problem.txt139
7 files changed, 429 insertions, 0 deletions
diff --git a/aoc_2022/day-10/example.test.ts b/aoc_2022/day-10/example.test.ts
new file mode 100644
index 0000000..8624477
--- /dev/null
+++ b/aoc_2022/day-10/example.test.ts
@@ -0,0 +1,167 @@
+import { expect, test } from "bun:test";
+import { main as part1 } from "./part_1";
+import { main as part2 } from "./part_2";
+
+const example = `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`.split("\n");
+
+test("part1", async () => {
+ const answer = 13140;
+ const res = await part1(example);
+ expect(res).toEqual(answer);
+});
+
+test("part2", async () => {
+ const answer = `##..##..##..##..##..##..##..##..##..##..
+###...###...###...###...###...###...###.
+####....####....####....####....####....
+#####.....#####.....#####.....#####.....
+######......######......######......####
+#######.......#######.......#######.....`;
+ const res = await part2(example);
+ expect(res).toEqual(answer);
+});
diff --git a/aoc_2022/day-10/.gitkeep b/aoc_2022/day-10/logs/.gitkeep
index e69de29..e69de29 100644
--- a/aoc_2022/day-10/.gitkeep
+++ b/aoc_2022/day-10/logs/.gitkeep
diff --git a/aoc_2022/day-10/logs/out_1.txt b/aoc_2022/day-10/logs/out_1.txt
new file mode 100644
index 0000000..4ec0aaf
--- /dev/null
+++ b/aoc_2022/day-10/logs/out_1.txt
@@ -0,0 +1,7 @@
+=== COMPUTATION ===
+
+
+=== /COMPUTATION ===
+
+=== ANSWER TO P1 ===
+13760
diff --git a/aoc_2022/day-10/logs/out_2.txt b/aoc_2022/day-10/logs/out_2.txt
new file mode 100644
index 0000000..8ad8d82
--- /dev/null
+++ b/aoc_2022/day-10/logs/out_2.txt
@@ -0,0 +1,12 @@
+=== COMPUTATION ===
+
+
+=== /COMPUTATION ===
+
+=== ANSWER TO P2 ===
+###..####.#..#.####..##..###..####.####.
+#..#.#....#.#.....#.#..#.#..#.#....#....
+#..#.###..##.....#..#....#..#.###..###..
+###..#....#.#...#...#....###..#....#....
+#.#..#....#.#..#....#..#.#....#....#....
+#..#.#....#..#.####..##..#....####.#....
diff --git a/aoc_2022/day-10/part_1.ts b/aoc_2022/day-10/part_1.ts
new file mode 100644
index 0000000..02bcb47
--- /dev/null
+++ b/aoc_2022/day-10/part_1.ts
@@ -0,0 +1,45 @@
+const cycles = (instruction: string): number => {
+ if (instruction === "noop") return 1;
+ if (instruction === "addx") return 2;
+ return 0;
+};
+
+export const main = async (lines: string[]): Promise<number | string> => {
+ const instructions = lines.map((line) => line.split(" "));
+ let signalStrength = 0;
+ let cycle = 0;
+ const registers = { x: 1 };
+
+ for (const [instruction, operand] of instructions) {
+ const instCycles = cycles(instruction);
+ for (let i = 0; i < instCycles; i++) {
+ cycle++;
+ if (cycle >= 20 && (cycle - 20) % 40 === 0) {
+ signalStrength += registers.x * cycle;
+ }
+ }
+
+ if (instruction === "addx") {
+ registers.x += parseInt(operand);
+ }
+ }
+ return signalStrength;
+};
+
+//
+
+const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
+if (isrun) {
+ const file = Bun.file("./problem.txt");
+ const text = await file.text();
+ const lines = text.split("\n");
+
+ console.log("=== COMPUTATION ===\n");
+
+ const answer = await main(lines);
+
+ console.log("\n=== /COMPUTATION ===\n");
+
+ console.log("=== ANSWER TO P1 ===");
+ console.log(answer);
+}
diff --git a/aoc_2022/day-10/part_2.ts b/aoc_2022/day-10/part_2.ts
new file mode 100644
index 0000000..700ad19
--- /dev/null
+++ b/aoc_2022/day-10/part_2.ts
@@ -0,0 +1,59 @@
+const cycles = (instruction: string): number => {
+ if (instruction === "noop") return 1;
+ if (instruction === "addx") return 2;
+ return 0;
+};
+
+export const main = async (lines: string[]): Promise<number | string> => {
+ const instructions = lines.map((line) => line.split(" "));
+ let signalStrength = 0;
+ let cycle = 0;
+ const registers = { x: 1 };
+ const dim = { width: 40, height: 6 };
+
+ const crt = Array(dim.width * dim.height).fill("");
+
+ for (const [instruction, operand] of instructions) {
+ const instCycles = cycles(instruction);
+ for (let i = 0; i < instCycles; i++) {
+ const crtx = cycle % dim.width;
+ crt[cycle] = [registers.x - 1, registers.x, registers.x + 1].includes(
+ crtx
+ )
+ ? "#"
+ : ".";
+
+ cycle++;
+ if (cycle >= 20 && (cycle - 20) % 40 === 0) {
+ signalStrength += registers.x * cycle;
+ }
+ }
+
+ if (instruction === "addx") {
+ registers.x += parseInt(operand);
+ }
+ }
+
+ return Array(dim.height)
+ .fill(null)
+ .map((_, i) => crt.slice(dim.width * i, dim.width * (i + 1)).join(""))
+ .join("\n");
+};
+
+//
+
+const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
+if (isrun) {
+ const file = Bun.file("./problem.txt");
+ const text = await file.text();
+ const lines = text.split("\n");
+
+ console.log("=== COMPUTATION ===\n");
+
+ const answer = await main(lines);
+
+ console.log("\n=== /COMPUTATION ===\n");
+
+ console.log("=== ANSWER TO P2 ===");
+ console.log(answer);
+}
diff --git a/aoc_2022/day-10/problem.txt b/aoc_2022/day-10/problem.txt
new file mode 100644
index 0000000..008b1e7
--- /dev/null
+++ b/aoc_2022/day-10/problem.txt
@@ -0,0 +1,139 @@
+noop
+noop
+noop
+addx 5
+noop
+addx 1
+addx 2
+addx 5
+addx 2
+addx 1
+noop
+addx 5
+noop
+addx -1
+noop
+addx 5
+noop
+noop
+addx 5
+addx 1
+noop
+noop
+addx 3
+addx 2
+noop
+addx -38
+noop
+addx 3
+addx 2
+addx -5
+addx 12
+addx 2
+addx 27
+addx -40
+addx 19
+addx 2
+addx 19
+addx -18
+addx 2
+addx 5
+addx 2
+addx -23
+addx 22
+addx 4
+addx -34
+addx -1
+addx 5
+noop
+addx 2
+addx 1
+addx 20
+addx -17
+noop
+addx 25
+addx -17
+addx -2
+noop
+addx 3
+addx 19
+addx -12
+addx 3
+addx -2
+addx 3
+addx 1
+noop
+addx 5
+noop
+noop
+addx -37
+addx 3
+addx 4
+noop
+addx 24
+addx -6
+addx -15
+addx 2
+noop
+addx 6
+addx -2
+addx 6
+addx -12
+addx -2
+addx 19
+noop
+noop
+noop
+addx 3
+noop
+addx 7
+addx -2
+addx -24
+addx -11
+addx 4
+addx 3
+addx -2
+noop
+addx 7
+addx -2
+addx 2
+noop
+addx 3
+addx 7
+noop
+addx -2
+addx 5
+addx 2
+addx 5
+noop
+noop
+noop
+addx 3
+addx -35
+addx 35
+addx -21
+addx -14
+noop
+addx 5
+addx 2
+addx 33
+addx -7
+addx -23
+addx 5
+addx 2
+addx 1
+noop
+noop
+addx 5
+addx -1
+noop
+addx 3
+addx -23
+addx 30
+addx 1
+noop
+addx 4
+addx -17
+addx 11
+noop
+noop