diff --git a/aoc b/aoc new file mode 100755 index 0000000..9392077 --- /dev/null +++ b/aoc @@ -0,0 +1,9 @@ +#!/usr/bin/env zsh +setopt extendedglob + +cd "day$1" + +# Handle deno days +if [[ -n *.ts(#qN) ]]; then + deno eval --ext=ts "await import('./part$2.ts').then(m => m.solve()).then(s => console.log('The solution is', s))" +fi \ No newline at end of file diff --git a/day1/part1.ts b/day1/part1.ts index 8993ac6..d91ca53 100644 --- a/day1/part1.ts +++ b/day1/part1.ts @@ -1,16 +1,18 @@ -const input = await Deno.readTextFile("./input.txt").then( - (f) => - f.split("\n").reduce<[number[], number[]]>( - ( - acc, - cur, - ) => [[...acc[0], +cur.split(" ")[0]], [ - ...acc[1], - +cur.split(" ")[1], - ]], - [[], []], - ), -).then(([first, second]) => [first.toSorted(), second.toSorted()]).then(( - [first, second], -) => first.reduce((acc, cur, i) => acc + Math.abs(cur - second[i]), 0)); -console.log("Answer = " + input); +export async function solve() { + return await Deno.readTextFile("./input.txt").then( + (f) => + f.split("\n").reduce<[number[], number[]]>( + ( + acc, + cur, + ) => [ + [...acc[0], +cur.split(" ")[0]], + [...acc[1], +cur.split(" ")[1]], + ], + [[], []], + ), + ).then(([first, second]) => [first.toSorted(), second.toSorted()]).then( + ([first, second]) => + first.reduce((acc, cur, i) => acc + Math.abs(cur - second[i]), 0), + ); +} diff --git a/day1/part2.ts b/day1/part2.ts index 39446c3..cd21658 100644 --- a/day1/part2.ts +++ b/day1/part2.ts @@ -1,24 +1,25 @@ -const input = await Deno.readTextFile("./input.txt").then( - (f) => - f.split("\n").reduce<[number[], number[]]>( - ( - acc, - cur, - ) => [[...acc[0], +cur.split(" ")[0]], [ - ...acc[1], - +cur.split(" ")[1], - ]], - [[], []], - ), -).then(([first, second]) => [ - first, - second.reduce((acc, cur) => { - if (acc[cur] !== undefined) acc[cur]++; - else acc[cur] = 1; +export async function solve() { + return await Deno.readTextFile("./input.txt").then( + (f) => + f.split("\n").reduce<[number[], number[]]>( + ( + acc, + cur, + ) => [ + [...acc[0], +cur.split(" ")[0]], + [...acc[1], +cur.split(" ")[1]], + ], + [[], []], + ), + ).then(([first, second]) => [ + first, + second.reduce((acc, cur) => { + if (acc[cur] !== undefined) acc[cur]++; + else acc[cur] = 1; - return acc; - }, {} as Record), -]).then(([first, map]) => - first.reduce((acc, cur) => acc + ((map[cur] ?? 0) * cur), 0) -); -console.log("Answer = " + input); + return acc; + }, {} as Record), + ] as const).then(([first, map]) => + first.reduce((acc, cur) => acc + ((map[cur] ?? 0) * cur), 0) + ); +}