Skip to content

Commit 912ec34

Browse files
committed
feat: day 01, part 2
1 parent be72a97 commit 912ec34

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
import { describe, it, expect } from "bun:test";
2-
import { findDistance } from "./day-01-historian-hysteria";
2+
import { findDistance, findDistancePart_2 } from "./day-01-historian-hysteria";
3+
4+
const fileTest = Bun.file("./src/day-01-historian-hysteria-input.test.txt");
5+
const contentTest = await fileTest.text();
36

47
const file = Bun.file("./src/day-01-historian-hysteria-input.txt");
58
const content = await file.text();
69

7-
describe("Day 1: Historian Hysteria", async () => {
8-
it("should return the correct distance", () => {
9-
const result = findDistance(content);
10-
const expected = 11;
11-
expect(result).toBe(expected);
12-
expect(typeof content).toBe("string");
10+
describe("Day 1: Historian Hysteria", () => {
11+
describe("part 1", () => {
12+
it("should return the correct distance with test file", () => {
13+
const result = findDistance(contentTest);
14+
const expected = 11;
15+
expect(result).toBe(expected);
16+
expect(typeof content).toBe("string");
17+
});
18+
it("should return the correct distance with real file", () => {
19+
const result = findDistance(content);
20+
const expected = 2192892;
21+
expect(result).toBe(expected);
22+
expect(typeof content).toBe("string");
23+
});
24+
});
25+
describe("part 2", () => {
26+
it("should return the correct distance with test file", () => {
27+
const result = findDistancePart_2(contentTest);
28+
const expected = 31;
29+
expect(result).toBe(expected);
30+
expect(typeof content).toBe("string");
31+
});
32+
it("should return the correct distance", () => {
33+
const result = findDistancePart_2(content);
34+
const expected = 22962826;
35+
expect(result).toBe(expected);
36+
expect(typeof content).toBe("string");
37+
});
1338
});
1439
});

2024/ts/src/day-01-historian-hysteria.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export function findDistance(input: string): Number {
2-
const [column_a, column_b] = input
1+
function transformInputIntoArrays(input: string): [never[], never[]] {
2+
return input
33
.trim()
44
.split("\n")
55
.map((line) => line.split(/\s+/))
@@ -11,6 +11,10 @@ export function findDistance(input: string): Number {
1111
},
1212
[[], []],
1313
);
14+
}
15+
16+
export function findDistance(input: string): Number {
17+
const [column_a, column_b] = transformInputIntoArrays(input);
1418

1519
const sorted_a = column_a.sort();
1620
const sorted_b = column_b.sort();
@@ -22,3 +26,11 @@ export function findDistance(input: string): Number {
2226

2327
return sum_of_arr;
2428
}
29+
30+
export function findDistancePart_2(input: string): Number {
31+
const [column_a, column_b] = transformInputIntoArrays(input);
32+
33+
return column_a
34+
.map((y) => column_b.filter((x) => x === y).length * y)
35+
.reduce((a, b) => a + b, 0);
36+
}

0 commit comments

Comments
 (0)