Skip to content

Instantly share code, notes, and snippets.

@protoEvangelion
Created October 15, 2022 19:05
Show Gist options
  • Save protoEvangelion/17fd28a471730e6ca35b43ed8d647343 to your computer and use it in GitHub Desktop.
Save protoEvangelion/17fd28a471730e6ca35b43ed8d647343 to your computer and use it in GitHub Desktop.
Node JS Maze Pathfinder Algo + Running Against TCP Dungeon Program (for a CTF)). Is not a shortest path algo. Just finds the end of the maze.
const { SocketClientTCP } = require("netlinkwrapper");
let maze;
let solved;
let lastTravelMsg;
const size = 30;
const endRow = size - 1;
const endCol = size - 1;
const port = 1234;
const client = new SocketClientTCP(port, "159.223.124.69");
const isClear = (rowI, colI) => {
client.send(`ex ${rowI} ${colI}\n`);
return getResponse() === "clear";
};
const getResponse = () => client.receive().toString().trim();
console.log(getResponse());
/** START PROGRAM */
begin();
/** END PROGRAM */
function begin() {
solved = false;
console.log("START BEGIN");
mapMaze();
crash();
console.log("START TRAVELLING");
travel(0, 0, []);
console.log("END TRAVELLING", solved);
if (!solved) {
crash(true);
begin();
}
lastTravelMsg.includes("congrats") ? begin() : crash(true);
console.log("END BEGIN");
}
function crash(shouldCrash = false) {
console.log("START CRASH");
const directions = [
[0, 1, "right"],
[1, 1, "down_right"],
[1, 0, "down"],
];
const blockedDirs = directions
.map(([row, col, direction]) => (isClear(row, col) ? false : direction))
.filter((x) => x);
if (shouldCrash || (!shouldCrash && blockedDirs.length === 3)) {
client.send(`mov ${blockedDirs[0]}\n`);
console.log(getResponse());
console.log("CRASHED");
}
console.log("DID NOT CRASH");
}
function mapMaze() {
console.log("START MAPPING");
const emptyMaze = Array(size)
.fill(0)
.map(() => Array(size).fill(0));
maze = emptyMaze.map((row, rowI) => {
rowI % 5 === 0 && console.log(rowI);
return row.map((_, colI) => {
return isClear(rowI, colI) ? 0 : 1;
});
});
console.log(maze.map((x) => x.join()).join("\n"));
console.log("END MAPPING");
}
function travel(row, col, moves, dir) {
if (solved) return true;
const newMoves = [...moves];
if (row === endRow && col === endCol) {
solved = true;
console.log("MADE IT!", JSON.stringify(newMoves));
newMoves.push(`mov ${dir}\n`);
newMoves.forEach((move) => {
client.send(move);
const res = getResponse();
lastTravelMsg = res;
console.log(res);
});
return true;
}
const item = maze[row]?.[col];
if (item === 0 && dir) newMoves.push(`mov ${dir}`);
maze[row][col] = "v";
const down = [row + 1, col];
valid(down) && travel(...down, newMoves, "down");
const down_right = [row + 1, col + 1];
valid(down_right) && travel(...down_right, newMoves, "down_right");
const down_left = [row + 1, col - 1];
valid(down_left) && travel(...down_left, newMoves, "down_left");
const right = [row, col + 1];
valid(right) && travel(...right, newMoves, "right");
const left = [row, col - 1];
valid(left) && travel(...left, newMoves, "left");
const up_left = [row - 1, col - 1];
valid(up_left) && travel(...up_left, newMoves, "up_left");
const up_right = [row - 1, col + 1];
valid(up_right) && travel(...up_right, newMoves, "up_right");
const up = [row - 1, col];
valid(up) && travel(...up, newMoves, "up");
return false;
}
function valid([row, col]) {
const item = maze[row]?.[col];
return !(item === undefined || item === "v" || item === 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment