diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2020-12-07 14:50:57 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2020-12-07 14:50:57 +0200 |
| commit | 0f7269d478d94f897b12e6542fa7c6cab88f6e85 (patch) | |
| tree | 7c871766f19b65ac06cd3ef6ee06d8514f79c938 /day7 | |
| parent | 89cf2c6f98881f2f01c130e644be954ed66cd45f (diff) | |
Solve 7.2
Diffstat (limited to 'day7')
| -rw-r--r-- | day7/index.ts | 61 | ||||
| -rw-r--r-- | day7/input_test2.txt | 7 |
2 files changed, 51 insertions, 17 deletions
diff --git a/day7/index.ts b/day7/index.ts index f6a126e..f3878d8 100644 --- a/day7/index.ts +++ b/day7/index.ts @@ -12,9 +12,15 @@ interface Rule { children: RuleChild[]; } -interface TreeNode { +interface TreeNode1 { type: string; - children: TreeNode[]; + children: TreeNode1[]; +} + +interface TreeNode2 { + type: string; + children: TreeNode2[]; + num: number; } const union = <T extends unknown>(...sets: Set<T>[]): Set<T> => { @@ -54,23 +60,21 @@ const rowToRule = (row: string): Rule => { } }; -const constructNode = (rules: Rule[], type: string): TreeNode => { - const children = rules +const buildTree1 = (rules: Rule[], type: string): TreeNode1 => { + const initial: TreeNode1 = { type, children: [] }; + + const node: TreeNode1 = rules .filter(rule => rule.children.some(child => child.type === type)) - .reduce((acc: TreeNode, rule) => { - const newChild = constructNode(rules, rule.type); + .reduce((acc: TreeNode1, ruleWithNum) => { + const newChild = buildTree1(rules, ruleWithNum.type); acc.children.push(newChild); return acc; - }, { type, children: [] }); + }, initial); - return children; + return node; } -const buildTree = (rules: Rule[], rootType: string) => { - return constructNode(rules, rootType); -} - -const uniqueNonRootNodes = (tree: TreeNode): Set<string> => { +const uniqueNonRootNodes = (tree: TreeNode1): Set<string> => { let set = new Set<string>(); for (let child of tree.children) { @@ -83,6 +87,24 @@ const uniqueNonRootNodes = (tree: TreeNode): Set<string> => { return set; } +const buildTree2 = (rules: Rule[], type: string): TreeNode2 => { + const rule = rules.find(rule => rule.type === type); + + const node: TreeNode2 = { + type: rule.type, + children: [], + num: 0 + }; + + for (let child of rule.children) { + const childNode = buildTree2(rules, child.type); + node.children.push(childNode); + node.num += child.num + child.num * childNode.num; + } + + return node; +} + const day7: ExerciseModuleFunc = async (input: string) => { const rows = input.split("\n"); @@ -90,15 +112,20 @@ const day7: ExerciseModuleFunc = async (input: string) => { concatAll(), map(rowToRule), toArray(), - map(rules => buildTree(rules, "shiny gold")), + map(rules => buildTree1(rules, "shiny gold")), map(tree => uniqueNonRootNodes(tree)), map(set => set.size) ).toPromise(); - // const prom2 = of(rows).pipe( - // ).toPromise(); + const prom2 = of(rows).pipe( + concatAll(), + map(rowToRule), + toArray(), + map(rules => buildTree2(rules, "shiny gold")), + map(tree => tree.num) + ).toPromise(); - return Promise.all([prom1]); + return Promise.all([prom1, prom2]); } export default day7; diff --git a/day7/input_test2.txt b/day7/input_test2.txt new file mode 100644 index 0000000..38b2f50 --- /dev/null +++ b/day7/input_test2.txt @@ -0,0 +1,7 @@ +shiny gold bags contain 2 dark red bags. +dark red bags contain 2 dark orange bags. +dark orange bags contain 2 dark yellow bags. +dark yellow bags contain 2 dark green bags. +dark green bags contain 2 dark blue bags. +dark blue bags contain 2 dark violet bags. +dark violet bags contain no other bags.
\ No newline at end of file |
