From d729141643776baf12f4e5fa08cd480e82d21667 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Wed, 18 Nov 2020 11:52:14 +0100 Subject: [PATCH] Initial commit. Basic functionality working --- index.html | 5 +++ main.js | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 index.html create mode 100644 main.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..352a3a5 --- /dev/null +++ b/index.html @@ -0,0 +1,5 @@ + + + + + diff --git a/main.js b/main.js new file mode 100644 index 0000000..6cd0801 --- /dev/null +++ b/main.js @@ -0,0 +1,119 @@ +class Resistor { + #ohmage; + constructor(ohmage) { + this.#ohmage = ohmage; + } + getOhmage() { + return this.#ohmage; + } + equals(other) { + return this.getOhmage() === other.getOhmage(); + } + hash() { + return this.#ohmage; + } + size() { + return 1; + } + toString() { + return `${this.constructor.name}(${this.getOhmage()})`; + } + prettyString(depth = 0) { + return `${'--'.repeat(depth)}${this.toString()}` + } +} +class ComplexResistor extends Resistor { + left; + constructor(other) { + super(other.getOhmage()); + this.left = other; + } +} +class SeriesResistor extends Resistor { + left; + right; + constructor(r1, r2) { + super(r1.getOhmage() + r2.getOhmage()); + this.left = r1; + this.right = r2; + } + size() { + return this.left.size() + this.right.size(); + } + prettyString(depth = 0) { + var leftString = this.left.prettyString(depth + 1) + var leftString = this.left.constructor.name === "SeriesResistor" ? `${this.left.left.prettyString(depth + 1)}\n${this.left.right.prettyString(depth + 1)}` : this.left.prettyString(depth + 1); + var rightString = this.right.constructor.name === "SeriesResistor" ? `${this.right.left.prettyString(depth + 1)}\n${this.right.right.prettyString(depth + 1)}` : this.right.prettyString(depth + 1); + return `${'--'.repeat(depth)}${this.toString()}\n${leftString}\n${rightString}`; + } +} +class ParallelResistor extends Resistor { + left + right + constructor(r1, r2) { + super(1 / ((1/r1.getOhmage()) + (1/r2.getOhmage()))); + this.left = r1; + this.right = r2; + } + size() { + return this.left.size() + this.right.size(); + } + prettyString(depth = 0) { + return `${'--'.repeat(depth)}${this.toString()} +${this.left.prettyString(depth + 1)} +${this.right.prettyString(depth + 1)}` + } +} + + +var resistors = [100] +var N = 4 +var maxValue = Math.max(...resistors) + +numberline = new Array(N * maxValue + 1) +numberline.fill(-1) + +set = new Map() + +for (i = 0; i < resistors.length; i++) { + var value = resistors[i]; + var resistor = new Resistor(value); + set.set(value, resistor); + numberline[value] = resistor; +} + + +for (i = 0; i < N; i++) { + iterSet(); + console.log("After iteration", i, set.size, "entries"); +} +console.log(set) + +function iterSet() { + addition = new Set() + set.forEach(e1 => { + set.forEach(e2 => { + r1 = new SeriesResistor(e1, e2); + r2 = new ParallelResistor(e1, e2); + //console.log(addition.size) + addition.add(r1); + addition.add(r2); + }); + }); + addition.forEach( e => { + value = Math.round(e.getOhmage()); + if (!set.has(value)) { + set.set(value, e) + numberline[value] = e; + } + }); +} + +function missings() { + missing = new Set() + for (i = 0; i < N * maxValue + 1; i++) { + if (numberline[i] == -1) + missing.add(i); + } + return missing; +}