5_2.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Inspect, LoadInput } from "../common.ts";
  2. import { ParseInput, MapSeed, isSeedMapArray } from "./5_common.ts"
  3. const tests = [
  4. "seeds: 79 14 55 13",
  5. "",
  6. "seed-to-soil map:",
  7. "50 98 2",
  8. "52 50 48",
  9. "",
  10. "soil-to-fertilizer map:",
  11. "0 15 37",
  12. "37 52 2",
  13. "39 0 15",
  14. "",
  15. "fertilizer-to-water map:",
  16. "49 53 8",
  17. "0 11 42",
  18. "42 0 7",
  19. "57 7 4",
  20. "",
  21. "water-to-light map:",
  22. "88 18 7",
  23. "18 25 70",
  24. "",
  25. "light-to-temperature map:",
  26. "45 77 23",
  27. "81 45 19",
  28. "68 64 13",
  29. "",
  30. "temperature-to-humidity map:",
  31. "0 69 1",
  32. "1 0 69",
  33. "",
  34. "humidity-to-location map:",
  35. "60 56 37",
  36. "56 93 4",
  37. ];
  38. const input = await LoadInput(5);
  39. // Parse the input
  40. const maps = ParseInput(input);
  41. // Initialize closest location ID
  42. let closestLocationID = Number.MAX_SAFE_INTEGER;
  43. // Parse & map the seed ranges
  44. do {
  45. console.log("Seed ranges remaining: ", maps.Seeds.length);
  46. const seedRangeStart = maps.Seeds.shift() || -1;
  47. const seedRangeEnd = maps.Seeds.shift() || -1;
  48. if (seedRangeStart == -1 || seedRangeEnd == -1) {
  49. throw new Error("seeds out of range!");
  50. }
  51. for(let i = 0; i < seedRangeEnd; i++) {
  52. const seedMap = MapSeed(seedRangeStart + i, maps);
  53. if (seedMap && seedMap.LocationID < closestLocationID) { closestLocationID = seedMap.LocationID; }
  54. }
  55. } while (maps.Seeds.length);
  56. console.log(`The lowest location ID found is: ${closestLocationID}`);