2022 day 10

This commit is contained in:
Elizabeth Hunt 2023-12-01 18:46:40 -07:00
parent 7fd9576422
commit 4959d7df01
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
9 changed files with 449 additions and 2 deletions

5
README.md Normal file
View File

@ -0,0 +1,5 @@
```
bun install
source ./aoc
aoc
```

17
aoc
View File

@ -22,7 +22,7 @@ exec_test() {
exec_part() { exec_part() {
local logfile="logs/out_$1.txt" local logfile="logs/out_$1.txt"
bun run "part_$1.ts" | tee $logfile bun run "part_$1.ts" | tee $logfile
echo $(cat $logfile | tail -n 1) cat $logfile
} }
get_aoc_problem_path() { 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..." echo "Please copy your Advent of Code cookie to the clipboard, then press Enter..."
read -r read -r
echo "$(pastecmd)" > "$AOCCOOKIE" echo $($pastecmd) > "$AOCCOOKIE"
echo "Cookie saved to $AOCCOOKIE" echo "Cookie saved to $AOCCOOKIE"
fi fi
echo $(cat $AOCCOOKIE | tail -n 1) echo $(cat $AOCCOOKIE | tail -n 1)
@ -79,6 +79,7 @@ aoc() {
echo "+ aoc init <? year day>: initialize 'template/' to problem solution and" echo "+ aoc init <? year day>: initialize 'template/' to problem solution and"
echo " pull input to '$AOCINPUT' (today by default)" echo " pull input to '$AOCINPUT' (today by default)"
echo "+ aoc test <? year day>: run 'exec_test' in aoc problem (today by default)" echo "+ aoc test <? year day>: run 'exec_test' in aoc problem (today by default)"
echo "+ aoc exec <1 | 2> <? year day>: get the output of an aoc problem without submission"
echo "+ aoc submit <1 | 2> <? year day>: submit part one or part two to aoc problem (today by default)" echo "+ aoc submit <1 | 2> <? year day>: submit part one or part two to aoc problem (today by default)"
fi fi
@ -123,6 +124,18 @@ aoc() {
exec_test exec_test
fi 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 [[ $1 == "submit" ]]; then
if [[ 4 == $argc ]]; then if [[ 4 == $argc ]]; then
year=$3 year=$3

View File

@ -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);
});

View File

@ -0,0 +1,7 @@
=== COMPUTATION ===
=== /COMPUTATION ===
=== ANSWER TO P1 ===
13760

View File

@ -0,0 +1,12 @@
=== COMPUTATION ===
=== /COMPUTATION ===
=== ANSWER TO P2 ===
###..####.#..#.####..##..###..####.####.
#..#.#....#.#.....#.#..#.#..#.#....#....
#..#.###..##.....#..#....#..#.###..###..
###..#....#.#...#...#....###..#....#....
#.#..#....#.#..#....#..#.#....#....#....
#..#.#....#..#.####..##..#....####.#....

45
aoc_2022/day-10/part_1.ts Normal file
View File

@ -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);
}

59
aoc_2022/day-10/part_2.ts Normal file
View File

@ -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);
}

139
aoc_2022/day-10/problem.txt Normal file
View File

@ -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