Skip to content

Commit

Permalink
Solve day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Dec 3, 2024
1 parent a6ca4a0 commit 5eec8c6
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 12 deletions.
13 changes: 13 additions & 0 deletions 2022/day01/part1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { lines, loadData, sum } from '@helper';

const input = await loadData();

const parsed = lines(
input,
(b) => {
return sum(lines(b, (l) => parseInt(l)));
},
{ size: 'block' },
);

console.log(Math.max(...parsed));
15 changes: 15 additions & 0 deletions 2022/day01/part2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { lines, loadData, self, sort, sum } from '@helper';

const input = await loadData();

const parsed = lines(
input,
(b) => {
return sum(lines(b, (l) => parseInt(l)));
},
{ size: 'block' },
);

const topThree = sort(parsed, self).reverse().slice(0, 3);

console.log(sum(topThree));
15 changes: 15 additions & 0 deletions 2024/day03/part1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { loadData, mapToInt, sumBy, product } from '@helper';

const input = await loadData();
const instructions = input.match(/mul\(\d{1,3},\d{1,3}\)/g);

if (!instructions) {
throw new Error('Invalid data');
}

console.log(
sumBy(instructions, (group) => {
const factors = mapToInt(group.slice(4, -1).split(','));
return product(factors);
}),
);
32 changes: 32 additions & 0 deletions 2024/day03/part2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { loadData, mapToInt, product } from '@helper';

const input = await loadData();
const instructions = input.match(/mul\(\d{1,3},\d{1,3}\)|do(?:n't)?\(\)/g);

if (!instructions) {
throw new Error('Invalid data');
}

const { result } = instructions.reduce(
(acc, instruction) => {
if (instruction === "don't()") {
return { isEnabled: false, result: acc.result };
}
if (instruction === 'do()') {
return { isEnabled: true, result: acc.result };
}

if (!acc.isEnabled) {
return acc;
}

const factors = mapToInt(instruction.slice(4, -1).split(','));
return {
isEnabled: acc.isEnabled,
result: acc.result + product(factors),
};
},
{ isEnabled: true, result: 0 },
);

console.log(result);
Empty file removed helpers/difference.ts
Empty file.
2 changes: 0 additions & 2 deletions helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { groupBy } from './groupBy';
import { median, average } from './medianAndAverage';
import { isPrime } from './isPrime';
import { lastElementOfList } from './last';
import { numToStr } from './numToStr';
import { filterOutNaN, filterOut } from './filterOutNan';
import { loadData } from './loadData';
import { replaceMultiple } from './replaceAll';
Expand Down Expand Up @@ -47,7 +46,6 @@ export {
average,
isPrime,
lastElementOfList,
numToStr,
filterOutNaN,
filterOut,
replaceMultiple,
Expand Down
1 change: 0 additions & 1 deletion helpers/numToStr.ts

This file was deleted.

14 changes: 14 additions & 0 deletions input/2022/day01.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1000
2000
3000

4000

5000
6000

7000
8000
9000

10000
1 change: 1 addition & 0 deletions input/2024/day03.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
9 changes: 0 additions & 9 deletions tests/helpers/numToStr.test.ts

This file was deleted.

20 changes: 20 additions & 0 deletions tests/helpers/pair.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from 'bun:test';
import { pair } from '@helper';

test('pair', () => {
expect(pair([1, 2, 3, 4, 5])).toEqual([
[1, 2],
[2, 3],
[3, 4],
[4, 5],
]);
expect(pair([])).toEqual([]);
expect(pair([1, 2, 3])).toEqual([
[1, 2],
[2, 3],
]);
expect(pair(['a', 'b', 'c'])).toEqual([
['a', 'b'],
['b', 'c'],
]);
});

0 comments on commit 5eec8c6

Please sign in to comment.