-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add solution for 2024 day 25
- Loading branch information
1 parent
74cb97c
commit 9368060
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
final-part: | ||
- input: | | ||
##### | ||
.#### | ||
.#### | ||
.#### | ||
.#.#. | ||
.#... | ||
..... | ||
##### | ||
##.## | ||
.#.## | ||
...## | ||
...#. | ||
...#. | ||
..... | ||
..... | ||
#.... | ||
#.... | ||
#...# | ||
#.#.# | ||
#.### | ||
##### | ||
..... | ||
..... | ||
#.#.. | ||
###.. | ||
###.# | ||
###.# | ||
##### | ||
..... | ||
..... | ||
..... | ||
#.... | ||
#.#.. | ||
#.#.# | ||
##### | ||
answer: 3 |