blob: 64f6b5bfb6f092380f6eabd498fb9f198c870763 (
plain) (
blame)
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
|
const data = require('./data')
const arrays = data
.replace(/\n/g,"")
.replace(/bag(s)?/g,"")
.split('.').slice(0,-1)
.map(s => s.split(' contain '))
.map(s => [s[0].trim(), s.slice(1).map(x => x.trim().split(' , '))[0]])
.map(s => [s[0], s[1].map(x => {
if (x !== 'no other') {
return[x.replace(/\d+/g, '').trim(), parseInt(x.match(/[0-9]+/g))]
} else {
return ['no other', 1]
}})])
.map(s => [s[0], s[1].filter(x => x !== null)])
let gold = arrays.find(s => s[0] === 'shiny gold')[1]
let parents = 0
gold.forEach(g => parents += g[1])
while (
gold.map(g => g[0] !== 'no other').some(g => true)
) {
gold = gold.map(g => {
if (arrays.find(s => s[0] === g[0])) {
return arrays.find(s => s[0] === g[0])[1].map(x => {
if (x[0] != 'no other') {
parents = parents + (x[1] * g[1])
}
return ([x[0], x[1] * g[1]])
})
} else { return [] }
}).flat(1)
}
console.log(parents)
|