Kaynağa Gözat

Add day 5 part ii solution

ApisNecros 1 yıl önce
ebeveyn
işleme
fa4c87d9de
1 değiştirilmiş dosya ile 66 ekleme ve 0 silme
  1. 66 0
      5/5_2.ts

+ 66 - 0
5/5_2.ts

@@ -0,0 +1,66 @@
+import { Inspect, LoadInput } from "../common.ts";
+import { ParseInput, MapSeed, isSeedMapArray } from "./5_common.ts"
+
+const tests = [
+    "seeds: 79 14 55 13",
+    "",
+    "seed-to-soil map:",
+    "50 98 2",
+    "52 50 48",
+    "",
+    "soil-to-fertilizer map:",
+    "0 15 37",
+    "37 52 2",
+    "39 0 15",
+    "",
+    "fertilizer-to-water map:",
+    "49 53 8",
+    "0 11 42",
+    "42 0 7",
+    "57 7 4",
+    "",
+    "water-to-light map:",
+    "88 18 7",
+    "18 25 70",
+    "",
+    "light-to-temperature map:",
+    "45 77 23",
+    "81 45 19",
+    "68 64 13",
+    "",
+    "temperature-to-humidity map:",
+    "0 69 1",
+    "1 0 69",
+    "",
+    "humidity-to-location map:",
+    "60 56 37",
+    "56 93 4",
+];
+
+const input = await LoadInput(5);
+
+// Parse the input
+const maps = ParseInput(input);
+
+// Initialize closest location ID
+let closestLocationID = Number.MAX_SAFE_INTEGER;
+
+// Parse & map the seed ranges
+do {
+    console.log("Seed ranges remaining: ", maps.Seeds.length);
+    
+    const seedRangeStart = maps.Seeds.shift() || -1;
+    const seedRangeEnd = maps.Seeds.shift() || -1;
+
+    if (seedRangeStart == -1 || seedRangeEnd == -1) {
+        throw new Error("seeds out of range!");
+    }
+
+    for(let i = 0; i < seedRangeEnd; i++) {
+        const seedMap = MapSeed(seedRangeStart + i, maps);
+
+        if (seedMap && seedMap.LocationID < closestLocationID) { closestLocationID = seedMap.LocationID; }
+    }
+} while (maps.Seeds.length);
+
+console.log(`The lowest location ID found is: ${closestLocationID}`);