-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00867ab
commit 52f53ef
Showing
5 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import AoCPuzzle from '../../puzzle'; | ||
|
||
class Node { | ||
private _parent?: Node; | ||
|
||
public children: Node[] = []; | ||
|
||
public childrenWeight: number = 0; | ||
|
||
public totalWeight: number = 0; | ||
|
||
constructor(public name: string, public weight: number) { | ||
} | ||
|
||
set parent(parent: Node) { | ||
this._parent = parent; | ||
this._parent.children.push(this); | ||
} | ||
|
||
get parent(): Node | undefined { | ||
return this._parent; | ||
} | ||
} | ||
|
||
export default class Puzzle extends AoCPuzzle { | ||
private nodes: Node[] = []; | ||
|
||
private root?: Node; | ||
|
||
private findNode(name: string): Node { | ||
const found = this.nodes.find((n) => n.name === name); | ||
if (!found) { | ||
this.nodes.push(new Node(name, 0)); | ||
return this.nodes.find((n) => n.name === name) as Node; | ||
} | ||
return found; | ||
} | ||
|
||
public async part1(): Promise<string | number> { | ||
for (const line of this.lines) { | ||
const [name, weight] = line.split(/\s/gi); | ||
const parent = this.findNode(name); | ||
parent.weight = parseInt(weight.replace(/[()]/gi, ''), 10); | ||
|
||
if (line.match(' -> ')) { | ||
line | ||
.split(' -> ')[1] | ||
.split(', ') | ||
.forEach((childName) => { | ||
const child = this.findNode(childName); | ||
child.parent = parent; | ||
}); | ||
} | ||
} | ||
this.root = this.nodes.find((n) => !n.parent)!; | ||
return this.root.name; | ||
} | ||
|
||
private computeBranchesWeight(node: Node): number { | ||
node.childrenWeight = node.children.reduce((acc, child) => acc + this.computeBranchesWeight(child), 0); | ||
node.totalWeight = node.weight + node.childrenWeight; | ||
return node.totalWeight; | ||
} | ||
|
||
private findUnbalancedNode(node: Node): Node | undefined { | ||
if (node.children.length === 0) { | ||
return; | ||
} | ||
|
||
const weights = node.children.map((child) => this.computeBranchesWeight(child)); | ||
const uniqueWeights = Array.from(new Set(weights)); | ||
if (uniqueWeights.length === 1) { | ||
return; | ||
} | ||
|
||
const [unbalancedWeight] = uniqueWeights.filter((weight) => weights.filter((w) => w === weight).length === 1); | ||
const unbalancedNode = node.children.find((child) => this.computeBranchesWeight(child) === unbalancedWeight)!; | ||
return this.findUnbalancedNode(unbalancedNode) || unbalancedNode; | ||
} | ||
|
||
public async part2(): Promise<string | number> { | ||
const unbalancedNode = this.findUnbalancedNode(this.root!); | ||
const unbalancedParent = unbalancedNode!.parent; | ||
const diff = unbalancedParent!.children.find((child) => child !== unbalancedNode)!.totalWeight - unbalancedNode!.totalWeight; | ||
return unbalancedNode!.weight + diff; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"1":{"part1ExampleResult":9,"part1ExampleTime":0.02391699999998309,"part1Result":1253,"part1Time":0.1158340000000635,"part2ExampleResult":4,"part2ExampleTime":0.046666999999956715,"part2Result":1278,"part2Time":0.07979100000000017},"2":{"part1ExampleResult":18,"part1ExampleTime":0.06704200000001492,"part1Result":30994,"part1Time":0.05000000000006821,"part2ExampleResult":9,"part2ExampleTime":0.055292000000008557,"part2Result":233,"part2Time":0.05224999999995816},"3":{"part1ExampleResult":31,"part1ExampleTime":0.04658299999994142,"part1Result":438,"part1Time":1.6318340000000262,"part2ExampleResult":1968,"part2ExampleTime":0.0861250000000382,"part2Result":266330,"part2Time":1.1300840000000107},"4":{"part1ExampleResult":2,"part1ExampleTime":0.04279200000007677,"part1Result":455,"part1Time":0.43766600000003564,"part2ExampleResult":3,"part2ExampleTime":0.07579100000009475,"part2Result":186,"part2Time":2.141666999999984},"5":{"part1ExampleResult":5,"part1ExampleTime":0.43645799999995916,"part1Result":358131,"part1Time":119.18454200000008,"part2ExampleResult":10,"part2ExampleTime":2.433749999999918,"part2Result":25558839,"part2Time":8206.510125},"6":{"part1ExampleResult":5,"part1ExampleTime":0.5149999999999864,"part1Result":12841,"part1Time":141.68408399999998,"part2ExampleResult":4,"part2ExampleTime":0.056791999999973086,"part2Result":8038,"part2Time":0.02612499999997908}} | ||
{"1":{"part1ExampleResult":9,"part1ExampleTime":0.02391699999998309,"part1Result":1253,"part1Time":0.1158340000000635,"part2ExampleResult":4,"part2ExampleTime":0.046666999999956715,"part2Result":1278,"part2Time":0.07979100000000017},"2":{"part1ExampleResult":18,"part1ExampleTime":0.06704200000001492,"part1Result":30994,"part1Time":0.05000000000006821,"part2ExampleResult":9,"part2ExampleTime":0.055292000000008557,"part2Result":233,"part2Time":0.05224999999995816},"3":{"part1ExampleResult":31,"part1ExampleTime":0.04658299999994142,"part1Result":438,"part1Time":1.6318340000000262,"part2ExampleResult":1968,"part2ExampleTime":0.0861250000000382,"part2Result":266330,"part2Time":1.1300840000000107},"4":{"part1ExampleResult":2,"part1ExampleTime":0.04279200000007677,"part1Result":455,"part1Time":0.43766600000003564,"part2ExampleResult":3,"part2ExampleTime":0.07579100000009475,"part2Result":186,"part2Time":2.141666999999984},"5":{"part1ExampleResult":5,"part1ExampleTime":0.43645799999995916,"part1Result":358131,"part1Time":119.18454200000008,"part2ExampleResult":10,"part2ExampleTime":2.433749999999918,"part2Result":25558839,"part2Time":8206.510125},"6":{"part1ExampleResult":5,"part1ExampleTime":0.5149999999999864,"part1Result":12841,"part1Time":141.68408399999998,"part2ExampleResult":4,"part2ExampleTime":0.056791999999973086,"part2Result":8038,"part2Time":0.02612499999997908},"7":{"part1ExampleResult":"tknk","part1ExampleTime":0.21191699999997127,"part1Result":"cyrupz","part1Time":51.789041,"part2ExampleResult":60,"part2ExampleTime":0.1207920000000513,"part2Result":193,"part2Time":0.26424999999994725}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters