Bladeren bron

Add "functional" solution

ApisNecros 1 jaar geleden
bovenliggende
commit
733fc2d40c
1 gewijzigde bestanden met toevoegingen van 35 en 6 verwijderingen
  1. 35 6
      5/5_1.ts

+ 35 - 6
5/5_1.ts

@@ -1,4 +1,4 @@
-import { ExtractNumbers, Inspect } from "../common.ts";
+import { ExtractNumbers, Inspect, LoadInput } from "../common.ts";
 
 const tests = [
     "seeds: 79 14 55 13",
@@ -36,15 +36,23 @@ const tests = [
     "56 93 4",
 ];
 
-const maps = ParseInput(tests);
+const input = await LoadInput(5);
 
-const demoSeed = FindObjectByID(14, maps.Seeds);
+// Parse the input
+const maps = ParseInput(input);
 
-Inspect(demoSeed);
+// Map the seeds
+const seeds = maps.Seeds.map((seed) => MapSeed(seed.ID, maps));
 
-const demoSoil = FindObjectByID(demoSeed?.ID || 0, maps.SeedToSoil);
+// Get the output
+let closestLocationID = Number.MAX_SAFE_INTEGER;
+if (isSeedMapArray(seeds)) {
+     seeds.forEach((seed) => {
+        if (seed.LocationID < closestLocationID) { closestLocationID = seed.LocationID; }
+    });
+}
 
-Inspect(demoSoil);
+console.log(`The lowest location ID found is: ${closestLocationID}`);
 
 function ParseInput(almanac: string[]): RangeMaps {
     /** An empty initializer for our output */
@@ -177,6 +185,27 @@ function MapSeed(seedID: number, rangeMaps: RangeMaps): SeedMap|undefined {
     return seed;
 }
 
+/**
+ * Type guard function to ensure an array is an array of SeedMaps
+ *
+ * @param {any[]} valueArray The array to check
+ * @returns {boolean} Whether the input is an array of SeedMaps or not
+ */
+function isSeedMapArray(valueArray: any[]): valueArray is SeedMap[] {
+    const value = valueArray.shift();
+
+    if (!value || typeof value !== "object") { return false; }
+
+    return Object.hasOwn(value, "ID")
+        && Object.hasOwn(value, "SoilID")
+        && Object.hasOwn(value, "FertilizerID")
+        && Object.hasOwn(value, "WaterID")
+        && Object.hasOwn(value, "LightID")
+        && Object.hasOwn(value, "TemperatureID")
+        && Object.hasOwn(value, "HumidityID")
+        && Object.hasOwn(value, "LocationID");
+}
+
 /** Any object with a unique identifier */
 interface IdentifiableObject {
     /** The ID number for this object */