Skip to content

Commit

Permalink
2017/04
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrice404 committed Nov 26, 2024
1 parent 00867ab commit 52f53ef
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
"no-continue": "off",
"no-eval": "off",
"no-param-reassign": "off",
"no-underscore-dangle": "off",
"no-restricted-syntax": ["error", "ForInStatement", "LabeledStatement", "WithStatement"],
"@typescript-eslint/no-loop-func": "off"
},
Expand Down
87 changes: 87 additions & 0 deletions 2017/07/index.ts
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;
}
}
1 change: 1 addition & 0 deletions 2017/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Day | Part 1 | Part 1 | Part 2 | Part 2
4 | 0.043ms <br/><sub><sup>2</sup></sub> | 0.438ms <br/><sub><sup>455</sup></sub> | 0.076ms <br/><sub><sup>3</sup></sub> | 2.142ms <br/><sub><sup>186</sup></sub>
5 | 0.436ms <br/><sub><sup>5</sup></sub> | 119.185ms <br/><sub><sup>358131</sup></sub> | 2.434ms <br/><sub><sup>10</sup></sub> | 8206.510ms <br/><sub><sup>25558839</sup></sub>
6 | 0.515ms <br/><sub><sup>5</sup></sub> | 141.684ms <br/><sub><sup>12841</sup></sub> | 0.057ms <br/><sub><sup>4</sup></sub> | 0.026ms <br/><sub><sup>8038</sup></sub>
7 | 0.212ms <br/><sub><sup>tknk</sup></sub> | 51.789ms <br/><sub><sup>cyrupz</sup></sub> | 0.121ms <br/><sub><sup>60</sup></sub> | 0.264ms <br/><sub><sup>193</sup></sub>
2 changes: 1 addition & 1 deletion 2017/stats.json
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}}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| [2020](./2020) | 20 | ⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️<br/>⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️ |
| [2019](./2019) | 12 | ⭐️⭐️⭐️⭐️⭐️⭐️<br/>⭐️⭐️⭐️⭐️⭐️⭐️ |
| [2018](./2018) | 26 | ⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️<br/>⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️ |
| [2017](./2017) | 12 | ⭐️⭐️⭐️⭐️⭐️⭐️<br/>⭐️⭐️⭐️⭐️⭐️⭐️ |
| [2017](./2017) | 14 | ⭐️⭐️⭐️⭐️⭐️⭐️⭐️<br/>⭐️⭐️⭐️⭐️⭐️⭐️⭐️ |
| [2016](./2016) | 10 | ⭐️⭐️⭐️⭐️⭐️<br/>⭐️⭐️⭐️⭐️⭐️ |
| [2015](./2015) | 14 | ⭐️⭐️⭐️⭐️⭐️⭐️⭐️<br/>⭐️⭐️⭐️⭐️⭐️⭐️⭐️ |
<!-- /auto-generated -->

0 comments on commit 52f53ef

Please sign in to comment.