Almanac.cs 985 B

12345678910111213141516171819202122232425262728293031323334
  1. using System.Collections.Generic;
  2. namespace Day5;
  3. public class Almanac
  4. {
  5. public IList<long> Seeds { get; } = new List<long>();
  6. public IList<(string Source, string Destination)> Mappings { get; } =
  7. new List<(string Source, string Destination)>();
  8. public Dictionary<string, IList<Range>> MappingData { get; } =
  9. new Dictionary<string, IList<Range>>();
  10. public IList<long> Map(IList<long> start, string source, string destination)
  11. {
  12. var result = start;
  13. var current = source;
  14. while (current != destination)
  15. {
  16. result = MapSingle(result, current);
  17. current = Mappings.First(m => m.Source == current).Destination;
  18. }
  19. return result;
  20. }
  21. private IList<long> MapSingle(IList<long> start, string source)
  22. {
  23. return start
  24. .Select(num => MappingData[source].FirstOrDefault(x => x.IsMatch(num), Range.Identity).Transform(num))
  25. .ToList();
  26. }
  27. }