using System.Collections.Generic; namespace Day5; public class Almanac { public IList Seeds { get; } = new List(); public IList<(string Source, string Destination)> Mappings { get; } = new List<(string Source, string Destination)>(); public Dictionary> MappingData { get; } = new Dictionary>(); public IList Map(IList start, string source, string destination) { var result = start; var current = source; while (current != destination) { result = MapSingle(result, current); current = Mappings.First(m => m.Source == current).Destination; } return result; } public IList Map(IList ranges, string source, string destination) { var result = ranges; var current = source; while (current != destination) { var inQueue = new Queue(result); result = new List(); while (inQueue.Count > 0) { var range = inQueue.Dequeue(); foreach (var rangeMapping in MappingData[current].Append(RangeMapping.Identity)) { if (rangeMapping.IsWithin(range)) { result.Add(rangeMapping.Transform(range)); break; } if (rangeMapping.IsOverlap(range)) { foreach (var fragment in rangeMapping.Split(range)) { inQueue.Enqueue(fragment); } break; } } } Console.WriteLine($"{current} => {result.Count}"); current = Mappings.First(m => m.Source == current).Destination; } return result; } private IList MapSingle(IList start, string source) { return start .Select(num => MappingData[source].FirstOrDefault(x => x.IsMatch(num), RangeMapping.Identity).Transform(num)) .ToList(); } }