Skip to content

Instantly share code, notes, and snippets.

@JordanAdams
Created December 1, 2022 11:47
Show Gist options
  • Save JordanAdams/22b0c667c2bd2bdd1725cac6133289ca to your computer and use it in GitHub Desktop.
Save JordanAdams/22b0c667c2bd2bdd1725cac6133289ca to your computer and use it in GitHub Desktop.
Advent of Code 2022
const sum = (xs: number[]): number => xs.reduce((acc, x) => acc + x, 0);
const parseInput = (input: string): number[][] => {
return input
.split(/\n\n/)
.map((chunk) => chunk.split("\n").map((line) => parseInt(line, 10)));
};
export const part1 = (input: string): number => {
const totals = parseInput(input).map((inventory) => sum(inventory));
return Math.max(...totals);
};
export const part2 = (input: string): number => {
const totals = parseInput(input).map((inventory) => sum(inventory));
const sorted = [...totals].sort();
return sum(sorted.slice(-3));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment