-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (45 loc) · 1.08 KB
/
index.ts
1
2
3
4
5
6
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
48
49
50
51
52
53
54
55
import {
debugGrid,
INPUT_PATH,
runner,
SAMPLE_PATH,
splitLines,
} from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
const parse = (input: string) => {
return input
.split('\n\n')
.map((row) => row.split('\n').map((el) => el.split('')));
};
const solution = (input: string) => {
const grids = parse(input);
const locks: number[][] = [];
const keys: number[][] = [];
const overlap = grids[0].length - 2;
grids.forEach((lock) => {
const size: number[] = [];
const isLock = lock[0].every((e) => e === '#');
lock.forEach((row, y) => {
row.forEach((cell, x) => {
if (cell === '#') {
if (size[x] !== undefined) size[x]++;
if (size[x] === undefined) size[x] = 0;
}
});
});
isLock ? locks.push(size) : keys.push(size);
});
let fitLocks = 0;
keys.forEach((key) => {
locks.forEach((lock) => {
if (key.every((el, idx) => el + lock[idx] <= overlap)) {
fitLocks++;
}
});
});
return fitLocks;
};
runner({
path,
solution: (input) => solution(input),
});