Skip to content

Commit

Permalink
feat: add 2022 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
artemtam committed Dec 3, 2024
1 parent 65d2cbf commit 9d13ace
Show file tree
Hide file tree
Showing 45 changed files with 12,504 additions and 0 deletions.
2,253 changes: 2,253 additions & 0 deletions 2022/day-01/input.txt

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions 2022/day-01/solution.test.ts
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);
})


43 changes: 43 additions & 0 deletions 2022/day-01/solution.ts
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 };


Loading

0 comments on commit 9d13ace

Please sign in to comment.