Skip to content

Commit

Permalink
feat(js): add solution for 2024 day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 25, 2024
1 parent 74cb97c commit 9368060
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
46 changes: 46 additions & 0 deletions js/src/2024/25/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Grid, solution } from '../../utils/index.js';

const FILLED = '#';

const parse = (input) => {
const keys = [];
const locks = [];
for (const schematic of input.trim().split('\n\n')) {
const grid = Grid.from(schematic);
const isLock = grid.get(0, 0) === FILLED;

const heights = [];
for (let x = grid.minX; x <= grid.maxX; ++x) {
let height = 0;
for (let y = isLock ? 1 : 5; isLock ? y < 6 : y > 0; y += isLock ? 1 : -1) {
if (grid.get(x, y) === FILLED) {
++height;
}
}
heights.push(height);
}

if (isLock) {
locks.push(heights);
} else {
keys.push(heights);
}
}
return { keys, locks };
};

const fits = (key, lock) => lock.every((height, index) => height + key[index] < 6);

export const finalPart = solution((input) => {
const { keys, locks } = parse(input);

let count = 0;
for (const lock of locks) {
for (const key of keys) {
if (fits(key, lock)) {
++count;
}
}
}
return count;
});
42 changes: 42 additions & 0 deletions puzzles/2024/25/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
final-part:
- input: |
#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####
answer: 3

0 comments on commit 9368060

Please sign in to comment.