summaryrefslogtreecommitdiff
path: root/aoc_2023/day-05/part_2.ts
blob: 4d833c05a85e180ba713b61077e7d895fa041b76 (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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { JSONHashMap } from "@/utils";

export const infinity = Math.pow(2, 42);

export type Pair = [number, number];

const getIntervals = (lines: string[]) => {
  const maps: number[][][] = [];

  for (const line of lines.slice(1, lines.length)) {
    if (!line) continue;

    if (line.includes("to")) {
      maps.push([]);
      continue;
    }

    const currMap = maps.at(-1);
    const [dest, source, range] = line.split(" ").map((x) => parseInt(x));
    if (currMap) {
      currMap.push([dest, source, range]);
    }
  }

  return maps;
};

const constructPiecewiseFn = (intervals: number[][]) => {
  const fn: JSONHashMap<Pair, Pair> = new JSONHashMap();

  let fromIntervals: Pair[] = [];

  for (const [dest, source, range] of intervals) {
    const fromI = [source, source + range - 1];
    fromIntervals.push([source, source + range - 1]);
    fn.set(fromI as Pair, [dest, dest + range - 1]);
  }

  fromIntervals = fromIntervals.sort(
    ([source_a], [source_b]) => source_a - source_b
  );

  for (let i = 0; i < fromIntervals.length - 1; i++) {
    const [_start, end] = fromIntervals[i];
    const [nextStart, _nextEnd] = fromIntervals[i + 1];

    if (end !== nextStart - 1) {
      fn.set([end + 1, nextStart - 1], [end + 1, nextStart - 1]);
    }
  }

  if (fromIntervals[0][0] !== 0) {
    const [start, _end] = fromIntervals[0];
    fn.set([0, start - 1], [0, start - 1]);
  }
  const final: Pair = [fromIntervals.at(-1)![1] + 1, infinity];
  fn.set(final, final);

  return fn
    .keys()
    .map((range) => [range, fn.get(range)!] as [Pair, Pair])
    .sort(([[source_a]], [[source_b]]) => source_a - source_b);
};

export const compose = (f: [Pair, Pair][], g: [Pair, Pair][]) => {
  const composed: [Pair, Pair][] = [];

  for (const [fDomain, fRange] of f) {
    const fOffset = fRange[0] - fDomain[0];

    for (const [gDomain, gRange] of g) {
      const gOffset = gRange[0] - gDomain[0];

      const start = Math.max(fDomain[0], gDomain[0] - fOffset);
      const end = Math.min(fDomain[1], gDomain[1] - fOffset);

      if (start > end) continue;

      composed.push([
        [start, end],
        [
          start + fOffset + gOffset,
          Math.min(end + fOffset + gOffset, infinity),
        ],
      ]);
    }
  }

  return composed;
};

export const main = async (lines: string[]): Promise<number | string> => {
  const seeds = lines[0]
    .split(":")
    .at(1)!
    .split(" ")
    .filter((x) => x)
    .map((x) => parseInt(x));

  const seedIntervals: Pair[] = [];
  for (let i = 0; i < seeds.length; i += 2) {
    const [seed, offset] = [seeds[i], seeds[i + 1]];
    seedIntervals.push([seed, seed + offset]);
  }

  const intervals = getIntervals(lines);

  const finalComposition: [Pair, Pair][] = intervals
    .slice(1)
    .reduce(
      (acc, x) => compose(acc, constructPiecewiseFn(x)),
      constructPiecewiseFn(intervals[0])
    );

  let min = infinity;
  for (const [start, end] of seedIntervals) {
    for (const [domain, range] of finalComposition) {
      if (start <= domain[1] && domain[0] <= end) {
        const begin = Math.max(domain[0], start);
        const last = Math.min(domain[1], end);

        const seedRangeMin = Math.min(
          range[0] + (begin - domain[0]),
          range[0] + (last - domain[0])
        );
        min = Math.min(min, seedRangeMin);
      }
    }
  }

  return min;
};

//

const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
if (isrun) {
  const file = Bun.file("./problem.txt");
  const text = await file.text();
  const lines = text.split("\n").filter((x) => x && x.length);

  console.log("=== COMPUTATION ===\n");

  const answer = await main(lines);

  console.log("\n=== /COMPUTATION ===\n");

  console.log("=== ANSWER TO P2 ===");
  console.log(answer);
}