Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
feat: day 6 (#8)
Browse files Browse the repository at this point in the history
* wildcard for workspace members

* day 6 part 1

* use trim

* ignore vscode dir

* count the array

* day 6 part 2
  • Loading branch information
rnbguy authored Dec 6, 2024
1 parent a83bc48 commit 714f54c
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
deno.lock
**/input.txt
.vscode
4 changes: 1 addition & 3 deletions day1/mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export function parse(data: string): number[][] {
const parsed = data.split("\n").map((x) =>
const parsed = data.trim().split("\n").map((x) =>
x.split(" ").map((x) => parseInt(x))
);
return parsed
// Remove the last element, which is an empty string
.slice(0, parsed.length - 1)
// Three spaces are used to separate the numbers
.map((x) => [x[0], x[3]]);
}
Expand Down
5 changes: 1 addition & 4 deletions day2/mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
export function parse(data: string): number[][] {
const parsed = data.split("\n").map((x) =>
return data.trim().split("\n").map((x) =>
x.split(" ").map((x) => parseInt(x))
);
return parsed
// Remove the last element, which is an empty string
.slice(0, parsed.length - 1);
}

function is_safe(report: number[]): boolean {
Expand Down
5 changes: 1 addition & 4 deletions day4/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ const XMAS: string = "XMAS";
const MAS: string = "MAS";

export function parse(data: string): string[] {
const parsed = data.split("\n");
return parsed
// Remove the last element, which is an empty string
.slice(0, parsed.length - 1);
return data.trim().split("\n");
}

class Grid {
Expand Down
5 changes: 2 additions & 3 deletions day5/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ class DirectedGraph {
}
}

export function parse(data_r: string): [number[][], number[][]] {
const data = data_r.trim();
const [rules_s, pages_s, _] = data.split("\n\n");
export function parse(data: string): [number[][], number[][]] {
const [rules_s, pages_s, _] = data.trim().split("\n\n");
const rules = rules_s.split("\n").map((rule) =>
rule.split("|").map((r) => Number(r))
);
Expand Down
7 changes: 7 additions & 0 deletions day6/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@scope/day6",
"version": "0.1.0",
"exports": {
".": "./mod.ts"
}
}
10 changes: 10 additions & 0 deletions day6/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
164 changes: 164 additions & 0 deletions day6/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
class Grid {
data: string[][];
guard: [number, number];
guardDirection: [number, number];

constructor(data: string[][]) {
this.data = data;
this.guard = this.findGuard;
this.guardDirection = [0, -1];
}

get width(): number {
return this.data[0].length;
}

get height(): number {
return this.data.length;
}

inside(x: number, y: number): boolean {
return x >= 0 && x < this.width && y >= 0 && y < this.height;
}

get(x: number, y: number): string {
if (this.inside(x, y)) {
return this.data[y][x];
} else {
return "";
}
}

set(x: number, y: number, value: string) {
if (this.inside(x, y)) {
this.data[y][x] = value;
}
}

get findGuard(): [number, number] {
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
if (this.get(x, y) === "^") {
return [x, y];
}
}
}
return [-1, -1];
}

isWall(x: number, y: number): boolean {
return this.get(x, y) === "#";
}

isVisited(x: number, y: number): boolean {
return this.get(x, y) === "X";
}

changeDirection() {
// [0, -1] -> [1, 0] -> [0, 1] -> [-1, 0] -> [0, -1]
const [x, y] = this.guardDirection;
this.guardDirection = [-y, x];
}

guardPresent(): boolean {
const [x, y] = this.guard;
return this.inside(x, y);
}

guardWalk() {
const [x, y] = this.guard;
const [dx, dy] = this.guardDirection;

const [x_, y_] = [x + dx, y + dy];

if (this.isWall(x_, y_)) {
this.changeDirection();
this.guardWalk();
} else {
this.guard = [x_, y_];
{
this.set(x, y, "X");
this.set(x_, y_, "^");
}
}
}

addObstacle(x: number, y: number) {
this.set(x, y, "#");
}

x_locations(): number[][] {
const locations = [];
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
if (this.isVisited(x, y)) {
locations.push([x, y]);
}
}
}
return locations;
}
}

function clone(data: string[][]): string[][] {
return data.map((row) => {
return row.slice();
});
}

export function parse(data_r: string): string[][] {
const data = data_r.trim();
return data.split("\n").map((line) => {
return line.split("");
});
}

export function solve1(data: string[][]): number {
const grid = new Grid(clone(data));

while (true) {
grid.guardWalk();
if (!grid.guardPresent()) {
break;
}
}

return grid.x_locations().length;
}

export function solve2(data: string[][]): number {
const grid = new Grid(clone(data));

while (true) {
grid.guardWalk();
if (!grid.guardPresent()) {
break;
}
}

const locations = grid.x_locations();

return locations.filter(([x, y]) => {
const grid = new Grid(clone(data));
grid.addObstacle(x, y);
let stepCount = 0;
while (true) {
grid.guardWalk();
if (!grid.guardPresent()) {
break;
}
stepCount++;
if (stepCount > 2 * locations.length) {
return true;
}
}
return false;
}).length;
}

if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(data_path));
console.log(solve1(data));
console.log(solve2(data));
}
9 changes: 9 additions & 0 deletions day6/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assertEquals } from "@std/assert";
import { parse, solve1, solve2 } from "./mod.ts";

Deno.test(async function testExample() {
const example_data_path = new URL("example.txt", import.meta.url).pathname;
const example_data = parse(await Deno.readTextFile(example_data_path));
assertEquals(solve1(example_data), 41);
assertEquals(solve2(example_data), 6);
});
10 changes: 1 addition & 9 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
{
"workspace": {
"members": [
"./day1",
"./day2",
"./day3",
"./day4",
"./day5"
]
},
"workspace": ["./day*"],
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
Expand Down

0 comments on commit 714f54c

Please sign in to comment.