-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
75 lines (58 loc) · 1.72 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { INPUT_PATH, runner, SAMPLE_PATH } from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
const solution = (input: string) => {
const [patternsRaw, designsRaw] = input.trim().split('\n\n');
const designs = designsRaw.split('\n');
const patterns = patternsRaw.split(', ');
let count = 0;
const hasDesign = (str: string, memo: Map<string, boolean>) => {
if (str === '') return true;
if (memo.has(str)) return memo.get(str);
for (const p of patterns) {
if (str.startsWith(p)) {
const part = str.substring(p.length);
if (hasDesign(part, memo)) {
return memo.set(part, true);
}
}
}
return false;
};
for (const design of designs) {
const designMap = new Map<string, boolean>();
if (hasDesign(design, designMap)) count++;
}
return count;
};
const solution2 = (input: string) => {
const [patternsRaw, designsRaw] = input.trim().split('\n\n');
const designs = designsRaw.split('\n');
const patterns = patternsRaw.split(', ');
const hasDesign = (str: string, memo: Map<string, number>): number => {
let localCount = 0;
if (memo.has(str)) return memo.get(str) || localCount;
if (str === '') return 1;
for (const p of patterns) {
if (str.startsWith(p)) {
const part = str.substring(p.length);
localCount += hasDesign(part, memo);
memo.set(str, localCount);
}
}
return localCount;
};
let count = 0;
for (const design of designs) {
const designMap = new Map<string, number>();
count += hasDesign(design, designMap);
}
return count;
};
runner({
path,
solution: (input) => solution(input),
});
runner({
path,
solution: (input) => solution2(input),
});