Public
Edited
Dec 31, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function editor(input) {
let str = input;
while (!/></g.test(str)) {
str = str.replace(/>1/, "3>");
str = str.replace(/>2/, "2>");
str = str.replace(/>3/, "1>");
str = str.replace(/3</, "<1");
str = str.replace(/2</, "<3");
str = str.replace(/1</, "<2");
}
return str;
}
Insert cell
input = ">" + "1".repeat(20) + "3".repeat(40) + "2".repeat(15) + "<"
Insert cell
result = editor(input)
Insert cell
Insert cell
getSum = (str) =>
str
.split("><")
.join("")
.split("")
.map(Number)
.reduce(function (a, b) {
return a + b;
}, 0)
Insert cell
sum = getSum(result)
Insert cell
Insert cell
permutations = Array.from(permUnique("11133222".split("").map(Number)))
Insert cell
short = editor(">11133222<")
Insert cell
short_sum = getSum(short)
Insert cell
{
let max = 0;
for (let p of permutations) {
const s = getSum(editor(">" + p.join("") + "<"));
if (s > max) {
max = s;
}
}
return max;
}
Insert cell
function permUnique(elements) {
let set = new Set(elements);
let listunique = Array.from(set).map(
(i) => new UniqueElement(i, elements.filter((el) => el === i).length)
);
let u = elements.length;
return permUniqueHelper(
listunique,
"0"
.repeat(u)
.split("")
.map((i) => parseInt(i)),
u - 1
);
}
Insert cell
class UniqueElement {
constructor(value, occurrences) {
this.value = value;
this.occurrences = occurrences;
}
}
Insert cell
function* permUniqueHelper(listunique, result_list, d) {
if (d < 0) {
yield [...result_list];
} else {
for (const i of listunique) {
if (i.occurrences > 0) {
result_list[d] = i.value;
i.occurrences--;
for (const g of permUniqueHelper(listunique, result_list, d - 1)) yield g;
i.occurrences++;
}
}
}
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more