-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.js
55 lines (41 loc) · 1.66 KB
/
day07.js
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
const bagageRules = require('./entries/day07.json');
const parseBagString = s => {
if (s.trim() === 'no other bags') return null;
const [, count, bag] = /^(\d*)([a-zA-Z ]+)bags?$/.exec(s.trim());
return {
count: count ? parseInt(count) : 1,
bag: bag.trim(),
};
};
const bagsTree = bagageRules.map(rule => {
const [parent, children] = rule.replace('.', '').split('contain');
return {
parent: parseBagString(parent),
children: children.split(',').map(parseBagString).filter(Boolean),
};
});
// PART 1
const getBagNames = arr => Array.from(new Set(arr.map(({ parent }) => parent.bag)));
const filterBagsThatCanContainBag = bagName => bagsTree.filter(i => i.children.find(({ bag }) => bag === bagName));
const computeBagsWhichCanContain = bagName => {
let names = [];
let allNames = [];
let bags = filterBagsThatCanContainBag(bagName);
do {
names = getBagNames(bags);
allNames.push(names);
bags = [].concat(...names.map(filterBagsThatCanContainBag));
} while (names.length !== 0);
return new Set([].concat(...allNames));
};
const bagsThatCanHaveAShinyGold = computeBagsWhichCanContain('shiny gold').size;
console.log(bagsThatCanHaveAShinyGold);
// PART 2
const findBagInfoByName = bagName => bagsTree.find(i => i.parent.bag === bagName);
const computeCountOfBagsThatGoIn = bagName => {
const foundInTree = findBagInfoByName(bagName);
const bagsThatGoIn = foundInTree && foundInTree.children;
return bagsThatGoIn.reduce((acc, curr) => curr.count + curr.count * computeCountOfBagsThatGoIn(curr.bag) + acc, 0);
};
const countBagsAShinyBagCanContain = computeCountOfBagsThatGoIn('shiny gold');
console.log(countBagsAShinyBagCanContain);