123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- import { ExtractNumbers, Inspect } from "../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 maps = ParseInput(tests);
- const demoSeed = FindObjectByID(14, maps.Seeds);
- Inspect(demoSeed);
- const demoSoil = FindObjectByID(demoSeed?.ID || 0, maps.SeedToSoil);
- Inspect(demoSoil);
- function ParseInput(almanac: string[]): RangeMaps {
-
- const output: RangeMaps = {
- Seeds: [],
- SeedToSoil: [],
- SoilToFertilizer: [],
- FertilizerToWater: [],
- WaterToLight: [],
- LightToTemperature: [],
- TemperatureToHumidity: [],
- HumidityToLocation: [],
- };
- for (let i = 0; i < almanac.length; i++) {
- let line = almanac[i];
-
- if (/^seeds:/.test(line)) {
-
- const seedIDs = ExtractNumbers(line);
-
- seedIDs.forEach((id) => { output.Seeds.push({ID: id}); });
-
- i++;
- }
-
- else if (/^seed\-to/.test(line)) {
- output.SeedToSoil = ParseSourceDestinationRange(++i, almanac);
- }
-
- else if (/^soil\-to/.test(line)) {
- output.SoilToFertilizer = ParseSourceDestinationRange(++i, almanac);
- }
-
- else if (/^fertilizer\-to/.test(line)) {
- output.FertilizerToWater = ParseSourceDestinationRange(++i, almanac);
- }
-
- else if (/^water\-to/.test(line)) {
- output.WaterToLight = ParseSourceDestinationRange(++i, almanac);
- }
-
- else if (/^light\-to/.test(line)) {
- output.LightToTemperature = ParseSourceDestinationRange(++i, almanac);
- }
-
- else if (/^temperature\-to/.test(line)) {
- output.TemperatureToHumidity = ParseSourceDestinationRange(++i, almanac);
- }
-
- else if (/^humidity\-to/.test(line)) {
- output.HumidityToLocation = ParseSourceDestinationRange(++i, almanac);
- }
- }
- return output;
- }
- function ParseSourceDestinationRange(lineNumber: number, almanac: string[]): MappableObject[] {
- const rangeMap: MappableObject[] = [];
- do {
-
- const [destinationRangeStart, sourceRangeStart, rangeLength] = ExtractNumbers(almanac[lineNumber]);
-
-
- for (let idx = 0; idx < rangeLength; idx++) {
- rangeMap.push({
- ID: sourceRangeStart + idx,
- PointsTo: destinationRangeStart + idx,
- });
- }
- } while(almanac[++lineNumber]);
-
- return rangeMap;
- }
- function FindObjectByID(needle: number, haystack: IdentifiableObject[]|MappableObject[]): IdentifiableObject|MappableObject|undefined {
- return haystack.find((obj) => obj.ID == needle);
- }
- function MapSeed(seedID: number, rangeMaps: RangeMaps): SeedMap|undefined {
-
- if(!FindObjectByID(seedID, rangeMaps.Seeds)) { return undefined; }
-
-
- const seed: SeedMap = {
- ID: seedID,
- SoilID: 0,
- FertilizerID: 0,
- WaterID: 0,
- LightID: 0,
- TemperatureID: 0,
- HumidityID: 0,
- LocationID: 0,
- };
- seed.SoilID = (FindObjectByID(seedID, rangeMaps.SeedToSoil) as MappableObject)?.PointsTo || seedID;
- seed.FertilizerID = (FindObjectByID(seed.SoilID, rangeMaps.SoilToFertilizer) as MappableObject)?.PointsTo || seed.SoilID;
- seed.WaterID = (FindObjectByID(seed.FertilizerID, rangeMaps.FertilizerToWater) as MappableObject)?.PointsTo || seed.FertilizerID;
- seed.LightID = (FindObjectByID(seed.WaterID, rangeMaps.WaterToLight) as MappableObject)?.PointsTo || seed.WaterID;
- seed.TemperatureID = (FindObjectByID(seed.LightID, rangeMaps.LightToTemperature) as MappableObject)?.PointsTo|| seed.LightID;
- seed.HumidityID = (FindObjectByID(seed.TemperatureID, rangeMaps.TemperatureToHumidity) as MappableObject)?.PointsTo || seed.TemperatureID;
- seed.LocationID = (FindObjectByID(seed.HumidityID, rangeMaps.HumidityToLocation) as MappableObject)?.PointsTo || seed.HumidityID;
- return seed;
- }
- interface IdentifiableObject {
-
- ID: number,
- };
- interface Seed extends IdentifiableObject {};
- interface MappableObject extends IdentifiableObject {
-
- PointsTo: number,
- };
- type RangeMaps = {
-
- Seeds: Seed[],
-
- SeedToSoil: MappableObject[],
-
- SoilToFertilizer: MappableObject[],
-
- FertilizerToWater: MappableObject[],
-
- WaterToLight: MappableObject[],
-
- LightToTemperature: MappableObject[],
-
- TemperatureToHumidity: MappableObject[],
-
- HumidityToLocation: MappableObject[],
- }
- type SeedMap = {
-
- ID: number,
-
- SoilID: number,
-
- FertilizerID: number,
-
- WaterID: number,
-
- LightID: number,
-
- TemperatureID: number,
-
- HumidityID: number,
-
- LocationID: number,
- }
|