This is part 2!
Actually I always traversed in the same order. I just bruteforced in part 1, trying all possible start point 😂 This is my part 1 solution:
const tree = input.reduce((tree, line) => { const color = /(^.*) bags contain/.exec(line)[1]; tree[color] = []; const matches = line.matchAll(/,? \d+ ([^,.]*) bags?/g); for (const match of matches) { tree[color].push(match[1]); } return tree; }, {}); const canContainGold = (color) => { if (tree[color].includes("shiny gold")) return true; return tree[color].some((color) => canContainGold(color)); }; const filtered = Object.keys(tree).filter((color) => { return canContainGold(color); }); console.log(filtered.length);
oh yes right :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is part 2!
Actually I always traversed in the same order. I just bruteforced in part 1, trying all possible start point 😂 This is my part 1 solution:
oh yes right :)