Public
Edited
Aug 28, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
operations = [
{
regex: /swap p.*(\d+).*(\d+)/,
fn: (pwd, m) => swap(pwd, +m[1], +m[2])
},
{
regex: /swap l.* (\w) .*(\w)/,
fn: (pwd, m) => swapLetters(pwd, m[1], m[2])
},
{
regex: /rotate (left|right).*(\d+)/,
fn: (pwd, m) => rotate(pwd, m[1], +m[2])
},
{
regex: /rotate b.*(\w)/,
fn: (pwd, m) => rotateAt(pwd, m[1])
},
{
regex: /reverse.*(\d+).*?(\d+)/,
fn: (pwd, m) => reverseBetween(pwd, +m[1], +m[2])
},
{
regex: /move.*(\d+).*(\d+)/,
fn: (pwd, m) => move(pwd, +m[1], +m[2])
}
]
Insert cell
function transform(pwd, instrs, ops) {
for (const instr of instrs) {
const op = ops.find((o) => o.regex.test(instr));
pwd = op.fn(pwd, instr.match(op.regex));
}
return pwd;
}
Insert cell
Insert cell
function swap(pwd, p1, p2) {
let arr = [...pwd];
[arr[p1], arr[p2]] = [arr[p2], arr[p1]];
return arr.join("");
}
Insert cell
function swapLetters(pwd, a, b) {
return [...pwd].map((c) => (c === a ? b : c === b ? a : c)).join("");
}
Insert cell
function rotate(pwd, dir, n) {
const len = pwd.length;
if (dir === "right") {
return pwd.slice(len - (n % len)) + pwd.slice(0, len - (n % len));
}
return pwd.slice(n % len) + pwd.slice(0, n % len);
}
Insert cell
function rotateAt(pwd, c) {
const i = pwd.indexOf(c);
return rotate(pwd, "right", 1 + i + (i >= 4 ? 1 : 0));
}
Insert cell
function reverseBetween(pwd, p1, p2) {
const middle = [...pwd.slice(p1, p2 + 1)].reverse().join("");
return pwd.slice(0, p1) + middle + pwd.slice(p2 + 1);
}
Insert cell
function move(pwd, p1, p2) {
const withoutChar = pwd.slice(0, p1) + pwd.slice(p1 + 1);
return withoutChar.slice(0, p2) + pwd[p1] + withoutChar.slice(p2);
}
Insert cell
Insert cell
function part1(input) {
return transform("abcdefgh", input.split("\n"), operations);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function inverseRotateAt(pwd, c) {
for (let i = 0; i < pwd.length; i++) {
const trial = rotate(pwd, "left", i);
if (rotateAt(trial, c) === pwd) {
return trial;
}
}
}
Insert cell
Insert cell
inverseOperations = [
{
regex: /swap p.*(\d+).*(\d+)/,
fn: (pwd, m) => swap(pwd, +m[1], +m[2]) // Involution
},
{
regex: /swap l.* (\w) .*(\w)/,
fn: (pwd, m) => swapLetters(pwd, m[1], m[2]) // Involution
},
{
regex: /rotate (left|right).*(\d+)/,
fn: (pwd, m) => rotate(pwd, m[1] === "left" ? "right" : "left", +m[2]) // Reverse direction
},
{
regex: /rotate b.*(\w)/,
fn: (pwd, m) => inverseRotateAt(pwd, m[1]) // Bijection trial and error
},
{
regex: /reverse.*(\d+).*?(\d+)/,
fn: (pwd, m) => reverseBetween(pwd, +m[1], +m[2]) // Involution
},
{
regex: /move.*(\d+).*(\d+)/,
fn: (pwd, m) => move(pwd, +m[2], +m[1]) // Swap order of positions
}
]
Insert cell
function part2(input) {
return transform("fbgdceah", input.split("\n").reverse(), inverseOperations);
}
Insert cell
Insert cell
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