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

Commit

Permalink
use camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Dec 7, 2024
1 parent 8d04e4b commit cc1a8c9
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 94 deletions.
24 changes: 12 additions & 12 deletions day1/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ export function parse(data: string): number[][] {
}

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) => 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);
});
4 changes: 2 additions & 2 deletions day2/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function solve2(data: number[][]): number {
}

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);
});
12 changes: 6 additions & 6 deletions day3/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ export function solve1(data: string): number {
}

export function solve2(data: string): number {
let is_enabled = true;
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);
});
32 changes: 16 additions & 16 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,7 +46,7 @@ class Grid {
return count;
}

is_x_mas_at(x: number, y: number): boolean {
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
Expand All @@ -57,21 +57,21 @@ class Grid {
);
}

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);
});
34 changes: 17 additions & 17 deletions day5/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class DirectedGraph {
return false;
}

topological_sort(): number[] {
topologicalSort(): number[] {
const nodes: Set<number> = new Set();

for (const [from, tos] of this.edges) {
Expand Down Expand Up @@ -129,14 +129,14 @@ class DirectedGraph {
const result = [];

while (parents.size > 0) {
const next_nodes = [...parents.entries()].find(([_, parent]) =>
const nextNodes = [...parents.entries()].find(([_, parent]) =>
parent.size === 0
);
if (next_nodes === undefined) {
if (nextNodes === undefined) {
return [];
}

const node = next_nodes[0];
const node = nextNodes[0];

result.push(node);
parents.delete(node);
Expand All @@ -155,12 +155,12 @@ class DirectedGraph {
}

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) =>
const [rulesStr, pagesStr] = data.trim().split("\n\n");
const rules = rulesStr.split("\n").map((rule) =>
rule.split("|").map((r) => Number(r))
);

const pages = pages_s.split("\n").map((page) => page.split(",").map(Number));
const pages = pagesStr.split("\n").map((page) => page.split(",").map(Number));

return [rules, pages];
}
Expand All @@ -178,8 +178,8 @@ export function solve1(data: [number[][], number[][]]): number {
for (let i = 0; i < page.length - 1; i++) {
pageGraph.addEdge(page[i], page[i + 1]);
}
const top_sort = pageGraph.topological_sort();
if (top_sort.length === 0) {
const topSort = pageGraph.topologicalSort();
if (topSort.length === 0) {
return 0;
} else {
return page[(page.length - 1) / 2];
Expand All @@ -194,21 +194,21 @@ export function solve2(data: [number[][], number[][]]): number {

rules.forEach(([from, to]) => dirGraph.addEdge(from, to));

const topological_sort = dirGraph.topological_sort();
const topologicalSort = dirGraph.topologicalSort();

const topological_index = new Map<number, number>();
topological_sort.forEach((node, index) => topological_index.set(node, index));
const topologicalIndex = new Map<number, number>();
topologicalSort.forEach((node, index) => topologicalIndex.set(node, index));

return pages.map((page) => {
const pageGraph = dirGraph.restrict(new Set(page));

const fixed = pageGraph.topological_sort();
const fixed = pageGraph.topologicalSort();

for (let i = 0; i < page.length - 1; i++) {
pageGraph.addEdge(page[i], page[i + 1]);
}
const top_sort = pageGraph.topological_sort();
if (top_sort.length === 0) {
const topSort = pageGraph.topologicalSort();
if (topSort.length === 0) {
return fixed[(fixed.length - 1) / 2];
} else {
return 0;
Expand All @@ -217,8 +217,8 @@ export function solve2(data: [number[][], number[][]]): number {
}

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 day5/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), 143);
assertEquals(solve2(example_data), 123);
const dataPath = new URL("example.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(dataPath));
assertEquals(solve1(data), 143);
assertEquals(solve2(data), 123);
});
Loading

0 comments on commit cc1a8c9

Please sign in to comment.