From 19af0555cd87f008a4d5610eb378585b2887abc9 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Wed, 18 Nov 2020 14:08:31 +0100 Subject: [PATCH] New classes work --- main.js | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 116 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index 6cd0801..a47441f 100644 --- a/main.js +++ b/main.js @@ -1,18 +1,18 @@ class Resistor { - #ohmage; + ohmage; constructor(ohmage) { - this.#ohmage = ohmage; + this.ohmage = ohmage; } getOhmage() { - return this.#ohmage; + return this.ohmage; } equals(other) { return this.getOhmage() === other.getOhmage(); } hash() { - return this.#ohmage; + return this.ohmage; } - size() { + cost() { return 1; } toString() { @@ -21,6 +21,12 @@ class Resistor { prettyString(depth = 0) { return `${'--'.repeat(depth)}${this.toString()}` } + parallel(other) { + return NewParallelResistor.smart(this, other); + } + series(other) { + return NewSeriesResistor.smart(this, other); + } } class ComplexResistor extends Resistor { left; @@ -65,6 +71,109 @@ ${this.right.prettyString(depth + 1)}` } } +function sum(list) { + var temp = 0; + list.forEach(e => temp += e); + return temp; +} +function harm(list) { + var temp = 0; + list.forEach(e => temp += 1 / e); + return 1 / temp; +} + +class NestedResistor extends Resistor { + children = [] + + constructor(l, func) { + super(func(l.map(e => e.getOhmage()))); + this.func = func + this.children = l; + } + cost() { + return func(this.children.map(e => e.cost())); + } + prettyString(depth = 0) { + var childrenStrings = this.children.map(c => c.prettyString(depth + 1)).join("\n") + return `${'--'.repeat(depth)}${this.toString()}\n${childrenStrings}` + } + parallel(other) { + return NewParallelResistor.smart(this, other); + } + series(other) { + return NewSeriesResistor.smart(this, other); + } +} +} + + +class NewSeriesResistor extends Resistor { + children = [] + + static smart(...list) { + var thelist = [] + list.forEach(e => { + if (e.constructor.name === "NewSeriesResistor") { + e.children.forEach(e => thelist.push(e)); + } else { + thelist.push(e); + } + }); + return new NewSeriesResistor(thelist); + } + constructor(l) { + super(sum(l.map(e => e.getOhmage()))); + this.children = l; + } + cost() { + return sum(this.children.map(e => e.cost())); + } + prettyString(depth = 0) { + var childrenStrings = this.children.map(c => c.prettyString(depth + 1)).join("\n") + return `${'--'.repeat(depth)}${this.toString()}\n${childrenStrings}` + } + parallel(other) { + return NewParallelResistor.smart(this, other); + } + series(other) { + return NewSeriesResistor.smart(this, other); + } +} + +class NewParallelResistor extends Resistor { + children = [] + + static smart(...list) { + var thelist = [] + list.forEach(e => { + if (e.constructor.name === "NewParallelResistor") { + e.children.forEach(e => thelist.push(e)); + } else { + thelist.push(e); + } + }); + return new NewParallelResistor(thelist); + } + constructor(l) { + super(harm(l.map(e => e.getOhmage()))); + this.children = l; + } + cost() { + return sum(this.children.map(e => e.cost())); + } + prettyString(depth = 0) { + var childrenStrings = this.children.map(c => c.prettyString(depth + 1)).join("\n") + return `${'--'.repeat(depth)}${this.toString()}\n${childrenStrings}` + } + parallel(other) { + return NewParallelResistor.smart(this, other); + } + series(other) { + return NewSeriesResistor.smart(this, other); + } +} + + var resistors = [100] var N = 4 @@ -93,9 +202,8 @@ 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) + r1 = NewSeriesResistor.smart(e1, e2); + r2 = NewParallelResistor.smart(e1, e2); addition.add(r1); addition.add(r2); });