|
| 1 | +const console = require('../helpers') |
| 2 | + |
| 3 | +const parseRule = (rule) => { |
| 4 | + const result = {} |
| 5 | + // Get the color of the outer bag |
| 6 | + const outRemainder = rule.split(' contain ') |
| 7 | + result.outer = outRemainder.shift().replace('bags', 'bag') |
| 8 | + // Get the color and values of inner bags |
| 9 | + if (outRemainder[0] !== 'no other bags.') { |
| 10 | + result.inner = outRemainder[0].split(', ').map((str) => { |
| 11 | + const inRemainder = str.split(' ') |
| 12 | + const count = Number(inRemainder.shift()) |
| 13 | + const color = inRemainder.join(' ') |
| 14 | + .replace('.', '') |
| 15 | + .replace('bags', 'bag') |
| 16 | + return { |
| 17 | + count, |
| 18 | + color |
| 19 | + } |
| 20 | + }) |
| 21 | + } |
| 22 | + |
| 23 | + return result |
| 24 | +} |
| 25 | + |
| 26 | +const findAllowedOuter = (rules, color) => { |
| 27 | + const isAllowed = (rule) => { |
| 28 | + if (!rule.inner) return false |
| 29 | + return ( |
| 30 | + rule.inner.filter((child) => { |
| 31 | + return ( |
| 32 | + child.color === color |
| 33 | + ) |
| 34 | + }).length > 0 |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + const allowed = {} |
| 39 | + |
| 40 | + // Loop through the rules, find all colors this bag is allowed within |
| 41 | + rules.filter(isAllowed).forEach((rule) => { |
| 42 | + allowed[rule.outer] = true |
| 43 | + }) |
| 44 | + |
| 45 | + // Take the list of allowed colors, and find out which they are allowed within |
| 46 | + Object.keys(allowed).forEach((color) => { |
| 47 | + const temp = findAllowedOuter(rules, color) |
| 48 | + if (Object.keys(temp).length > 0) { |
| 49 | + Object.assign(allowed, temp) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + return allowed |
| 54 | +} |
| 55 | + |
| 56 | +const countInner = (rules, color, count = 1) => { |
| 57 | + // const children = {} |
| 58 | + /** checks if rule matches color */ |
| 59 | + const matchesColor = ({ outer }) => outer === color |
| 60 | + /** checks if rule has child bags */ |
| 61 | + const hasChildren = ({ inner }) => (inner) && inner.length > 0 |
| 62 | + |
| 63 | + const getChildrenBags = ({ inner }, multiplier = 1) => { |
| 64 | + const res = {} |
| 65 | + // Convert into structured list |
| 66 | + inner.forEach(({ color, count }) => { |
| 67 | + res[color] = count * multiplier |
| 68 | + }) |
| 69 | + return res |
| 70 | + } |
| 71 | + /** type-safe addition */ |
| 72 | + const add = (a, b) => { |
| 73 | + a = (typeof a === 'number') ? a : 0 |
| 74 | + b = (typeof b === 'number') ? b : 0 |
| 75 | + return a + b |
| 76 | + } |
| 77 | + /** combine two objects using the specified operator method for collsions */ |
| 78 | + const combineObjects = (a = {}, b = {}, operator) => { |
| 79 | + const c = {} |
| 80 | + // check for collisions between fields across the objects and run operator() on them |
| 81 | + for (const [key, value] of Object.entries(b)) { |
| 82 | + c[key] = operator(a[key], value) |
| 83 | + } |
| 84 | + return Object.assign({}, a, c) // b not needed because covered in collision resolver |
| 85 | + } |
| 86 | + |
| 87 | + console.debug('matching', color) |
| 88 | + |
| 89 | + // Loop through the rules to find first level children |
| 90 | + return rules |
| 91 | + .filter(matchesColor) // find all matches for the color |
| 92 | + .filter(hasChildren) // filter for matches that have children |
| 93 | + .map(rule => getChildrenBags(rule, count)) // get the counts from the children |
| 94 | + .reduce((res, children) => { |
| 95 | + // Add everything back together |
| 96 | + const childrensChildren = Object.entries(children) |
| 97 | + .map(([key, value]) => countInner(rules, key, value)) |
| 98 | + .reduce((r, c) => combineObjects(r, c, add), {}) |
| 99 | + |
| 100 | + res = combineObjects(res, children, add) |
| 101 | + res = combineObjects(res, childrensChildren, add) |
| 102 | + |
| 103 | + return res |
| 104 | + }, {}) |
| 105 | +} |
| 106 | + |
| 107 | +module.exports = { |
| 108 | + parseRule, |
| 109 | + findAllowedOuter, |
| 110 | + countInner |
| 111 | +} |
0 commit comments