Thoughts

mental health break ,./'"**^^$_---
My solution for AoC day 7 part 1: #tw Javascript
Definitely a trickier one. At this rate, we'll be doing machine learning by the end of the month. ```js const input = document.body.textContent; const bagTypes = {}; for (let line of input.split("\n").filter(l => l.length)) { let [_, color] = /^([a-z ]+?) bags/.exec(line); const bag = {color, canContain: []}; for (let canContain of line.matchAll(/(\d+) ([a-z ]+) bags?[,.](?: |$)/g)) { let [_, count, subColor] = canContain; bag.canContain.push({count, color: subColor}); } bagTypes[color] = bag; } function canHoldGold(bag) { for (let content of bag.canContain) { if (content.color === "shiny gold") { // Then we can, base case return true; } //Otherwise, we need to check if any of this bag's sub-bags can if (canHoldGold(bagTypes[content.color])) { return true; } } //If none of them can, return false; } console.log("Part 1: ", Object.keys(bagTypes).filter(bagType => canHoldGold(bagTypes[bagType])).length) function countSubBags(bag) { let total = 0; for (let canContain of bag.canContain) { total += parseInt(canContain.count, 10); //Plus all the bags those have to contain. total += parseInt(canContain.count, 10) * countSubBags(bagTypes[canContain.color]); } return total; } console.log("Part 2: ", countSubBags(bagTypes["shiny gold"])); ```
Link 12:59 a.m. Dec 10, 2020 UTC-5