summaryrefslogtreecommitdiff
path: root/day-5/script.ts
diff options
context:
space:
mode:
Diffstat (limited to 'day-5/script.ts')
-rw-r--r--day-5/script.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/day-5/script.ts b/day-5/script.ts
new file mode 100644
index 0000000..2d18477
--- /dev/null
+++ b/day-5/script.ts
@@ -0,0 +1,37 @@
+import fs from "fs";
+
+const input = fs.readFileSync("./input.txt").toString();
+
+const [seedStr, ...mapStrings] = input.split("\n\n");
+
+const seeds = seedStr.match(/\d+/g)?.map(Number) || [];
+
+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,
+ })),
+);
+
+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 });