₍^. .^₎⟆

This commit is contained in:
Leonard Lorenz 2025-12-02 22:20:36 +01:00
commit f33dda8916
10 changed files with 158 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
.DS_Store
.session
.tool_versions
input

1
.tool-versions Normal file
View file

@ -0,0 +1 @@
deno 2.5.6

24
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"name": "Launch Program",
"type": "node",
"program": "${workspaceFolder}/main.ts",
"cwd": "${workspaceFolder}",
"env": {},
"runtimeExecutable": "/Users/mtrx/.asdf/shims/deno",
"runtimeArgs": [
"run",
"--unstable",
"--inspect-wait",
"--allow-all"
],
"attachSimplePort": 9229
}
]
}

8
deno.json Normal file
View file

@ -0,0 +1,8 @@
{
"tasks": {
"dev": "deno run --watch --allow-read=. main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1"
}
}

5
load-todays-input.bash Normal file
View file

@ -0,0 +1,5 @@
day=$(date +%-e)
day_zero=$(date +%0e)
session=$(cat .sessioncookie)
mkdir -p input
curl -b "session=${session}" -o "./input/${day_zero}.txt" "https://adventofcode.com/2024/day/${day}/input"

22
main.ts Normal file
View file

@ -0,0 +1,22 @@
import type { Solvable } from "./solvable.ts";
const solutionFiles = Deno.readDirSync('./solutions');
const solvables: Solvable[] = [];
// need to wrap this stuff in a function to not trip the top level await
(async () => {
// cool dynamic module loading
for (const solution of solutionFiles) {
await import('./solutions/' + solution.name)
}
// only solve the latest day
console.log("Solving latest day only:")
const day = solvables.toReversed()[0];
day.part1()
day.part2()
})()
export { solvables }

59
solutions/01.ts Normal file
View file

@ -0,0 +1,59 @@
import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
class DayOne implements Solvable {
input = readInput('01')
public part1(): DayOne {
const vals: [string, number][] = this.input
.split("\n")
.map(line => [line[0], Number.parseInt(line.slice(1))]);
let state = 50;
let zerooccurences = 0;
for (const [direction, amount] of vals) {
if (direction === 'R') {
state += amount % 100;
state %= 100;
} else if (direction === 'L') {
state -= amount % 100;
if (state < 0) state += 100;
}
state === 0 ? zerooccurences++ : null;
}
console.log(zerooccurences)
return this
}
public part2(): DayOne {
const vals: [string, number][] = this.input
.split("\n")
.map(line => [line[0], Number.parseInt(line.slice(1))]);
let state = 50;
let zerooccurences = 0;
for (const [direction, amount] of vals) {
for (let i = 0; i < amount; i++) {
state === 0 ? zerooccurences++ : null;
if (direction === 'R') {
state += 1;
if (state === 100) {
state = 0;
}
} else if (direction === 'L') {
state -= 1;
if (state === -1) {
state = 99;
}
}
}
}
console.log(zerooccurences)
return this
}
}
solvables.push(new DayOne())

22
solutions/02.ts Normal file
View file

@ -0,0 +1,22 @@
import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
class DayOne implements Solvable {
input = readInput('01')
public part1(): DayOne {
const vals: [string, number][] = this.input
.split("\n")
.map(line => [line[0], Number.parseInt(line.slice(1))]);
return this
}
public part2(): DayOne {
return this
}
}
solvables.push(new DayOne())

6
solvable.ts Normal file
View file

@ -0,0 +1,6 @@
interface Solvable {
part1(): Solvable;
part2(): Solvable;
}
export type { Solvable }

7
utils.ts Normal file
View file

@ -0,0 +1,7 @@
function readInput(day: string): string {
return Deno.readTextFileSync('./input/' + day + '.txt').trim()
}
const delay = (ms: number) => new Promise(res => setTimeout(res, ms));
export { readInput, delay };