-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.ts
53 lines (35 loc) · 940 Bytes
/
solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { answer, question, section } from "../utils.ts";
export default function solution(input: string) {
const elves = input
.split("\n\n")
.map(inv =>
inv.split("\n").map(cal => parseInt(cal))
);
function sum(array: number[]) {
return array.reduce((a, b) => a + b, 0);
}
// Part 1
question("Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?");
const elvesCalories = elves.map(sum);
const elvesCaloriesSorted = elvesCalories.sort((a, b) => b - a);
answer(elvesCaloriesSorted[0]);
// Part 2
question("Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?");
answer(sum(elvesCaloriesSorted.slice(0, 3)));
}
const example =
`1000
2000
3000
4000
5000
6000
7000
8000
9000
10000`;
const input = await Deno.readTextFile("./input.txt");
section("Example");
solution(example);
section("Input");
solution(input);