summaryrefslogtreecommitdiff
path: root/day-5/script.ts
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-12-05 22:45:14 +0300
committereug-vs <eugene@eug-vs.xyz>2023-12-05 22:45:14 +0300
commit7991dc2e29c4ee94de9d07b898fbe4c3130784bc (patch)
treeb6ca3037adc052a9fcb41e95b7f7eec60aa57955 /day-5/script.ts
parent3d6836a790e1cdd662a323a3eb5b19a6e6e19970 (diff)
downloadaoc-2023-7991dc2e29c4ee94de9d07b898fbe4c3130784bc.tar.gz
feat(day-5): add first part solution
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 });