-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
12,504 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { test, expect } from 'bun:test'; | ||
|
||
import { solvePart1, solvePart2 } from './solution.ts'; | ||
|
||
const inputFile = Bun.file(import.meta.dir + '/input.txt'); | ||
const input = await inputFile.text(); | ||
|
||
test('part 1', () => { | ||
expect(solvePart1(input)).toEqual(68923); | ||
}) | ||
|
||
test('part 2', () => { | ||
expect(solvePart2(input)).toEqual(200044); | ||
}) | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const solve = (input: string): [number, number] => { | ||
let maxCalories1 = 0; // calories >= 0 | ||
let maxCalories2 = 0; | ||
let maxCalories3 = 0; | ||
|
||
const elvesCalories = input.split('\n\n'); // \n\n = empty line between elves | ||
|
||
elvesCalories.forEach((elfCalories) => { | ||
const elfCaloriesParsed = elfCalories.split('\n'); | ||
|
||
// count the sum of the elf's calories | ||
const elfCaloriesSum = elfCaloriesParsed.reduce((sum, elfCalorie) => { | ||
return sum + Number(elfCalorie); | ||
}, 0); | ||
|
||
if (elfCaloriesSum >= maxCalories1) { | ||
maxCalories3 = maxCalories2; | ||
maxCalories2 = maxCalories1; | ||
maxCalories1 = elfCaloriesSum; | ||
} else if (elfCaloriesSum >= maxCalories2) { | ||
maxCalories3 = maxCalories2; | ||
maxCalories2 = elfCaloriesSum; | ||
} else if (elfCaloriesSum >= maxCalories3) { | ||
maxCalories3 = elfCaloriesSum; | ||
} | ||
}); | ||
|
||
return [maxCalories1, maxCalories1 + maxCalories2 + maxCalories3]; | ||
} | ||
|
||
const solvePart1 = (input: string): number => { | ||
const [maxCalories] = solve(input) | ||
return maxCalories; | ||
}; | ||
|
||
const solvePart2 = (input: string): number => { | ||
const [_, maxCaloriesTop3] = solve(input) | ||
return maxCaloriesTop3; | ||
}; | ||
|
||
export { solvePart1, solvePart2 }; | ||
|
||
|
Oops, something went wrong.