New classes work

This commit is contained in:
Dominic Zimmer 2020-11-18 14:08:31 +01:00
parent d729141643
commit 19af0555cd

124
main.js
View File

@ -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);
});