-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
73 lines (54 loc) · 1.68 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
interface Bags {
[bagType: string]: BagContent | {};
}
interface BagContent {
[bagType: string]: number;
}
const SHINY_GOLD_BAG_TYPE = "shiny gold";
const formatBagRules = (acc: Bags, rule: string): Bags => {
const [bagType, containedBags] = rule.split(" bags contain ");
const content: BagContent = containedBags.split(", ").reduce((acc, bag) => {
const matches = bag.match(/^(\d+) ([a-z ]+) bags?\.?$/);
if (!matches) {
return {};
}
const [_, quantity, type] = matches;
return { ...acc, [type]: parseInt(quantity, 10) };
}, {});
return { ...acc, [bagType]: content };
};
const recursiveFindBag = (
desiredBag: string,
bagType: keyof Bags,
bags: Bags
): boolean => {
const keys = Object.keys(bags[bagType]);
if (keys.includes(desiredBag)) {
return true;
}
return keys.some((key) => recursiveFindBag(desiredBag, key, bags));
};
export const part1 = (input: string) => {
const rules = input.split("\n");
const bags: Bags = rules.reduce(formatBagRules, {});
return Object.keys(bags).reduce((acc: number, bagType) => {
if (recursiveFindBag(SHINY_GOLD_BAG_TYPE, bagType, bags)) {
return acc + 1;
}
return acc;
}, 0);
};
const recursiveCountBags = (bagType: keyof Bags, bags: Bags): number => {
const bagContent = Object.entries(bags[bagType]);
if (bagContent.length === 0) {
return 0;
}
return bagContent.reduce((acc, [key, quantity]) => {
return acc + quantity + quantity * recursiveCountBags(key, bags);
}, 0);
};
export const part2 = (input: string) => {
const rules = input.split("\n");
const bags: Bags = rules.reduce(formatBagRules, {});
return recursiveCountBags(SHINY_GOLD_BAG_TYPE, bags);
};