-
Notifications
You must be signed in to change notification settings - Fork 0
/
day 7-2.js
31 lines (28 loc) · 1.14 KB
/
day 7-2.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
const day7 = require('./inputs/day7-test1').input.split('\n')
let result = []
day7.forEach(bag => {
const bags = bag.split('contain')[1].trim().split(', ')
result = bags.reduce((bags, p) => {
let [ bagCount, bagName ] = p.split(/ (.*)/)
bagName = bagName.split('.')[0].replace('bags', 'bag').trim()
if (bagName === 'other bag') return bags
const isIn = bag.split('contain')[0].replace('bags', 'bag').trim()
if (bags.find(b => b.bagName === isIn)) {
bags.find(b => b.bagName === isIn).holds.push( {bagName, bagCount})
} else {
bags.push({bagName: isIn, holds: [{bagName, bagCount}]});
}
return bags
}, result)
})
const findContainers = (colour, multipier) => {
if (!result.find(b =>b.bagName === colour)) return 0
const bag = result.find(b =>b.bagName === colour)
console.log(bag)
return bag.holds.reduce((count, item) => {
console.log(item.bagCount, multipier, findContainers(item.bagName, item.bagCount * multipier))
count += (item.bagCount * multipier) + findContainers(item.bagName, item.bagCount * multipier)
console.log(count)
}, 0)
}
console.log(findContainers('shiny gold bag', 1))