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

Commit

Permalink
chores: refactor (#10)
Browse files Browse the repository at this point in the history
* add deno tasks

* update gitignore

* update workflow file

* refactor

* use camelCase

* add comment

* refactor

* direct return

* use camelCase

* fix ci lcov
  • Loading branch information
rnbguy authored Dec 7, 2024
1 parent 9284994 commit a0ec146
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 174 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/deno.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ jobs:
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- run: |
deno test --allow-read --coverage=cov_profile
deno coverage --lcov --output=cov.lcov cov_profile
- run: deno task test
- run: deno task lcov
- uses: hrishikesh-kadam/setup-lcov@v1
- uses: zgosalvez/github-actions-report-lcov@v3
with:
coverage-files: cov.lcov
coverage-files: cov_profile/cov.lcov
minimum-coverage: 60
github-token: ${{ secrets.GITHUB_TOKEN }}
update-comment: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
deno.lock
**/input.txt
.vscode
cov_profile
32 changes: 15 additions & 17 deletions day1/mod.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
export function parse(data: string): number[][] {
const parsed = data.trim().split("\n").map((x) =>
x.split(" ").map((x) => parseInt(x))
// 3 spaces between the numbers
x.split(" ").map((x) => parseInt(x))
);
return parsed
// Three spaces are used to separate the numbers
.map((x) => [x[0], x[3]]);
.map((x) => [x[0], x[1]]);
}

export function solve1(data: number[][]): number {
const left_nums = data.map((x) => x[0]);
const right_nums = data.map((x) => x[1]);
const leftNums = data.map((x) => x[0]);
const rightNums = data.map((x) => x[1]);

const sorted_left_nums = left_nums.sort((a, b) => a - b);
const sorted_right_nums = right_nums.sort((a, b) => a - b);
const sortedLeftNums = leftNums.sort((a, b) => a - b);
const sortedRightNums = rightNums.sort((a, b) => a - b);

return Array.from({ length: data.length }, (_, i) => i)
.reduce(
(acc, i) => acc + Math.abs(sorted_left_nums[i] - sorted_right_nums[i]),
(acc, i) => acc + Math.abs(sortedLeftNums[i] - sortedRightNums[i]),
0,
);
}

export function solve2(data: number[][]): number {
const left_nums = data.map((x) => x[0]);
const right_nums = data.map((x) => x[1]);
const leftNums = data.map((x) => x[0]);
const rightNums = data.map((x) => x[1]);

const right_count = right_nums.reduce<Record<number, number>>(
const rightCount = rightNums.reduce<Record<number, number>>(
(acc, x) => {
acc[x] = (acc[x] || 0) + 1;
return acc;
},
{},
);

return left_nums.reduce(
(acc, x) => {
return acc + (x * (right_count[x] || 0));
},
return leftNums.reduce(
(acc, x) => acc + (x * (rightCount[x] || 0)),
0,
);
}

if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(data_path));
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));
}
8 changes: 4 additions & 4 deletions day1/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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), 11);
assertEquals(solve2(example_data), 31);
const dataPath = new URL("example.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(dataPath));
assertEquals(solve1(data), 11);
assertEquals(solve2(data), 31);
});
26 changes: 13 additions & 13 deletions day2/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ export function parse(data: string): number[][] {
}

function is_safe(report: number[]): boolean {
const diffs = Array.from({ length: report.length - 1 }, (_, i) => {
return (report[i + 1] - report[i]);
});
const diffs = Array.from(
{ length: report.length - 1 },
(_, i) => report[i + 1] - report[i],
);

if (diffs[0] === 0) {
return false;
}

const direction = diffs[0] / Math.abs(diffs[0]);

return diffs.every((x) => {
return 1 <= x / direction && x / direction <= 3;
});
return diffs.every((x) => 1 <= x / direction && x / direction <= 3);
}

export function solve1(data: number[][]): number {
Expand All @@ -26,16 +25,17 @@ export function solve1(data: number[][]): number {
}

export function solve2(data: number[][]): number {
return data.filter((report) => {
return Array.from({ length: report.length }, (_, i) => {
return report.slice(0, i).concat(report.slice(i + 1));
}).some(is_safe);
}).length;
return data.filter((report) =>
Array.from(
{ length: report.length },
(_, i) => report.slice(0, i).concat(report.slice(i + 1)),
).some(is_safe)
).length;
}

if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(data_path));
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));
}
8 changes: 4 additions & 4 deletions day2/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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), 2);
assertEquals(solve2(example_data), 4);
const dataPath = new URL("example.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(dataPath));
assertEquals(solve1(data), 2);
assertEquals(solve2(data), 4);
});
24 changes: 11 additions & 13 deletions day3/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ export const regex1 = /mul\(([0-9]{1,3}),([0-9]{1,3})\)/g;
export const regex2 = /do(?:n't)?\(\)|mul\(([0-9]{1,3}),([0-9]{1,3})\)/g;

export function solve1(data: string): number {
return data.matchAll(regex1).map((match) => {
const [_, a, b] = match;
return Number(a) * Number(b);
}).reduce((acc, val) => acc + val, 0);
return data.matchAll(regex1).map(([_, a, b]) => Number(a) * Number(b)).reduce(
(acc, val) => acc + val,
0,
);
}

export function solve2(data: string): number {
let is_enabled = true;
return data.matchAll(regex2).map((match) => {
const [m, a, b] = match;

let isEnabled = true;
return data.matchAll(regex2).map(([m, a, b]) => {
if (m === "do()") {
is_enabled = true;
isEnabled = true;
return 0;
} else if (m === "don't()") {
is_enabled = false;
isEnabled = false;
return 0;
} else {
if (is_enabled) {
if (isEnabled) {
return Number(a) * Number(b);
} else return 0;
}
}).reduce((acc, val) => acc + val, 0);
}

if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = await Deno.readTextFile(data_path);
const dataPath = new URL("input.txt", import.meta.url).pathname;
const data = await Deno.readTextFile(dataPath);
console.log(solve1(data));
console.log(solve2(data));
}
12 changes: 6 additions & 6 deletions day3/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { assertEquals } from "@std/assert";
import { solve1, solve2 } from "./mod.ts";

Deno.test(async function testExample() {
const example_data_path_1 = new URL("example1.txt", import.meta.url).pathname;
const example_data_1 = await Deno.readTextFile(example_data_path_1);
assertEquals(solve1(example_data_1), 161);
const dataPath1 = new URL("example1.txt", import.meta.url).pathname;
const data1 = await Deno.readTextFile(dataPath1);
assertEquals(solve1(data1), 161);

const example_data_path_2 = new URL("example2.txt", import.meta.url).pathname;
const example_data_2 = await Deno.readTextFile(example_data_path_2);
assertEquals(solve2(example_data_2), 48);
const dataPath2 = new URL("example2.txt", import.meta.url).pathname;
const data2 = await Deno.readTextFile(dataPath2);
assertEquals(solve2(data2), 48);
});
48 changes: 24 additions & 24 deletions day4/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class Grid {
this.grid = grid;
}

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

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

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

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

for (const dx of [-1, 0, 1]) {
Expand All @@ -46,32 +46,32 @@ class Grid {
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;
});
isXAndMasAt(x: number, y: number): boolean {
return [-1, 1].some((d) =>
this.get(x - d, y - d) + this.get(x, y) +
this.get(x + d, y + d) == MAS
) &&
[-1, 1].some((d) =>
this.get(x - d, y + d) + this.get(x, y) +
this.get(x + d, y - d) == MAS
);
}

find_all_xmas(): number {
findAllXmas(): 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);
for (let x = 0; x < this.width(); x++) {
for (let y = 0; y < this.height(); y++) {
count += this.findXMasAt(x, y);
}
}
return count;
}

find_all_x_mas(): number {
findAllXAndMas(): 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)) {
for (let y = 0; y < this.height(); y++) {
for (let x = 0; x < this.width(); x++) {
if (this.isXAndMasAt(x, y)) {
count++;
}
}
Expand All @@ -82,17 +82,17 @@ class Grid {

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

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

if (import.meta.main) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(data_path));
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));
}
8 changes: 4 additions & 4 deletions day4/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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), 18);
assertEquals(solve2(example_data), 9);
const dataPath = new URL("example.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(dataPath));
assertEquals(solve1(data), 18);
assertEquals(solve2(data), 9);
});
Loading

0 comments on commit a0ec146

Please sign in to comment.