| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System.Text.RegularExpressions;
- namespace Day5;
- public partial class MapParser
- {
- [GeneratedRegex(@"\d+")]
- private partial Regex NumberList();
-
- public Almanac Parse(string inputFile)
- {
- using var reader = File.OpenText(inputFile);
- var almanac = new Almanac();
-
- var line = reader.ReadLine()!;
- if (!line.StartsWith("seeds:"))
- {
- throw new Exception("Expecting the first line to list the seeds");
- }
- foreach (Match m in NumberList().Matches(line.Substring(6)))
- {
- almanac.Seeds.Add(long.Parse(m.Value));
- }
- string source = string.Empty;
-
- while (!reader.EndOfStream)
- {
- line = reader.ReadLine()!;
- if (String.IsNullOrWhiteSpace(line))
- {
- line = reader.ReadLine()!;
- var parts = line.Split(new char[] {'-', ' '},
- StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
- if (parts.Length < 4)
- {
- throw new Exception($"Expected src-to-dst map line: {line}");
- }
- source = parts[0];
- var destination = parts[2];
-
- almanac.Mappings.Add((source, destination));
- }
- else
- {
- var numbers = NumberList().Matches(line).Cast<Match>().Select(x => long.Parse(x.Value)).ToList();
- if (numbers.Count != 3)
- {
- throw new Exception($"Expecting lines to contain exactly 3 numbers, but got: {line}");
- }
- if (!almanac.MappingData.ContainsKey(source))
- {
- almanac.MappingData[source] = new Dictionary<long, long>();
- }
- var dstIndex = numbers[0];
- var srcIndex = numbers[1];
- var lenght = numbers[2];
- for (var i = 0; i < lenght; i++)
- {
- almanac.MappingData[source].Add(srcIndex + i, dstIndex + i);
- }
- }
- }
-
- return almanac;
- }
- }
|