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

Commit

Permalink
feat: day 8 (#11)
Browse files Browse the repository at this point in the history
* use camelCase

* day 8 setup

* day 8 done
  • Loading branch information
rnbguy authored Dec 8, 2024
1 parent a0ec146 commit dfdcd96
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
6 changes: 3 additions & 3 deletions day2/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function parse(data: string): number[][] {
);
}

function is_safe(report: number[]): boolean {
function isSafe(report: number[]): boolean {
const diffs = Array.from(
{ length: report.length - 1 },
(_, i) => report[i + 1] - report[i],
Expand All @@ -20,7 +20,7 @@ function is_safe(report: number[]): boolean {
}

export function solve1(data: number[][]): number {
return data.filter(is_safe)
return data.filter(isSafe)
.length;
}

Expand All @@ -29,7 +29,7 @@ export function solve2(data: number[][]): number {
Array.from(
{ length: report.length },
(_, i) => report.slice(0, i).concat(report.slice(i + 1)),
).some(is_safe)
).some(isSafe)
).length;
}

Expand Down
7 changes: 7 additions & 0 deletions day8/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@scope/day8",
"version": "0.1.0",
"exports": {
".": "./mod.ts"
}
}
12 changes: 12 additions & 0 deletions day8/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
68 changes: 68 additions & 0 deletions day8/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export function parse(data: string): string[][] {
return data.trim().split("\n").map((line) => line.trim().split(""));
}

export function solve(rdata: string[][], bound: [number, number]): number {
const data = rdata.map((row) => row.slice());
const height = data.length;
const width = data[0].length;
const charLocs = new Map<string, [number, number][]>();
for (let y = 0; y < width; y++) {
for (let x = 0; x < height; x++) {
const char = data[y][x];
if (char === ".") continue;
if (!charLocs.has(char)) {
charLocs.set(char, []);
}
charLocs.get(char)!.push([x, y]);
}
}

for (const [_, locs] of charLocs) {
for (let i = 0; i < locs.length; i++) {
for (let j = 0; j < locs.length; j++) {
if (i === j) continue;

const [x1, y1] = locs[i];
const [x2, y2] = locs[j];

// point1 -> point2
for (let h = bound[0]; h <= bound[1]; h++) {
const [ax1, ay1] = [x1 + h * (x1 - x2), y1 + h * (y1 - y2)];
if (0 <= ax1 && ax1 < width && 0 <= ay1 && ay1 < height) {
data[ay1][ax1] = "#";
} else {
break;
}
}
}
}
}

let count = 0;

for (let y = 0; y < width; y++) {
for (let x = 0; x < height; x++) {
if (data[y][x] === "#") {
count++;
}
}
}

return count;
}

export function solve1(data: string[][]): number {
return solve(data, [1, 1]);
}

export function solve2(data: string[][]): number {
return solve(data, [0, Infinity]);
}

if (import.meta.main) {
const dataPath = new URL("input.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(dataPath));
console.log(solve1(data));
console.log(solve2(data));
}
9 changes: 9 additions & 0 deletions day8/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 dataPath = new URL("example.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(dataPath));
assertEquals(solve1(data), 14);
assertEquals(solve2(data), 34);
});

0 comments on commit dfdcd96

Please sign in to comment.