| 12345678910111213141516171819202122232425262728293031323334 |
- using System.Collections.Generic;
- namespace Day5;
- public class Almanac
- {
- public IList<long> Seeds { get; } = new List<long>();
- public IList<(string Source, string Destination)> Mappings { get; } =
- new List<(string Source, string Destination)>();
- public Dictionary<string, IList<Range>> MappingData { get; } =
- new Dictionary<string, IList<Range>>();
- public IList<long> Map(IList<long> 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;
- }
- private IList<long> MapSingle(IList<long> start, string source)
- {
- return start
- .Select(num => MappingData[source].FirstOrDefault(x => x.IsMatch(num), Range.Identity).Transform(num))
- .ToList();
- }
- }
|