Skip to content

Instantly share code, notes, and snippets.

@niorad
Created December 18, 2024 17:29
Show Gist options
  • Save niorad/ab9d18cf95915673bd001bf6b5acd76e to your computer and use it in GitHub Desktop.
Save niorad/ab9d18cf95915673bd001bf6b5acd76e to your computer and use it in GitHub Desktop.
Aoc24_18_1
import r from "npm:raylib";
import { goget } from "../_util/util.ts";
const bytes: number[][] = goget("input.txt").map((line) => {
//const bytes: number[][] = goget('testinput.txt').map((line) => {
const str = line.split(",");
return [parseInt(str[0]), parseInt(str[1])];
});
const VISUAL = false;
//const LIMIT = 22;
const LIMIT = 1024;
const GRIDSIZE = 71;
const open = [[0, 0, 0]];
const goalDists = [];
const tracker = { "0_0": 0 };
if (VISUAL) {
init();
}
mainLoop();
function updateFrame() {
const last = open.pop();
const curX = last[0];
const curY = last[1];
const dist = last[2];
tracker[`${curX}_${curY}`] = dist;
if (curX === GRIDSIZE - 1 && curY === GRIDSIZE - 1) {
goalDists.push(dist);
} else {
const right = getRight(curX, curY, dist);
const down = getDown(curX, curY, dist);
const up = getUp(curX, curY, dist);
const left = getLeft(curX, curY, dist);
if (!visited(up) && walkable(up)) {
open.push(up);
}
if (!visited(left) && walkable(left)) {
open.push(left);
}
if (!visited(right) && walkable(right)) {
open.push(right);
}
if (!visited(down) && walkable(down)) {
open.push(down);
}
}
}
function mainLoop() {
if (VISUAL) {
if (!r.WindowShouldClose()) {
setTimeout(() => {
updateFrame();
drawFrame();
mainLoop();
}, 0);
} else {
r.CloseWindow();
Deno.exit();
}
} else {
do {
updateFrame();
} while (open.length > 0);
console.log("Solution: ", currentMinGoalSteps());
}
}
function init() {
r.InitWindow(284, 284, "Day 18 Pt 1");
r.SetTargetFPS(1200);
}
function drawFrame() {
r.BeginDrawing();
r.ClearBackground(r.RAYWHITE);
for (let y = 0; y < 71; y++) {
for (let x = 0; x < 71; x++) {
if (some(x, y)) {
r.DrawRectangle(x * 4, y * 4, 4, 4, r.LIGHTGRAY);
} else if (tracker[`${x}_${y}`]) {
//r.DrawRectangle(x * 4, y * 4, 4, 4, r.BLUE);
}
}
}
for (let i = 0; i < open.length; i++) {
const a = open[i];
r.DrawRectangle(a[0] * 4, a[1] * 4, 4, 4, {
r: 100 + a[2] / 5,
g: 10,
b: 10,
a: 255,
});
}
r.EndDrawing();
}
function some(x, y) {
for (let i = 0; i < LIMIT; i++) {
if (bytes[i][0] === x && bytes[i][1] === y) {
return true;
}
}
return false;
}
function visited(f: number[]) {
const x = f[0];
const y = f[1];
const dist = f[2];
const str = `${x}_${y}`;
if (dist > currentMinGoalSteps()) return true;
if (tracker[str]) {
if (tracker[str] <= dist) {
return true;
}
} else {
return false;
}
}
function currentMinGoalSteps() {
//return tracker['6_6'];
return tracker["70_70"];
}
function walkable(f: number[]) {
const x = f[0];
const y = f[1];
if (x < 0 || x >= GRIDSIZE || y < 0 || y >= GRIDSIZE || some(x, y)) {
return false;
}
return true;
}
function getUp(x: number, y: number, dist: number): number[] {
return [x, y - 1, dist + 1];
}
function getRight(x: number, y: number, dist: number): number[] {
return [x + 1, y, dist + 1];
}
function getDown(x: number, y: number, dist: number): number[] {
return [x, y + 1, dist + 1];
}
function getLeft(x: number, y: number, dist: number): number[] {
return [x - 1, y, dist + 1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment