Created
December 1, 2022 11:47
-
-
Save JordanAdams/22b0c667c2bd2bdd1725cac6133289ca to your computer and use it in GitHub Desktop.
Advent of Code 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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