Skip to content

Commit 5d999ac

Browse files
committed
feat: 2024 Day 25
1 parent d7b46ec commit 5d999ac

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

2024/day/25/part/1/solve copy.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default function solve(input: string) {
2+
const locks: number[][] = [];
3+
const keys: number[][] = [];
4+
for (const lines of input.split("\n\n")) {
5+
const rows = lines.split("\n").slice(1, -1);
6+
const heights = Array.from(rows[0], () => 0);
7+
if (lines.startsWith(".")) rows.reverse();
8+
for (let x = 0; x < rows[0].length; x++) {
9+
for (let height = rows.length; height > 0; height--) {
10+
if (rows[height - 1][x] === ".") continue;
11+
heights[x] = height;
12+
break;
13+
}
14+
}
15+
if (lines.startsWith("#")) locks.push(heights);
16+
else keys.push(heights);
17+
}
18+
let count = 0;
19+
for (const lock of locks) {
20+
for (const key of keys) {
21+
if (
22+
key.every((height, index, { length: max }) =>
23+
height + lock[index] <= max
24+
)
25+
) count++;
26+
}
27+
}
28+
return count;
29+
}

2024/day/25/part/1/solve.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "@std/assert";
4+
5+
Deno.test("example", () => {
6+
const input = `\
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+
assertEquals(solve(input), 3);
48+
});

2024/day/25/part/1/solve.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function solve(input: string, { size = 5 } = {}) {
2+
const locks: number[][] = [];
3+
const keys: number[][] = [];
4+
for (const schematic of input.split("\n\n")) {
5+
const rows = schematic.split("\n").slice(1, -1);
6+
const heights = Array.from(
7+
{ length: size },
8+
(_, x) => rows.reduce((height, row) => height + +(row[x] === "#"), 0),
9+
);
10+
(schematic[0] === "#" ? locks : keys).push(heights);
11+
}
12+
let count = 0;
13+
for (const lock of locks) {
14+
for (const key of keys) {
15+
if (key.every((height, index) => height + lock[index] <= size)) count++;
16+
}
17+
}
18+
return count;
19+
}

0 commit comments

Comments
 (0)