Skip to content

Commit

Permalink
feat: 2024 Day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
mfulton26 committed Dec 25, 2024
1 parent d7b46ec commit 5d999ac
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2024/day/25/part/1/solve copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default function solve(input: string) {
const locks: number[][] = [];
const keys: number[][] = [];
for (const lines of input.split("\n\n")) {
const rows = lines.split("\n").slice(1, -1);
const heights = Array.from(rows[0], () => 0);
if (lines.startsWith(".")) rows.reverse();
for (let x = 0; x < rows[0].length; x++) {
for (let height = rows.length; height > 0; height--) {
if (rows[height - 1][x] === ".") continue;
heights[x] = height;
break;
}
}
if (lines.startsWith("#")) locks.push(heights);
else keys.push(heights);
}
let count = 0;
for (const lock of locks) {
for (const key of keys) {
if (
key.every((height, index, { length: max }) =>
height + lock[index] <= max
)
) count++;
}
}
return count;
}
48 changes: 48 additions & 0 deletions 2024/day/25/part/1/solve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import solve from "./solve.ts";

import { assertEquals } from "@std/assert";

Deno.test("example", () => {
const input = `\
#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####`;

assertEquals(solve(input), 3);
});
19 changes: 19 additions & 0 deletions 2024/day/25/part/1/solve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function solve(input: string, { size = 5 } = {}) {
const locks: number[][] = [];
const keys: number[][] = [];
for (const schematic of input.split("\n\n")) {
const rows = schematic.split("\n").slice(1, -1);
const heights = Array.from(
{ length: size },
(_, x) => rows.reduce((height, row) => height + +(row[x] === "#"), 0),
);
(schematic[0] === "#" ? locks : keys).push(heights);
}
let count = 0;
for (const lock of locks) {
for (const key of keys) {
if (key.every((height, index) => height + lock[index] <= size)) count++;
}
}
return count;
}

0 comments on commit 5d999ac

Please sign in to comment.