Compare commits
2 commits
bef73c0cb5
...
fb9a36535b
Author | SHA1 | Date | |
---|---|---|---|
fb9a36535b | |||
7bdeb7864b |
6 changed files with 97 additions and 1039 deletions
9
aoc
Executable file
9
aoc
Executable file
|
@ -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
|
1000
day1/input.txt
1000
day1/input.txt
File diff suppressed because it is too large
Load diff
|
@ -1,16 +1,18 @@
|
||||||
const input = await Deno.readTextFile("./input.txt").then(
|
export async function solve() {
|
||||||
|
return await Deno.readTextFile("./input.txt").then(
|
||||||
(f) =>
|
(f) =>
|
||||||
f.split("\n").reduce<[number[], number[]]>(
|
f.split("\n").reduce<[number[], number[]]>(
|
||||||
(
|
(
|
||||||
acc,
|
acc,
|
||||||
cur,
|
cur,
|
||||||
) => [[...acc[0], +cur.split(" ")[0]], [
|
) => [
|
||||||
...acc[1],
|
[...acc[0], +cur.split(" ")[0]],
|
||||||
+cur.split(" ")[1],
|
[...acc[1], +cur.split(" ")[1]],
|
||||||
]],
|
],
|
||||||
[[], []],
|
[[], []],
|
||||||
),
|
),
|
||||||
).then(([first, second]) => [first.toSorted(), second.toSorted()]).then((
|
).then(([first, second]) => [first.toSorted(), second.toSorted()]).then(
|
||||||
[first, second],
|
([first, second]) =>
|
||||||
) => first.reduce((acc, cur, i) => acc + Math.abs(cur - second[i]), 0));
|
first.reduce((acc, cur, i) => acc + Math.abs(cur - second[i]), 0),
|
||||||
console.log("Answer = " + input);
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
const input = await Deno.readTextFile("./input.txt").then(
|
export async function solve() {
|
||||||
|
return await Deno.readTextFile("./input.txt").then(
|
||||||
(f) =>
|
(f) =>
|
||||||
f.split("\n").reduce<[number[], number[]]>(
|
f.split("\n").reduce<[number[], number[]]>(
|
||||||
(
|
(
|
||||||
acc,
|
acc,
|
||||||
cur,
|
cur,
|
||||||
) => [[...acc[0], +cur.split(" ")[0]], [
|
) => [
|
||||||
...acc[1],
|
[...acc[0], +cur.split(" ")[0]],
|
||||||
+cur.split(" ")[1],
|
[...acc[1], +cur.split(" ")[1]],
|
||||||
]],
|
],
|
||||||
[[], []],
|
[[], []],
|
||||||
),
|
),
|
||||||
).then(([first, second]) => [
|
).then(([first, second]) => [
|
||||||
|
@ -18,7 +19,7 @@ const input = await Deno.readTextFile("./input.txt").then(
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
}, {} as Record<number, number>),
|
}, {} as Record<number, number>),
|
||||||
]).then(([first, map]) =>
|
] as const).then(([first, map]) =>
|
||||||
first.reduce((acc, cur) => acc + ((map[cur] ?? 0) * cur), 0)
|
first.reduce((acc, cur) => acc + ((map[cur] ?? 0) * cur), 0)
|
||||||
);
|
);
|
||||||
console.log("Answer = " + input);
|
}
|
||||||
|
|
17
day2/part1.ts
Normal file
17
day2/part1.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
export async function solve() {
|
||||||
|
return await Deno.readTextFile("./input.txt").then((f) =>
|
||||||
|
f.split("\n").filter((l) => {
|
||||||
|
const report = l.split(" ").map((s) => +s);
|
||||||
|
const increasing = report[0] < report[1];
|
||||||
|
let last = report[0];
|
||||||
|
for (const number of report.slice(1)) {
|
||||||
|
if (number > last && !increasing) return false;
|
||||||
|
if (number < last && increasing) return false;
|
||||||
|
if (Math.abs(number - last) < 1 || Math.abs(number - last) > 3) return false;
|
||||||
|
|
||||||
|
last = number;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}).reduce(acc => acc + 1, 0)
|
||||||
|
);
|
||||||
|
}
|
29
day2/part2.ts
Normal file
29
day2/part2.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
export async function solve() {
|
||||||
|
return await Deno.readTextFile("./input.txt").then((f) =>
|
||||||
|
f.split("\n").filter((l) => {
|
||||||
|
const report = l.split(" ").map((s) => +s);
|
||||||
|
for (let i = 0; i < report.length; i++) {
|
||||||
|
const sliced = [
|
||||||
|
...report.slice(0, i),
|
||||||
|
...report.slice(i + 1, report.length),
|
||||||
|
];
|
||||||
|
const increasing = sliced[0] < sliced[1];
|
||||||
|
let last = sliced[0];
|
||||||
|
let failed = false;
|
||||||
|
for (const number of sliced.slice(1)) {
|
||||||
|
if (number > last && !increasing) failed = true;
|
||||||
|
if (number < last && increasing) failed = true;
|
||||||
|
if (
|
||||||
|
Math.abs(number - last) < 1 ||
|
||||||
|
Math.abs(number - last) > 3
|
||||||
|
) failed = true;
|
||||||
|
|
||||||
|
last = number;
|
||||||
|
}
|
||||||
|
if (failed) continue;
|
||||||
|
else return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}).reduce((acc) => acc + 1, 0)
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in a new issue