feat(day_1): both exercises done

This commit is contained in:
2025-12-01 09:25:12 +01:00
parent 43bb503253
commit 8a71c89b64
10 changed files with 4944 additions and 33 deletions

14
.editorconfig Normal file
View File

@@ -0,0 +1,14 @@
root = true
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
charset = utf-8
[*.{txt}]
indent_size = 4
[Makefile]
indent_style = tab

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
/node_modules
/vendor
.env
*.orig
*.pyc
*.swp

View File

@@ -1,8 +1,7 @@
# code-advent-2025 # code-advent-2025
Code Advent problems of 2025. [Advent of Code](https://adventofcode.com/) problems of 2025.
| Day | Name | Part 1 | Part 2 | | Day | Name | Part 1 | Part 2 |
| :-: | :--- | :----: | :----: | | :-: | :-------------- | :----: | :----: |
| 1 | | | | | 1 | Secret Entrance | ⭐ | ⭐ |
| 2 | | | |

View File

@@ -4,9 +4,10 @@
"dev": "deno run -R --watch src/main.ts", "dev": "deno run -R --watch src/main.ts",
"today:dev": "deno run -R --watch src/main.ts $(date +%e)", "today:dev": "deno run -R --watch src/main.ts $(date +%e)",
"today": "deno run -R src/main.ts $(date +%e)", "today": "deno run -R src/main.ts $(date +%e)",
"test": "deno test --parallel src", "test": "deno test -R --parallel src",
"lint:commit": "commitlint --edit" "lint:commit": "commitlint --edit"
}, },
"vendor": true,
"imports": { "imports": {
"@commitlint/cli": "npm:@commitlint/cli@^20.1.0", "@commitlint/cli": "npm:@commitlint/cli@^20.1.0",
"@std/assert": "jsr:@std/assert@1", "@std/assert": "jsr:@std/assert@1",

View File

@@ -0,0 +1,20 @@
import { assertEquals } from "@std/assert";
import {
find_zero_count,
find_zero_count_any,
read_instruction_input,
} from "../exercises/day_1.ts";
const FILE_PATH = "src/exercises/assets/day_1_test_input.txt";
Deno.test("Day 1 - A", async () => {
const instructions = await read_instruction_input(FILE_PATH);
const zero_count = find_zero_count(instructions);
assertEquals(zero_count, 3);
});
Deno.test("Day 1 - B", async () => {
const instructions = await read_instruction_input(FILE_PATH);
const zero_count = find_zero_count_any(instructions);
assertEquals(zero_count, 6);
});

View File

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

View File

@@ -1,4 +1,112 @@
export default function FirstDayExerciseName() { type Direction = "L" | "R";
console.log("test_module"); interface Intruction {
return 1; direction: Direction;
quantity: number;
}
const INITIAL_POSITION = 50;
const MAX_POSITION = 99;
export default async function SecretEntrance() {
const instructions = await read_instruction_input(
"src/exercises/assets/day_1_input.txt",
);
const zero_count = find_zero_count(instructions);
const zero_count_any = find_zero_count_any(instructions);
console.log(">> zero_count", zero_count, zero_count_any);
}
function classic_module(left: number, right: number): number {
return ((left % right) + right) % right;
}
export function find_zero_count_any(instructions: Intruction[]): number {
let zero_count = 0;
let current_position = INITIAL_POSITION;
instructions.forEach((instruction) => {
let count = 0;
// NOTE: Ugly solution, mathematics obvious does not work since are all positive numbers and HUGE rotations are allowed
switch (instruction.direction) {
case "L":
while (count < instruction.quantity) {
current_position -= 1;
if (current_position < 0) {
current_position = classic_module(
current_position,
MAX_POSITION + 1,
);
}
if (current_position === 0) {
zero_count += 1;
}
count++;
}
break;
case "R":
while (count < instruction.quantity) {
current_position += 1;
if (current_position > MAX_POSITION) {
current_position = classic_module(
current_position,
MAX_POSITION + 1,
);
}
if (current_position === 0) {
zero_count += 1;
}
count++;
}
break;
}
});
return zero_count;
}
export function find_zero_count(instructions: Intruction[]): number {
let zero_count = 0;
let current_position = INITIAL_POSITION;
instructions.forEach((instruction) => {
switch (instruction.direction) {
case "L":
current_position = classic_module(
current_position - instruction.quantity,
MAX_POSITION + 1,
);
break;
case "R":
current_position = classic_module(
current_position + instruction.quantity,
MAX_POSITION + 1,
);
}
if (current_position === 0) {
zero_count += 1;
}
});
return zero_count;
}
export async function read_instruction_input(
path: string,
): Promise<Intruction[]> {
const txt_input: string = await globalThis.Deno.readTextFile(
path,
);
// NOTE: regex is useles here, since the split is done manually
const data_regex = new RegExp(/([RL][0-9]+)/g);
const data_match = txt_input.match(data_regex);
const instruction_list: Intruction[] = [];
data_match?.forEach((instruction) => {
const direction = instruction.slice(0, 1);
const quantity = instruction.slice(1);
instruction_list.push({
direction: direction as Direction,
quantity: parseInt(quantity),
});
});
return instruction_list;
} }

View File

@@ -1,17 +1,17 @@
async function main(): Promise<number> {
const args = Deno.args;
if (!args || args.length !== 1) {
return Promise.resolve(-1);
}
const day = parseInt(args[0]);
const module = await import(`./exercises/day_${day}.ts`);
module.default();
return Promise.resolve(0);
}
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts // Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) { if (import.meta.main) {
await main(); await main();
}
async function main(): Promise<number> {
const args = globalThis.Deno.args;
if (!args || args.length !== 1) {
return Promise.resolve(-1);
}
const day = parseInt(args[0]);
const module = await import(`./exercises/day_${day}.ts`);
await module.default();
return Promise.resolve(0);
} }

View File

@@ -1,9 +0,0 @@
import { assertEquals } from "@std/assert";
function add(a: number, b: number): number {
return a + b;
}
Deno.test(function addTest() {
assertEquals(add(2, 3), 5);
});