Skip to content

Commit

Permalink
day: ⭐ Day 6, 2024 (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
luximus-hunter committed Dec 6, 2024
1 parent 4648916 commit 35c65aa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
AOC_SESSION=sessionCookie
GITHUB_REPO=username/repo
REPO=username/repo
2 changes: 1 addition & 1 deletion .lume/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Calendar } from "./types.ts";

const root = Deno.cwd();
const yearRegex = /^\d{4}$/;
const repositoryUrl = "https://github.com/" + Deno.env.get("GITHUB_REPO");
const repositoryUrl = "https://github.com/" + Deno.env.get("REPO");

const calendars: Calendar[] = [];
for (const dir of Deno.readDirSync(root)) {
Expand Down
69 changes: 69 additions & 0 deletions 2024/6_guard-gallivant/part-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Read the input file and split it into lines
const input = await Deno.readTextFile("./input.txt");
const grid = input.split("\n").map((line) => line.split(""));

// the guard is the first of any of the following characters: ^, v, <, >
const guard = grid
.map((row) => row.find((cell) => "^v<>".includes(cell)))
.find((cell) => cell);

if (!guard) {
throw new Error("No guard found");
}

let guardPosition = {
x: grid.findIndex((row) => row.includes(guard)),
y: grid[grid.findIndex((row) => row.includes(guard))].indexOf(guard),
};

const moveGuard = (position: { x: number; y: number }) => {
const nextPosition = { ...position };
const direction = grid[position.x][position.y]; // ^, v, <, >

const nextPositionOutOfBounds = nextPosition.x < 0 ||
nextPosition.x >= grid.length ||
nextPosition.y < 0 ||
nextPosition.y >= grid[0].length;

if (direction === "^") {
nextPosition.x--;
} else if (direction === "v") {
nextPosition.x++;
} else if (direction === "<") {
nextPosition.y--;
} else if (direction === ">") {
nextPosition.y++;
}

// check if the next position is a wall
if (
!nextPositionOutOfBounds &&
grid[nextPosition.x][nextPosition.y] === "#"
) {
// rotate the guard 90 degrees to the right
const directions = ["^", ">", "v", "<"];
const currentIndex = directions.indexOf(direction);
const nextIndex = (currentIndex + 1) % directions.length;
grid[position.x][position.y] = directions[nextIndex];
} else {
// move the guard
grid[nextPosition.x][nextPosition.y] = direction;
grid[position.x][position.y] = "X";
guardPosition = { ...nextPosition };
}
};

const isGuardInsideBounds = () =>
grid.some((row) => row.includes("^")) ||
grid.some((row) => row.includes("v")) ||
grid.some((row) => row.includes("<")) ||
grid.some((row) => row.includes(">"));

while (isGuardInsideBounds()) {
moveGuard(guardPosition);
}

// count the number of cells visited by the guard
const visitedCells = grid.flat().filter((cell) => cell === "X");

console.log(visitedCells.length);

0 comments on commit 35c65aa

Please sign in to comment.