summaryrefslogtreecommitdiff
path: root/day-5/script.ts
blob: f0eb76ee5c4e4279a7033048ff97f0e916439982 (plain)
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
import fs from "fs";

const input = fs.readFileSync("./input.txt").toString();

const [seedStr, ...mapStrings] = input.split("\n\n");
export function chunks<T>(data: Array<T>, chunkSize: number) {
  return data.reduce((acc, value, index) => {
    // Initialize a new chunk
    if (index % chunkSize === 0) acc.push([]);
    acc[acc.length - 1].push(value);
    return acc;
  }, [] as T[][]);
}

const seedRanges = chunks(seedStr.match(/\d+/g)?.map(Number) || [], 2).map(
  ([start, size]) => {
    return { start, size };
  },
);

const maps = mapStrings.map((mapString) =>
  mapString
    .split("\n")
    .slice(1)
    .filter((x) => x.length)
    .map((s) => s.match(/\d+/g)?.map(Number) || [])
    .map(([destinationStart, sourceStart, rangeSize]) => ({
      sourceStart,
      destinationStart,
      rangeSize,
    })),
);

// We dont need to inspect whole seed ranges, instead we only select "interesting points"
// Interesting points lie at starts/end of ranges defined in our maps
const seeds = maps
  .flatMap((ranges) =>
    ranges.flatMap((r) => [r.sourceStart, r.destinationStart + 1]),
  )
  .filter((breakpoint) =>
    seedRanges.find(
      (range) =>
        breakpoint >= range.start && breakpoint < range.start + range.size,
    ),
  );

const locationNumbers = seeds.map((seed) =>
  maps.reduce((currentNumber, mapRules) => {
    const validRule = mapRules.find(
      (rule) =>
        currentNumber >= rule.sourceStart &&
        currentNumber < rule.sourceStart + rule.rangeSize,
    );

    if (!validRule) return currentNumber;
    return currentNumber - validRule.sourceStart + validRule.destinationStart;
  }, seed),
);

const result = locationNumbers.reduce((acc, x) => Math.min(acc, x), Infinity);

console.log({ result });