Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Created November 4, 2024 00:59
Show Gist options
  • Save matthewoestreich/19a80e5403ce16d9e62448a0a323bc69 to your computer and use it in GitHub Desktop.
Save matthewoestreich/19a80e5403ce16d9e62448a0a323bc69 to your computer and use it in GitHub Desktop.
Training on Warcraft Series I
function generateEmptyPathArray(pathLength) {
let path = ["M"];
for (let i = 1; i < pathLength-1; i++) {
path.push(".");
}
path.push("B");
return path;
}
function simulateMining(path, time) {
const LEN = path.length;
const workers = [];
// Put default state of workers in our workers array.
for (let i = 0; i < LEN; i++) {
const action = path.charAt(i);
if (action === "<" || action === ">") {
let direction = "L";
if (action === ">") {
direction = "R";
}
workers.push([i, action, direction]);
}
}
// If no workers, just return the "path" string N times (where N="time").
if (!workers.length) {
return new Array(time).fill(path);
}
const finalPaths = [path];
for (let i = 1; i < time; i++) {
let nextPath = generateEmptyPathArray(LEN);
for (let w = 0; w < workers.length; w++) {
const [index, action, direction] = workers[w];
let nextIndex = undefined;
let nextAction = action;
let nextDirection = direction;
if (action === "<") {
nextIndex = index - 1;
} else if (action === ">") {
nextIndex = index + 1;
} else if (action === "#" || action === "*") {
// assume we were heading left..
nextIndex = index - 1;
nextAction = "<";
if (direction === "R") {
nextIndex = index + 1;
nextAction = ">";
}
}
let nextMove = nextPath[nextIndex];
if (nextMove === "M" || nextMove === "B") {
nextAction = "*";
nextDirection = direction === "R" ? "L" : "R";
} else if (nextMove === ">" || nextMove === "<") { // Someone is already there...
nextAction = "#";
}
nextPath[nextIndex] = nextAction;
workers[w] = [nextIndex, nextAction, nextDirection];
}
finalPaths.push(nextPath.join(""));
}
return finalPaths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment