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

Commit

Permalink
feat: day 4 (#6)
Browse files Browse the repository at this point in the history
* add code coverage

* rm redundant task

* day 4

* refactor

* write permission for pr comment

* corner case
  • Loading branch information
rnbguy authored Dec 4, 2024
1 parent 326d944 commit 4b35e49
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 17 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/deno.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ jobs:
test:
name: Test
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- run: deno test --allow-read
- run: |
deno test --allow-read --coverage=cov_profile
deno coverage --lcov --output=cov.lcov cov_profile
- uses: hrishikesh-kadam/setup-lcov@v1
- uses: zgosalvez/github-actions-report-lcov@v3
with:
coverage-files: cov.lcov
minimum-coverage: 60
github-token: ${{ secrets.GITHUB_TOKEN }}
update-comment: true
6 changes: 3 additions & 3 deletions day1/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function process_data(data: string): number[][] {
export function parse(data: string): number[][] {
const parsed = data.split("\n").map((x) =>
x.split(" ").map((x) => parseInt(x))
);
Expand Down Expand Up @@ -46,6 +46,6 @@ export function solve2(data: number[][]): number {
if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = await Deno.readTextFile(data_path);
console.log(solve1(process_data(data)));
console.log(solve2(process_data(data)));
console.log(solve1(parse(data)));
console.log(solve2(parse(data)));
}
6 changes: 3 additions & 3 deletions day1/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { assertEquals } from "@std/assert";
import { process_data, solve1, solve2 } from "./mod.ts";
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 = await Deno.readTextFile(example_data_path);
assertEquals(solve1(process_data(example_data)), 11);
assertEquals(solve2(process_data(example_data)), 31);
assertEquals(solve1(parse(example_data)), 11);
assertEquals(solve2(parse(example_data)), 31);
});
1 change: 1 addition & 0 deletions day2/example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
1 2 2 9 4
6 changes: 3 additions & 3 deletions day2/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function process_data(data: string): number[][] {
export function parse(data: string): number[][] {
const parsed = data.split("\n").map((x) =>
x.split(" ").map((x) => parseInt(x))
);
Expand Down Expand Up @@ -39,6 +39,6 @@ export function solve2(data: number[][]): number {
if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = await Deno.readTextFile(data_path);
console.log(solve1(process_data(data)));
console.log(solve2(process_data(data)));
console.log(solve1(parse(data)));
console.log(solve2(parse(data)));
}
6 changes: 3 additions & 3 deletions day2/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { assertEquals } from "@std/assert";
import { process_data, solve1, solve2 } from "./mod.ts";
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 = await Deno.readTextFile(example_data_path);
assertEquals(solve1(process_data(example_data)), 2);
assertEquals(solve2(process_data(example_data)), 4);
assertEquals(solve1(parse(example_data)), 2);
assertEquals(solve2(parse(example_data)), 4);
});
7 changes: 7 additions & 0 deletions day4/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@scope/day4",
"version": "0.1.0",
"exports": {
".": "./mod.ts"
}
}
10 changes: 10 additions & 0 deletions day4/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
101 changes: 101 additions & 0 deletions day4/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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);
}

class Grid {
grid: string[];
constructor(grid: string[]) {
this.grid = grid;
}

x_len(): number {
return this.grid[0].length;
}

y_len(): number {
return this.grid.length;
}

get(x: number, y: number): string {
return this.grid[y]?.[x] ?? "";
}

find_xmas_at(x: number, y: number): number {
let count = 0;

for (const dx of [-1, 0, 1]) {
for (const dy of [-1, 0, 1]) {
if (dx === 0 && dy === 0) continue;

if (
Array.from({ length: XMAS.length }).every((_, i) => {
const xx = x + dx * i;
const yy = y + dy * i;

return this.get(xx, yy) == XMAS[i];
})
) {
count++;
}
}
}

return count;
}

is_x_mas_at(x: number, y: number): boolean {
return [-1, 1].some((d) => {
return this.get(x - d, y - d) + this.get(x, y) +
this.get(x + d, y + d) == MAS;
}) &&
[-1, 1].some((d) => {
return this.get(x - d, y + d) + this.get(x, y) +
this.get(x + d, y - d) == MAS;
});
}

find_all_xmas(): number {
let count = 0;
for (let x = 0; x < this.x_len(); x++) {
for (let y = 0; y < this.y_len(); y++) {
count += this.find_xmas_at(x, y);
}
}
return count;
}

find_all_x_mas(): number {
let count = 0;
for (let y = 0; y < this.y_len(); y++) {
for (let x = 0; x < this.x_len(); x++) {
if (this.is_x_mas_at(x, y)) {
count++;
}
}
}
return count;
}
}

export function solve1(data: string[]): number {
const grid = new Grid(data);
return grid.find_all_xmas();
}

export function solve2(data: string[]): number {
const grid = new Grid(data);
return grid.find_all_x_mas();
}

if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = await Deno.readTextFile(data_path);
console.log(solve1(parse(data)));
console.log(solve2(parse(data)));
}
9 changes: 9 additions & 0 deletions day4/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 = await Deno.readTextFile(example_data_path);
assertEquals(solve1(parse(example_data)), 18);
assertEquals(solve2(parse(example_data)), 9);
});
6 changes: 2 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
"members": [
"./day1",
"./day2",
"./day3"
"./day3",
"./day4"
]
},
"tasks": {
"dev": "deno run --watch main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
Expand Down

0 comments on commit 4b35e49

Please sign in to comment.