Public
Edited
Jan 28, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof x = display(state)
Insert cell
import { view } from "@tomlarkworthy/view"
Insert cell
import { variable } from "@tomlarkworthy/view"
Insert cell
display = (state) =>
view`<pre style="display:inline-block">${join2D(state).replaceAll(
/./g,
(c) => Gems[c].symbol
)}</pre>`
Insert cell
history = reduceHistory(instructions, StateUpdate, initial_state).slice(1)
Insert cell
Insert cell
Insert cell
Insert cell
// Movements = ({ L: "MOVE_LEFT", R: "MOVE_RIGHT", A: "ROTATE_CC", B: "ROTATE_C" })
Insert cell
Insert cell
instructions.forEach(dispatchInstructions)
Insert cell
dispatchInstructions = (instructions) => {
const { gems, rotations, translations } = instructions;
const { NewGems, Rotation, Translation, DropGems } = Events;
// new gems
NewGems.dispatch({ gems: gems.split`` });
// rotations
_.forEach((rotation) => Rotation.dispatch({ rotation }))(rotations);
// translations
_.forEach((translation) => Translation.dispatch({ translation }))(
translations
);
// drop gems
DropGems.dispatch();
}
Insert cell
mutable event_log = []
Insert cell
logEvent(mutable event_log[0])
Insert cell
logEvent = (event) => {
mutable event_log.push(event);
mutable event_log = mutable event_log;
}
Insert cell
console.log(_.last(event_log))
Insert cell
Events = toEvents(["NewGems", "Rotation", "Translation", "DropGems"])
Insert cell
toEvents = _.flow(
_.map((name) => [name, toEvent(name)]),
_.fromPairs
)
Insert cell
toEvent = (name) => {
const type = `${name}Event`;

const event_class = class extends CustomEvent {};

return {
type,
// toString: _.constant(type),
dispatch: (detail) => dispatchEvent(new CustomEvent(type, { detail }))
// listen: (fn) => addEventListener(type, fn),
// remove: (fn) => removeEventListener(type, fn)
};
}
Insert cell
_.forEach((event) => {
addEventListener(event.type, logEvent);
invalidation.then(() => removeEventListener(event.type, logEvent));
})(Events)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
test(test1)
Insert cell
test1 = ({
instructions: [
["BR", "LLL"],
["BY", "LL"],
["BG", "ALL"],
["BY", "BRR"],
["RR", "AR"],
["GY", "A"],
["BB", "AALLL"],
["GR", "A"],
["RY", "LL"],
["GG", "L"],
["GY", "BB"],
["bR", "ALLL"],
["gy", "AAL"]
],
end_state: [
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" R ",
" R YR",
"RR RB"
].map((e) => e.split``),
history: [
" \n \n \n \n \n \n \n \n \n \nB \nR ",
" \n \n \n \n \n \n \n \n \n \nBB \nRY ",
" \n \n \n \n \n \n \n \n \n B \nBB \nRYG ",
" \n \n \n \n \n \n \n \n \n B \nBB \nRYG YB",
" \n \n \n \n \n \n \n \n \n B \nBB RR\nRYG YB",
" \n \n \n \n \n \n \n \n \n B Y \nBB RR\nRYGGYB",
" \n \n \n \n \n \n \n \nB \nBB Y \nBB RR\nRYGGYB",
" \n \n \n \n \n \n \n \nB R \nBB Y \nBB GRR\nRYGGYB",
" \n \n \n \n \n \n \n R \nBY R \nBB Y \nBB GRR\nRYGGYB",
" \n \n \n \n \n \n \n R \nBY R \nBBG Y \nBBGGRR\nRYGGYB",
" \n \n \n \n \n \n \n R \nBY YR \nBBGGY \nBBGGRR\nRYGGYB",
" \n \n \n \n \n \n \n \n R YR \n RGGY \n YGGRR\nRYGGYB",
" \n \n \n \n \n \n \n \n \n R \n R YR\nRR RB"
].map((e) => e.split`\n`.map((e) => e.split``))
})
Insert cell
verify = ({ instructions, state }) => puzzleFighter(instructions) == state
Insert cell
test = (...args) => htl.html`${verify(args) ? `✔` : `❌`}`
Insert cell
Insert cell
Insert cell
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