Skip to content

Instantly share code, notes, and snippets.

@spacecowb0y
Last active December 1, 2023 23:59
Show Gist options
  • Save spacecowb0y/48cca7bd8103815a5c788eed8236649a to your computer and use it in GitHub Desktop.
Save spacecowb0y/48cca7bd8103815a5c788eed8236649a to your computer and use it in GitHub Desktop.
AOC 2023
interface Solutions {
part1: number;
part2: number;
}
export const solve = async (): Promise<{ solutions: Solutions }> => {
const input: string[] = (
await fetch(`https://adventofcode.com/2023/day/1/input`, {
headers: {
cookie: `session=${process.env.SESSION_TOKEN}`,
},
}).then((res) => res.text())
).match(/(\S*)\n/g) || [];
const part1 = (input: string[]): number =>
input
.map((line) => line.split("").map(Number).filter(Boolean))
.reduce((sum, arr) => (sum += arr[0] * 10 + arr[arr.length - 1]), 0);
const part2 = (input: string[]): number =>
input
.map((line) =>
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
.reduce(
(acc, word, index) =>
acc.replaceAll(word, word + (index + 1) + word),
line
)
.split("")
.map(Number)
.filter(Boolean)
.map((_, i, arr) => arr[0] * 10 + arr[arr.length - 1])
.reduce((_, value) => value)
)
.reduce((sum, value) => sum + value, 0);
return {
solutions: {
part1: part1(input),
part2: part2(input),
},
};
};
console.log(await solve());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment