Skip to content

Commit c2674a3

Browse files
committed
chore: 2022 stuff
1 parent d80cc04 commit c2674a3

File tree

6 files changed

+4827
-0
lines changed

6 files changed

+4827
-0
lines changed

2022/01/code.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { readFileSync } from 'fs'
2+
import { join } from 'path'
3+
4+
const data = readFileSync(join(__dirname, 'input.txt'), { encoding: 'utf-8', flag: 'r' })
5+
const elfCalories = data.split('\r\n\r\n').map((line) => line.split('\r\n'))
6+
7+
function part1() {
8+
const caloriesPerElf = elfCalories.map((calories) => calories.reduce((acc, curr) => acc + parseInt(curr), 0))
9+
const highestCalories = Math.max(...caloriesPerElf)
10+
return highestCalories
11+
}
12+
13+
function part2() {
14+
const caloriesPerElf = elfCalories.map((calories) => calories.reduce((acc, curr) => acc + parseInt(curr), 0))
15+
const topThreeCalories = caloriesPerElf.sort((a, b) => b - a).slice(0, 3)
16+
const sum = topThreeCalories.reduce((acc, curr) => acc + curr, 0)
17+
return sum
18+
}
19+
20+
console.log('Part 1:', part1())
21+
console.log('Part 2:', part2())

2022/01/example.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1000
2+
2000
3+
3000
4+
5+
4000
6+
7+
5000
8+
6000
9+
10+
7000
11+
8000
12+
9000
13+
14+
10000

0 commit comments

Comments
 (0)