feat(day_1): both exercises done
This commit is contained in:
@@ -3,6 +3,6 @@
|
|||||||
Code Advent problems of 2025.
|
Code Advent problems of 2025.
|
||||||
|
|
||||||
| Day | Name | Part 1 | Part 2 |
|
| Day | Name | Part 1 | Part 2 |
|
||||||
| :-: | :--- | :----: | :----: |
|
| :-: | :-------------- | :----: | :----: |
|
||||||
| 1 | | | |
|
| 1 | Secret Entrance | ⭐ | ⭐ |
|
||||||
| 2 | | | |
|
| 2 | | | |
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,108 @@
|
|||||||
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 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;
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/main.ts
14
src/main.ts
@@ -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> {
|
async function main(): Promise<number> {
|
||||||
const args = Deno.args;
|
const args = globalThis.Deno.args;
|
||||||
if (!args || args.length !== 1) {
|
if (!args || args.length !== 1) {
|
||||||
return Promise.resolve(-1);
|
return Promise.resolve(-1);
|
||||||
}
|
}
|
||||||
const day = parseInt(args[0]);
|
const day = parseInt(args[0]);
|
||||||
|
|
||||||
const module = await import(`./exercises/day_${day}.ts`);
|
const module = await import(`./exercises/day_${day}.ts`);
|
||||||
module.default();
|
await module.default();
|
||||||
|
|
||||||
return Promise.resolve(0);
|
return Promise.resolve(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
|
|
||||||
if (import.meta.main) {
|
|
||||||
await main();
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user