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 new ParallelResistor(this, other);
}
series(other) {
return new SeriesResistor(this, other);
}
getSVG() {
var svg = ``
var [width, height] = [50, 100]
return [svg, width, height]
}
draw() {
var [svg, width, height] = this.getSVG();
var maxHeight = height + 2*25
var maxWidth = width
var midPoint = width / 2
var thesvg = ``
document.getElementsByTagName("body")[0].innerHTML = thesvg;
}
}
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;
}
function flatten(list, flattable) {
return list.reduce( (acc, cur) => acc.concat(cur.constructor.name === flattable ? cur.children : [cur]), []);
}
class NestedResistor extends Resistor {
children = []
constructor(l, func, instance) {
super(func(l.map(e => e.getOhmage())));
this.func = func
this.children = flatten(l, instance.constructor.name);
}
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 new ParallelResistor(this, other);
}
series(other) {
return new SeriesResistor(this, other);
}
}
class SeriesResistor extends NestedResistor {
constructor(...l) {
super(l, sum, "SeriesResistor");
}
getSVG() {
var svgs = this.children.map(e => e.getSVG()); // triplets [svg, width, height]
var n = svgs.length;
var maxHeight = sum(svgs.map(([svg, width, height]) => height)) + 25 * (n - 1)
var maxWidth = Math.max(...svgs.map(([svg, width, height]) => width))
var yOffset = 0; // where to start the next boundin
var centerLine = maxWidth / 2;
var innerSVGs = []
var i = 0;
svgs.forEach(([svg, width, height]) => {
var xStart = (maxWidth - width) / 2
var partialInnerSVG = `${svg}`
if (i < n - 1)
partialInnerSVG += ``
// finally
yOffset += 25 + height
i ++;
innerSVGs.push(partialInnerSVG)
});
var svg = innerSVGs.join("")
return [svg, maxWidth, maxHeight]
}
}
class ParallelResistor extends NestedResistor {
constructor(...l) {
super(l, harm, "ParallelResistor");
}
getSVG() {
var svgs = this.children.map(e => e.getSVG()); // triplets [svg, width, height]
var n = svgs.length;
var maxHeight = Math.max(...svgs.map(([svg, width, height]) => height)) + 2 * 25
var maxWidth = sum(svgs.map(([svg, width, height]) => width)) + 25 * (n - 1)
var bottomLineStart = svgs[0][1] / 2
var bottomLineEnd = maxWidth - svgs[n - 1][1] / 2
var xOffset = 0; // where to start the next boundin
var innerSVGs = []
svgs.forEach(([svg, width, height]) => {
var antennaX = xOffset + width / 2
var antennaLength = (maxHeight - height) / 2
var antennaOneStart = 0
var antennaOneEnd = antennaLength
var antennaTwoStart = antennaLength + height
var antennaTwoEnd = maxHeight
var partialInnerSVG = `${svg}
`
// finally
xOffset += 25 + width
innerSVGs.push(partialInnerSVG)
});
var innerSVG = innerSVGs.join("")
var svg = `
${innerSVG}`
return [svg, maxWidth, maxHeight]
}
}
var resistors = [100, 1000]
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);
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;
}