diff --git a/README.md b/README.md new file mode 100644 index 0000000..e2af1e8 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +``` +bun install +source ./aoc +aoc +``` diff --git a/aoc b/aoc index 51e6da8..f4c56a0 100755 --- a/aoc +++ b/aoc @@ -22,7 +22,7 @@ exec_test() { exec_part() { local logfile="logs/out_$1.txt" bun run "part_$1.ts" | tee $logfile - echo $(cat $logfile | tail -n 1) + cat $logfile } get_aoc_problem_path() { @@ -62,7 +62,7 @@ get_aoc_cookie() { echo "Please copy your Advent of Code cookie to the clipboard, then press Enter..." read -r - echo "$(pastecmd)" > "$AOCCOOKIE" + echo $($pastecmd) > "$AOCCOOKIE" echo "Cookie saved to $AOCCOOKIE" fi echo $(cat $AOCCOOKIE | tail -n 1) @@ -79,6 +79,7 @@ aoc() { echo "+ aoc init : initialize 'template/' to problem solution and" echo " pull input to '$AOCINPUT' (today by default)" echo "+ aoc test : run 'exec_test' in aoc problem (today by default)" + echo "+ aoc exec <1 | 2> : get the output of an aoc problem without submission" echo "+ aoc submit <1 | 2> : submit part one or part two to aoc problem (today by default)" fi @@ -123,6 +124,18 @@ aoc() { exec_test fi + if [[ $1 == "exec" ]]; then + if [[ 4 == $argc ]]; then + year=$3 + day=$4 + curr=$(get_aoc_problem_path $year $day) + fi + cd $curr + + local level=$2 + exec_part $level + fi + if [[ $1 == "submit" ]]; then if [[ 4 == $argc ]]; then year=$3 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 similarity index 100% rename from aoc_2022/day-10/.gitkeep rename to 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 => { + 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 => { + 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