Skip to content

Commit 0496d3f

Browse files
committed
AoC2025: day 1 part 1
1 parent bd25eed commit 0496d3f

3 files changed

Lines changed: 40 additions & 41 deletions

File tree

2024-2025/src/2025/day01/day01.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {part1, part2} from "./day01";
22

33
describe('2024 Day 1', () => {
44
test('Part 1', async () => {
5-
expect(await part1('testInput1')).toEqual(11);
6-
expect(await part1('input')).toEqual(3246517);
5+
expect(await part1('testInput1')).toEqual(3);
6+
expect(await part1('input')).toEqual(1105);
77
});
88

9-
test('Part 2', async () => {
10-
expect(await part2('testInput1')).toEqual(31);
11-
expect(await part2('input')).toEqual(29379307);
12-
});
9+
// test('Part 2', async () => {
10+
// expect(await part2('testInput1')).toEqual(31);
11+
// expect(await part2('input')).toEqual(29379307);
12+
// });
1313
});

2024-2025/src/2025/day01/day01.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,42 @@ import path from "node:path";
22
import {readInputLineByLine} from "@utils/io";
33

44
export async function part1(inputFile: string): Promise<number> {
5-
return await day1(inputFile, calcDistanceChecksum);
5+
return await day1(inputFile, countZeroPositions);
66
}
77

88
export async function part2(inputFile: string): Promise<number> {
9-
return await day1(inputFile, calcSimilarityScore);
9+
return await day1(inputFile, countZeroPositions);
1010
}
1111

12-
async function day1(inputFile: string, calcFn: (list1: number[], list2: number[]) => number): Promise<number> {
12+
type MovementType = { dir: string, value: number };
13+
14+
async function day1(inputFile: string, calcFn: ( movements: MovementType[]) => number): Promise<number> {
1315
const inputPath = path.join(__dirname, inputFile);
1416
const lines = await readInputLineByLine(inputPath);
1517

16-
const leftList: number[] = [];
17-
const rightList: number[] = [];
18-
19-
lines.forEach(line => {
20-
const numbers = line.split(/\s+/).map(str => Number.parseInt(str));
21-
leftList.push(numbers[0]);
22-
rightList.push(numbers[1]);
23-
});
24-
25-
leftList.sort();
26-
rightList.sort();
18+
const movements = lines.map(line => {
19+
const dir = line.slice(0, 1);
20+
const value = Number.parseInt(line.slice(1));
21+
return { dir, value };
22+
})
2723

28-
return calcFn(leftList, rightList);
24+
return calcFn(movements);
2925
}
3026

31-
function calcSimilarityScore(leftList: number[], rightList: number[]): number {
32-
let totalSimilarityScore = 0;
33-
leftList.forEach((num) => {
34-
const occurrences = rightList.filter(rightNum => rightNum === num).length;
35-
totalSimilarityScore += num * occurrences;
36-
});
3727

38-
return totalSimilarityScore;
39-
}
28+
function countZeroPositions(movements: MovementType[]): number {
29+
let zeroPassages = 0;
30+
let position = 50;
31+
32+
movements.forEach(movement => {
33+
const newPosition = movement.dir === 'R' ? position + movement.value : position - movement.value;
34+
position = ((newPosition % 100) + 100) % 100; // handles all cases: positive, negative, multiple wraps
4035

41-
function calcDistanceChecksum(leftList: number[], rightList: number[]): number {
42-
let distanceChecksum = 0;
43-
leftList.forEach((num, index) => {
44-
distanceChecksum += Math.abs(num - rightList[index]);
45-
});
36+
if (position === 0) {
37+
zeroPassages++;
38+
}
39+
})
4640

47-
return distanceChecksum;
41+
return zeroPassages;
4842
}
43+
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
3 4
2-
4 3
3-
2 5
4-
1 3
5-
3 9
6-
3 3
1+
L68
2+
L30
3+
R48
4+
L5
5+
R60
6+
L55
7+
L1
8+
L99
9+
R14
10+
L82

0 commit comments

Comments
 (0)