The Graceful Tree Problem
by SwampFall
HTML
<input type="text" id="input"/><br>
<button onclick="start();">
calc
</button><br>
<div id="count">
</div>
<div id="output">
output here
</div>
JavaScript
// Recursively fills the given structure
function fill(s, options, path, prev, diff, output) {
// Clone so you can restore it after each attempt
var diff_clone = diff.slice(0);
// necessary for sure
var path_clone = path.slice(0);
s = s.slice(0);
for (let i = 0; i < options.length; i++) {
// restore the path (bc it's changed a lil further in code)
path = path_clone.slice(0);
// same thing with options, don't want future steps to change it (also doing it within the loop bc I'm changing the clone)
var options_clone = options.slice(0);
// here I remove the first option out of the clone & option is the option I choose
var option = options_clone.splice(i, 1)[0];
// Use clone to revert back after each loop
diff = diff_clone.slice(0);
// Only try configuration if the difference is distinct
var d = undefined;
if (prev != undefined) {
// calc the difference (only if there is a previous)
d = Math.abs(prev - option);
}
//console.log(["diff", JSON.stringify(path)])
// only do this part if the difference isn't in diff
// (so if it's unique)
if (!diff.includes(d)) {
if (d) {
// add the difference
diff.push(d);
}
// sets the value at 'x'
// setValue is a lil function finding x & then setting
// the value
var temp = setValue(s, path, option);
//console.log(JSON.stringify(s));
// Get the path to the next index
// if the last thingy in the path equals the length of the [] thingy, this means it's at the end of it
if (path[path.length - 1] == temp.length - 1) {
// change the path to go to the next 'x'
path = getNextPath(s, path);
if (path != undefined) {
// if there is a next 'x' find what the prev value was
let prev = getPrev(s,path);
//console.log(JSON.stringify(["end", s, options_clone, path, prev, diff]));
// call fill again with the settings for the...