-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.js
103 lines (89 loc) · 2.21 KB
/
part2.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const fs = require("fs");
const contents = fs
.readFileSync("./input.txt", "utf8")
.split("\n\n")
.filter((x) => x.length);
const seedVals = contents.shift().split(": ")[1].split(" ");
const seeds = [];
for (let i = 0; i < seedVals.length; i += 2) {
seeds.push({
start: +seedVals[i],
length: +seedVals[i + 1],
});
}
const createRange = (line) => {
const items = line.split(" ");
const range = {
dest: +items[0],
src: +items[1],
range: +items[2],
};
return range;
};
const createNegRanges = (ranges) => {
ranges.sort((a, b) => a.src - b.src);
let start = 0;
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
if (range.src > start) {
ranges.splice(i, 0, {
src: start,
dest: start,
range: range.src - start,
});
i++;
}
start = range.src + range.range;
}
return ranges;
};
const parseMap = (data) => {
const contents = data.split("\n").filter((x) => x);
const [from, _, to] = contents.shift().split(" ")[0].split("-");
return {
from: from,
to: to,
map: contents.map(createRange),
};
};
const walk = (value, range, name, map) => {
if (map[name] === undefined) {
return [value, range];
}
const item = map[name];
const rangeItem = item.map.find(
(x) => x.src <= value && value < x.src + x.range,
);
if (rangeItem) {
const diff = value - rangeItem.src;
const newVal = rangeItem.dest + diff;
return walk(newVal, Math.min(range, rangeItem.range - diff), item.to, map);
}
return walk(value, 1, item.to, map);
};
const parsed = contents.map((x) => parseMap(x));
parsed.forEach((p) => {
p.map = createNegRanges(p.map);
});
const parsedMap = parsed.reduce((acc, x) => {
acc[x.from] = x;
return acc;
}, {});
let lowest = Infinity;
for (const seed of seeds) {
let remaining = seed.length;
let start = seed.start;
while (remaining > 0) {
const [startLocation, consumed] = walk(start, remaining, "seed", parsedMap);
remaining -= consumed;
start += consumed;
if (consumed > 1) {
console.log("consumed", consumed);
}
if (startLocation < lowest) {
lowest = startLocation;
}
}
console.log("finished", seed);
}
console.log(lowest);