feat(day_1): both exercises done

This commit is contained in:
2025-12-01 09:25:12 +01:00
parent 43bb503253
commit 900ffc679f
4 changed files with 4887 additions and 22 deletions

View File

@@ -3,6 +3,6 @@
Code Advent problems of 2025.
| Day | Name | Part 1 | Part 2 |
| :-: | :--- | :----: | :----: |
| 1 | | | |
| :-: | :-------------- | :----: | :----: |
| 1 | Secret Entrance | ⭐ | ⭐ |
| 2 | | | |

View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,108 @@
export default function FirstDayExerciseName() {
console.log("test_module");
return 1;
type Direction = "L" | "R";
interface Intruction {
direction: Direction;
quantity: number;
}
const INITIAL_POSITION = 50;
const MAX_POSITION = 99;
export default async function FirstDayExerciseName() {
const instructions = await read_instruction_input();
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;
}
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;
}
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;
}
async function read_instruction_input(): Promise<Intruction[]> {
const txt_input: string = await globalThis.Deno.readTextFile(
"src/exercises/assets/day_1_input.txt",
);
// 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 @@
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
await main();
}
async function main(): Promise<number> {
const args = Deno.args;
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`);
module.default();
await module.default();
return Promise.resolve(0);
}
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
await main();
}