-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.ts
50 lines (45 loc) · 1.23 KB
/
index2.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
interface SeedRange {
start: number;
end: number;
}
const data = await Bun.file("input.txt").text();
const lines = data.split("\n\n");
const seeds = lines[0]
.split(":")[1]
.trim()
.split(" ")
.map((seed) => parseInt(seed));
const steps = lines
.slice(1)
.map((line) => line.split(":"))
.filter((line) => line.length > 0)
.map((x) => x[1].trim().split("\n"))
.map((x) => x.map((y) => y.split(" ").map(Number)));
let seedRange: SeedRange[] = [];
for (let i = 0; i < seeds.length; i += 2) {
seedRange.push({ start: seeds[i], end: seeds[i] + seeds[i + 1] });
}
const seedExist = (seed: number) =>
seedRange.some(({ start, end }) => seed >= start && seed <= end);
const getSeedByLocation = (location: number) => {
let result = location;
for (const item of [...steps].reverse()) {
for (const [destination, source, range] of item) {
if (destination <= result && destination + range > result) {
result = source + (result - destination);
break;
}
}
}
return result;
};
console.time("Part 2");
console.log("Start");
for (let i = 0; i < Number.MAX_SAFE_INTEGER; i++) {
const seed = getSeedByLocation(i);
if (seedExist(seed)) {
console.log(i);
break;
}
}
console.timeEnd("Part 2");