class Resistor { ohmage; constructor(ohmage) { this.ohmage = ohmage; } getOhmage() { return this.ohmage; } equals(other) { return this.getOhmage() === other.getOhmage(); } hash() { return this.ohmage; } cost() { return 1; } toString() { return `${this.constructor.name}(${this.getOhmage()})`; } 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; 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)}` } } 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 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 = NewSeriesResistor.smart(e1, e2); r2 = NewParallelResistor.smart(e1, e2); 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; }