day 2
This commit is contained in:
parent
4959d7df01
commit
f76bfc2d78
21
aoc_2023/day-02/example.test.ts
Normal file
21
aoc_2023/day-02/example.test.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { main as part1 } from "./part_1";
|
||||
import { main as part2 } from "./part_2";
|
||||
|
||||
const example = `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`.split("\n");
|
||||
|
||||
test("part1", async () => {
|
||||
const answer = 8;
|
||||
const res = await part1(example);
|
||||
expect(res).toEqual(answer);
|
||||
});
|
||||
|
||||
test("part2", async () => {
|
||||
const answer = 2286;
|
||||
const res = await part2(example);
|
||||
expect(res).toEqual(answer);
|
||||
});
|
0
aoc_2023/day-02/logs/.gitkeep
Normal file
0
aoc_2023/day-02/logs/.gitkeep
Normal file
7
aoc_2023/day-02/logs/out_1.txt
Normal file
7
aoc_2023/day-02/logs/out_1.txt
Normal file
@ -0,0 +1,7 @@
|
||||
=== COMPUTATION ===
|
||||
|
||||
|
||||
=== /COMPUTATION ===
|
||||
|
||||
=== ANSWER TO P1 ===
|
||||
2285
|
7
aoc_2023/day-02/logs/out_2.txt
Normal file
7
aoc_2023/day-02/logs/out_2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
=== COMPUTATION ===
|
||||
|
||||
|
||||
=== /COMPUTATION ===
|
||||
|
||||
=== ANSWER TO P2 ===
|
||||
77021
|
50
aoc_2023/day-02/part_1.ts
Normal file
50
aoc_2023/day-02/part_1.ts
Normal file
@ -0,0 +1,50 @@
|
||||
export const main = async (lines: string[]): Promise<number | string> => {
|
||||
const colors = new Map<string, number>([
|
||||
["red", 12],
|
||||
["green", 13],
|
||||
["blue", 14],
|
||||
]);
|
||||
|
||||
const games = lines
|
||||
.filter((x) => x)
|
||||
.map((line) => line.split(":").at(-1) as string)
|
||||
.map((line) => line.split(";").map((x) => x.trim()));
|
||||
|
||||
const unplayableGames = new Set<number>();
|
||||
|
||||
for (let i = 0; i < games.length; i++) {
|
||||
const game = games[i];
|
||||
for (const play of game) {
|
||||
const sets = play.split(",").map((x) => x.trim());
|
||||
for (const [num, color] of sets.map((s) => s.split(" "))) {
|
||||
const totalColors = colors.get(color) ?? 0;
|
||||
if (parseInt(num) > totalColors) {
|
||||
unplayableGames.add(i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const n = games.length;
|
||||
const perfect = (n * (n + 1)) / 2;
|
||||
|
||||
return perfect - Array.from(unplayableGames).reduce((acc, x) => acc + x, 0);
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
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);
|
||||
}
|
48
aoc_2023/day-02/part_2.ts
Normal file
48
aoc_2023/day-02/part_2.ts
Normal file
@ -0,0 +1,48 @@
|
||||
export const main = async (lines: string[]): Promise<number | string> => {
|
||||
const games = lines
|
||||
.filter((x) => x)
|
||||
.map((line) => line.split(":").at(-1) as string)
|
||||
.map((line) => line.split(";").map((x) => x.trim()));
|
||||
|
||||
let sum = 0;
|
||||
for (let i = 0; i < games.length; i++) {
|
||||
const game = games[i];
|
||||
|
||||
const mins = new Map<string, number>([
|
||||
["red", 0],
|
||||
["green", 0],
|
||||
["blue", 0],
|
||||
]);
|
||||
|
||||
for (const play of game) {
|
||||
const sets = play.split(",").map((x) => x.trim());
|
||||
for (const [num, color] of sets.map((s) => s.split(" "))) {
|
||||
mins.set(color, Math.max(mins.get(color)!, parseInt(num)));
|
||||
}
|
||||
}
|
||||
|
||||
sum += Array.from(mins.keys())
|
||||
.map((color) => mins.get(color)!)
|
||||
.reduce((acc, x) => acc * x, 1);
|
||||
}
|
||||
|
||||
return sum;
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
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);
|
||||
}
|
100
aoc_2023/day-02/problem.txt
Normal file
100
aoc_2023/day-02/problem.txt
Normal file
@ -0,0 +1,100 @@
|
||||
Game 1: 4 green, 7 blue; 2 blue, 4 red; 5 blue, 2 green, 2 red; 1 green, 3 red, 9 blue; 3 green, 9 blue; 7 green, 2 blue, 2 red
|
||||
Game 2: 1 blue, 2 red; 1 green, 2 blue, 1 red; 1 red, 5 green; 3 red, 2 blue, 8 green; 3 blue, 2 red, 4 green; 2 blue, 4 green, 3 red
|
||||
Game 3: 7 red, 7 blue, 9 green; 15 green, 4 red, 8 blue; 3 green, 12 blue, 6 red
|
||||
Game 4: 4 blue, 11 green, 6 red; 4 green, 2 red; 12 red, 1 blue, 3 green
|
||||
Game 5: 10 green, 4 blue, 9 red; 3 green, 15 blue, 11 red; 15 blue, 1 green, 2 red; 8 red, 8 blue, 5 green
|
||||
Game 6: 5 green, 19 red; 6 green, 13 red, 2 blue; 2 blue, 16 red, 4 green; 13 red, 9 blue, 5 green
|
||||
Game 7: 1 blue, 6 red, 6 green; 7 blue, 4 red; 6 green, 1 red, 11 blue; 3 green, 4 blue, 4 red; 6 green, 13 blue, 11 red
|
||||
Game 8: 8 green, 2 blue; 20 green, 1 red; 1 blue, 6 red, 6 green; 9 green
|
||||
Game 9: 5 red; 4 green, 3 red, 1 blue; 1 blue; 6 red, 1 blue, 9 green
|
||||
Game 10: 2 green, 3 red; 18 blue, 20 green, 9 red; 7 red, 9 blue, 17 green
|
||||
Game 11: 15 green, 7 blue, 9 red; 7 blue, 10 green, 7 red; 5 red, 3 blue, 10 green; 5 blue, 12 green; 14 green, 8 blue, 5 red; 7 blue, 2 red, 5 green
|
||||
Game 12: 3 green, 1 red, 8 blue; 9 blue, 3 red, 3 green; 4 blue, 1 green; 2 red, 3 green, 1 blue; 4 red, 7 blue, 3 green
|
||||
Game 13: 14 green, 1 red; 4 green; 2 green, 1 blue; 14 green; 13 green, 1 red, 1 blue; 1 blue, 1 red, 5 green
|
||||
Game 14: 1 blue, 6 red, 13 green; 5 red, 10 blue, 3 green; 19 green, 1 red, 14 blue; 4 red, 17 green, 9 blue; 12 green, 10 blue, 7 red
|
||||
Game 15: 10 red, 2 blue, 18 green; 17 green, 3 blue, 7 red; 18 blue, 8 red, 12 green; 6 blue, 6 green, 12 red
|
||||
Game 16: 14 blue, 5 green, 12 red; 7 green, 3 red, 9 blue; 4 green, 1 red, 8 blue; 9 red, 19 green, 12 blue; 12 blue, 7 red, 6 green; 5 blue, 3 green, 6 red
|
||||
Game 17: 1 green, 1 blue, 15 red; 1 blue, 3 green, 12 red; 9 blue, 2 green, 10 red
|
||||
Game 18: 12 red, 7 green, 7 blue; 3 blue, 8 red, 1 green; 2 green, 17 red
|
||||
Game 19: 1 red, 7 blue, 17 green; 11 red, 15 blue; 11 blue, 18 green; 6 blue, 14 green, 14 red; 16 blue, 8 red, 8 green; 17 green, 9 red, 1 blue
|
||||
Game 20: 11 green; 1 blue, 4 green, 7 red; 7 green; 3 red, 1 blue, 6 green
|
||||
Game 21: 18 blue, 6 green, 10 red; 12 blue; 9 blue, 2 green, 9 red; 9 red, 20 blue, 1 green; 8 blue, 6 red; 19 blue, 1 green, 4 red
|
||||
Game 22: 4 blue, 16 red, 3 green; 7 blue, 3 green, 12 red; 10 red, 7 green, 10 blue; 7 red, 11 blue, 4 green; 3 blue, 1 green, 16 red
|
||||
Game 23: 5 green, 8 red, 1 blue; 2 red, 5 blue, 3 green; 2 green, 17 blue, 4 red; 2 blue, 2 red; 7 red, 1 green, 14 blue; 4 red, 8 blue
|
||||
Game 24: 1 blue, 3 green, 1 red; 3 blue, 11 green, 15 red; 3 blue, 2 red, 12 green; 9 green, 6 red, 2 blue; 15 green, 12 red, 3 blue; 13 green, 1 blue, 13 red
|
||||
Game 25: 6 blue, 5 red, 10 green; 9 red, 3 blue, 3 green; 6 blue, 11 red, 15 green; 7 green, 10 red, 4 blue; 2 red, 20 blue, 11 green
|
||||
Game 26: 3 blue, 5 red, 10 green; 7 green, 6 red; 7 green, 1 red, 3 blue; 10 green, 4 red, 3 blue; 4 red, 7 green, 3 blue; 8 green, 4 red
|
||||
Game 27: 16 green, 6 blue, 4 red; 3 red, 6 blue, 7 green; 10 green; 6 green, 1 red
|
||||
Game 28: 5 green, 5 red, 2 blue; 1 blue, 9 red, 6 green; 2 blue, 3 red; 1 blue, 1 green, 5 red; 4 green, 3 red; 9 green, 1 blue, 14 red
|
||||
Game 29: 1 red, 2 green, 13 blue; 1 green, 2 red, 9 blue; 12 red; 3 blue, 5 red
|
||||
Game 30: 8 blue, 3 red, 9 green; 10 green, 9 blue; 9 green, 12 blue; 3 blue, 2 red, 4 green; 8 blue, 9 green; 1 red, 12 blue, 6 green
|
||||
Game 31: 8 red, 16 blue; 2 red, 1 green, 1 blue; 5 red, 8 blue, 1 green
|
||||
Game 32: 3 green, 5 blue, 8 red; 10 blue, 1 red, 3 green; 9 green, 3 blue, 2 red; 2 blue, 1 red, 14 green; 3 blue, 10 red, 16 green
|
||||
Game 33: 1 red, 3 green, 2 blue; 15 blue, 1 green; 1 green, 1 red, 10 blue
|
||||
Game 34: 4 green, 7 blue; 2 blue, 12 green; 6 red, 14 green, 7 blue
|
||||
Game 35: 9 blue, 1 green; 2 green, 6 blue, 11 red; 1 green, 10 red, 1 blue
|
||||
Game 36: 5 blue, 1 green, 2 red; 11 blue, 3 green, 5 red; 2 green, 14 blue, 2 red; 3 green, 5 blue, 5 red; 13 blue, 2 green, 5 red; 3 green
|
||||
Game 37: 4 blue, 2 red, 8 green; 1 blue, 9 green, 4 red; 1 red, 4 green, 1 blue; 16 green, 3 blue, 4 red
|
||||
Game 38: 9 blue, 9 green; 1 green, 3 blue; 8 blue, 6 red, 5 green; 1 green, 9 red, 1 blue
|
||||
Game 39: 1 red, 17 green; 1 blue, 7 green, 7 red; 6 red, 4 blue
|
||||
Game 40: 1 blue, 2 red; 10 blue, 4 green, 2 red; 1 green, 11 blue, 3 red; 4 blue, 2 green; 3 blue, 4 red
|
||||
Game 41: 7 red, 4 blue, 4 green; 10 red, 11 blue, 1 green; 6 red, 6 blue, 4 green; 13 blue, 3 red, 7 green; 9 green, 12 blue, 14 red; 9 blue, 12 red, 10 green
|
||||
Game 42: 3 blue, 1 red, 11 green; 4 blue, 9 green, 8 red; 3 red, 5 blue, 1 green
|
||||
Game 43: 2 green, 17 blue, 9 red; 16 red, 12 blue, 2 green; 12 red, 12 blue, 7 green; 17 red, 16 blue, 7 green
|
||||
Game 44: 2 red, 3 green, 5 blue; 5 red, 5 blue, 7 green; 2 red, 5 blue, 5 green; 6 red, 5 blue, 2 green
|
||||
Game 45: 9 green, 1 blue; 1 red, 5 green, 2 blue; 2 blue, 4 green, 9 red; 13 green, 7 red, 1 blue; 3 blue, 4 green
|
||||
Game 46: 5 green, 7 red; 8 green, 5 blue, 1 red; 1 blue, 7 red, 17 green
|
||||
Game 47: 1 green, 17 blue; 9 blue, 1 green; 1 blue, 1 red
|
||||
Game 48: 1 red, 6 green, 7 blue; 9 green, 1 red, 2 blue; 10 blue, 6 green, 1 red; 1 red, 4 green, 9 blue; 6 blue, 3 green, 1 red
|
||||
Game 49: 1 red, 1 blue, 16 green; 3 red, 1 green; 16 green; 2 blue, 8 red, 19 green; 20 green, 9 red; 8 green, 6 red
|
||||
Game 50: 1 green, 2 blue; 2 red, 3 blue; 4 red, 2 blue; 1 green; 3 blue; 3 blue, 2 green, 1 red
|
||||
Game 51: 6 blue, 5 green; 6 red, 5 green; 6 green, 6 blue, 16 red; 10 red, 10 green, 1 blue
|
||||
Game 52: 4 red, 5 green, 1 blue; 15 green, 1 blue; 8 green, 5 blue, 5 red; 4 blue, 11 green, 1 red
|
||||
Game 53: 5 green, 3 blue, 5 red; 2 red, 4 blue, 1 green; 1 red, 2 green; 11 red
|
||||
Game 54: 7 green, 16 blue, 5 red; 5 green; 10 blue, 6 green, 5 red; 3 green
|
||||
Game 55: 3 green; 16 green, 1 blue; 13 green, 19 blue, 1 red; 13 green, 18 blue
|
||||
Game 56: 9 green; 3 blue, 1 red, 10 green; 1 red, 4 blue, 9 green
|
||||
Game 57: 14 blue, 2 red, 3 green; 1 red, 8 blue, 7 green; 1 green, 3 red, 15 blue; 5 green, 12 blue; 4 green, 15 blue
|
||||
Game 58: 9 red, 5 green; 10 green, 11 red, 1 blue; 12 green, 17 red, 1 blue; 1 blue, 1 green, 17 red; 14 red, 1 blue, 16 green
|
||||
Game 59: 2 red, 12 blue, 10 green; 6 green, 1 red, 14 blue; 14 blue, 9 green, 2 red; 12 green, 14 blue, 2 red
|
||||
Game 60: 5 blue, 8 green; 1 red, 6 green, 7 blue; 1 blue
|
||||
Game 61: 5 red, 2 blue, 5 green; 2 blue, 11 green; 1 blue, 2 red, 14 green; 3 green; 4 red, 13 green; 2 blue, 6 green, 1 red
|
||||
Game 62: 1 red, 1 blue, 2 green; 3 red, 1 blue, 2 green; 1 blue, 10 red; 6 red, 1 blue
|
||||
Game 63: 7 red, 6 blue, 4 green; 2 blue, 5 green, 8 red; 5 blue, 4 green, 10 red; 4 blue, 7 red, 10 green; 5 blue, 10 green, 8 red; 4 blue, 10 green, 3 red
|
||||
Game 64: 6 red, 7 green, 15 blue; 8 blue, 16 green, 3 red; 11 green, 12 blue; 4 red, 17 blue, 8 green
|
||||
Game 65: 2 blue, 7 green; 2 red, 8 blue; 2 green, 1 red, 5 blue; 1 green, 2 blue; 19 green, 7 blue; 2 red, 3 blue, 14 green
|
||||
Game 66: 15 green, 7 blue; 9 blue, 3 green, 16 red; 1 red, 1 blue, 16 green; 18 red, 8 blue, 11 green
|
||||
Game 67: 1 blue, 1 green, 2 red; 5 green, 8 red, 2 blue; 7 red, 1 blue
|
||||
Game 68: 3 blue, 10 red; 13 red, 1 green; 5 blue, 5 red; 2 blue, 1 green, 16 red; 16 red, 3 blue
|
||||
Game 69: 7 red, 1 blue, 3 green; 14 green, 2 blue; 3 green, 2 blue; 4 red, 1 green, 1 blue; 10 red, 14 green, 2 blue
|
||||
Game 70: 10 green, 12 red; 5 red, 7 green; 1 blue, 6 red, 11 green
|
||||
Game 71: 16 green, 13 red, 10 blue; 7 red, 7 blue, 15 green; 17 green, 13 red, 1 blue; 5 blue, 8 green, 11 red; 7 red, 1 blue, 15 green; 15 green, 4 blue, 2 red
|
||||
Game 72: 3 blue, 3 red; 2 blue, 3 red, 1 green; 1 red, 1 blue, 3 green; 1 green, 2 blue, 3 red; 3 blue, 1 green, 1 red; 1 blue, 4 red
|
||||
Game 73: 10 blue, 11 red, 5 green; 6 green, 9 blue, 4 red; 10 red, 5 green, 9 blue
|
||||
Game 74: 6 green, 17 blue; 1 red, 1 blue, 11 green; 2 blue, 1 red, 3 green
|
||||
Game 75: 11 red, 11 green, 3 blue; 11 red, 1 blue, 6 green; 4 green, 3 blue, 8 red
|
||||
Game 76: 3 green, 3 blue, 12 red; 3 blue, 15 green, 3 red; 4 red, 15 green, 2 blue
|
||||
Game 77: 13 blue, 11 red, 1 green; 3 red, 12 green, 12 blue; 7 red, 15 green, 4 blue; 5 red, 2 green, 3 blue
|
||||
Game 78: 4 red, 8 blue, 2 green; 7 blue, 3 green, 7 red; 3 green, 13 blue; 3 red, 4 green
|
||||
Game 79: 7 blue, 13 red, 8 green; 7 green, 15 red, 9 blue; 2 green, 8 red, 10 blue; 13 blue, 20 red, 7 green; 11 red, 2 green, 14 blue
|
||||
Game 80: 12 red; 2 blue, 15 red, 3 green; 1 blue, 1 green, 2 red; 1 green; 1 green, 3 blue, 13 red; 1 green, 2 blue, 1 red
|
||||
Game 81: 11 blue; 6 blue, 8 green, 4 red; 7 blue, 1 red, 1 green
|
||||
Game 82: 9 blue, 1 red; 3 blue, 1 red, 3 green; 8 blue, 8 green, 2 red; 5 blue
|
||||
Game 83: 7 blue, 13 red; 4 blue, 2 green, 3 red; 15 blue, 9 red, 1 green; 14 red, 1 green, 12 blue
|
||||
Game 84: 8 blue, 1 green, 20 red; 9 green, 20 red, 18 blue; 16 red, 15 blue, 5 green; 15 red, 10 green, 16 blue; 11 green, 14 red, 12 blue
|
||||
Game 85: 1 red, 2 blue, 9 green; 13 green, 3 blue, 5 red; 1 green, 1 red, 3 blue; 8 green, 2 blue, 1 red
|
||||
Game 86: 10 red, 6 blue, 11 green; 1 red, 11 green; 7 blue, 6 red, 11 green
|
||||
Game 87: 14 red, 4 blue, 4 green; 14 red, 4 blue, 7 green; 12 red, 11 green, 5 blue; 5 blue, 12 red
|
||||
Game 88: 3 green, 4 blue, 11 red; 3 green, 4 blue, 3 red; 10 red, 3 green; 3 blue, 2 red, 2 green
|
||||
Game 89: 4 blue, 2 red, 3 green; 3 green, 7 red, 13 blue; 1 green, 6 blue
|
||||
Game 90: 7 red; 5 blue, 11 red, 8 green; 8 red, 3 green, 2 blue
|
||||
Game 91: 2 blue; 4 red; 2 blue, 4 green; 3 green, 1 blue, 1 red
|
||||
Game 92: 7 blue, 10 green; 9 green, 9 blue, 7 red; 6 green; 12 red, 1 blue, 4 green; 5 red, 1 green, 13 blue
|
||||
Game 93: 7 green, 6 red, 1 blue; 3 red, 6 green, 8 blue; 1 blue, 6 green; 6 red, 15 blue, 4 green; 10 blue, 2 green, 6 red; 3 green, 5 red, 6 blue
|
||||
Game 94: 5 red, 1 green, 15 blue; 1 blue, 6 red; 2 red, 6 blue, 2 green
|
||||
Game 95: 9 blue, 4 red, 17 green; 15 green, 9 red, 10 blue; 1 blue, 13 green, 12 red
|
||||
Game 96: 1 blue, 12 green, 1 red; 3 blue, 1 green, 5 red; 2 blue, 8 red, 10 green
|
||||
Game 97: 18 blue, 7 red, 11 green; 6 red, 3 blue, 14 green; 1 red, 13 blue, 4 green
|
||||
Game 98: 5 blue, 2 green, 8 red; 12 red; 13 red, 4 blue, 4 green; 7 red, 11 blue; 10 blue, 2 green, 2 red; 6 red, 12 blue
|
||||
Game 99: 4 green, 2 blue, 4 red; 9 blue, 11 red, 1 green; 5 green
|
||||
Game 100: 2 blue, 12 green; 6 green, 1 red, 12 blue; 1 green, 5 blue, 1 red; 1 red, 12 green, 6 blue; 16 blue, 3 green
|
Loading…
Reference in New Issue
Block a user