Public
Edited
Dec 16, 2022
Insert cell
Insert cell
wod = "adrift"
Insert cell
lengths = [
[], // zero-indexing is confusing here
wordlist6.filter((d) => d.length == 1),
wordlist6.filter((d) => d.length == 2),
wordlist6.filter((d) => d.length == 3),
wordlist6.filter((d) => d.length == 4),
wordlist6.filter((d) => d.length == 5),
wordlist6.filter((d) => d.length == 6),
wordlist6.filter((d) => d.length == 7)
]
Insert cell
all_words = lengths.flat()
Insert cell
//part1 = optimize(all_words.indexOf(wod), 12, undefined, new Set())
Insert cell
function show_neighbors(word) {
const worddex = all_words.indexOf(word);
const neighbors = adjacency[worddex];
}
Insert cell
score = {
let score = 0;
let stack = [wod];
let remaining = 42;
let step = 19;
while (remaining >= 0) {
const depth = d3.min([step, remaining]);
const used = new Set([...stack.map((d) => all_words.indexOf(d))]);
console.log(new Set([...stack]));
const [newscore, ...rest] = optimize(
all_words.indexOf(stack[stack.length - 1]),
depth,
undefined,
used
);
stack = [...stack, ...rest].filter((d) => d !== null);
// return stack;
score = d3.sum(stack.slice(1).map((d) => d.length));
remaining -= depth;
yield { score, remaining, stack: [...stack] };
}
}
Insert cell
md`${score.stack.join(", ")}`
Insert cell
crazy_good_words = adjacency
.map((d, i) => [d.length * all_words[i].length, i])
.filter((d) => d[0] > 1000)
.map((d) => [all_words[d[1]], d[0]])
Insert cell
md`Adjacency construction is done when the below cell is pretty long... about 54,000 for through length 7`
Insert cell
max_thresh = 7
Insert cell
function dist(a, b, cost = 0) {
for (let i = 0; i < a.length; i++) {
let dist = Math.abs(a[i] - b[i]);
if (dist > 13) {
dist = 25 - dist;
}
cost += dist;
if (cost > max_thresh) {
return 100;
}
}
return cost;
}
Insert cell
adjacency = {
const vals = [];
for (let wordlength of [1, 2, 3, 4, 5, 6]) {
vals.push(...build_adjacency(wordlength, vals.length));
yield vals;
}
yield vals;
}
Insert cell
build_adjacency = function (n, offset = 0) {
const coded = lengths[n].map((d) => d.split("").map((e) => codes[e]));
const dists = coded.map((d) => []);
for (let i = 0; i < coded.length; i++) {
const word1 = coded[i];
const words = [[0, word1]];
for (let k = 1; k < word1.length; k++) {
words.push([
Math.min(k, word1.length - k),
[...word1.slice(k, word1.length), ...word1.slice(0, k)]
]);
}
for (let j = i + 1; j < coded.length; j++) {
if (coded[j].length != word1.length) {
continue;
}
for (let [cost, word] of words) {
const d = dist(word, coded[j], cost);
if (d <= max_thresh) {
dists[i].push([j + offset, d]);
dists[j].push([i + offset, d]);
}
}
}
for (let drop = 0; drop < word1.length; drop++) {
const word = [
...word1.slice(0, drop),
...word1.slice(drop + 1, word1.length)
];
if (total_vocab[word1.join("-")] !== undefined) {
dists[i].push([total_vocab[word1.join("-")], 3]);
}
}
}
return dists;
}
Insert cell
total_vocab = Object.fromEntries(coded.map((d, i) => [i, d.join("-")]))
Insert cell
coded = all_words.map((d) => d.split("").map((e) => codes[e]))
Insert cell
function optimize(
word,
move_budget,
remaining,
history = new Set(),
current_score = 0
) {
// console.log({ history });
const neighbors = adjacency[word];
const scores = [];
const new_history = new Set([...history, word]);
for (let [neighbor, dist] of neighbors) {
if (!history.has(neighbor) && dist < move_budget) {
const path = optimize(
neighbor,
move_budget - dist,
undefined,
new_history,
neighbor.length
);
// console.log({ path });
// console.log(path[0]);
// console.log(neighbor.length);
scores.push([
path[0] + coded[neighbor].length,
all_words[neighbor],
...path.slice(1)
]);
}
}
if (scores.length === 0) {
return [0, null];
}
scores.sort((a, b) => b[0] - a[0]);
return scores[0];
}
Insert cell
codes = Object.fromEntries(
d3.range(97, 97 + 26).map((i) => [String.fromCharCode(i), i - 97])
)
Insert cell
wordlist6 = FileAttachment("wordlist6.txt")
.text()
.then((d) => d.split("\n"))
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