-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb.test.ts
70 lines (58 loc) · 1.89 KB
/
b.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { expect, test } from "bun:test";
function solution(input: string) {
const [seedsString, ...mapStrings] = input.split("\n\n");
const seeds = seedsString
.split(":")[1]
.trim()
.split(" ")
.map((n) => Number(n));
const maps = mapStrings.map((map) =>
map
.split("\n")
.slice(1)
.map((string) => string.split(" ").map((n) => Number(n))),
);
const possibleSeeds = [];
for (let i = 0; i < maps.length; i++) {
const map = maps[i];
for (const [d] of map) {
let curr = d;
for (let k = i; k >= 0; k--) {
const innerMap = maps[k];
const instr = innerMap.find((instr) => curr >= instr[0] && curr < instr[0] + instr[2]);
if (instr) {
curr = instr[1] - instr[0] + curr;
}
}
if (seeds.some((seed, i) => i % 2 === 0 && curr >= seed && curr < seed + seeds[i + 1])) {
possibleSeeds.push(curr);
}
}
}
const locations = possibleSeeds.map((seed) => {
let source = seed;
for (const map of maps) {
const instr = map.find((instr) => source >= instr[1] && source < instr[1] + instr[2]);
if (instr) {
source = instr[0] - instr[1] + source;
}
}
return source;
});
const ans = Math.min(...locations);
return ans;
}
test("example", async () => {
const file = Bun.file("./05/example.txt");
const input = await file.text();
const actual = solution(input);
const expected = 46;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file("./05/input.txt");
const input = await file.text();
const actual = solution(input);
const expected = 125742456;
expect(actual).toBe(expected);
});