using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; namespace D15._1 { class Program { public class R : StreamReader, IEnumerable { public R(Stream stream) : base(stream) { } public IEnumerator GetEnumerator() { while (true) { var line = ReadLine(); if (line == null) break; yield return line; } } } static void Main(string[] args) { List<(int positions, int position)> discs = new List<(int, int)>(); var regex = new Regex(@"Disc #\d has (?\d+) positions; at time=0, it is at position (?\d+)."); using (var file = new R(File.OpenText(args[0]).BaseStream)) { foreach (string line in file) { var result = regex.Match(line); discs.Add((int.Parse(result.Groups["n"].Value), int.Parse(result.Groups["t"].Value))); } } Console.WriteLine($"Part 1 answer is : {RunMachine(discs)}"); discs.Add((11, 0)); Console.WriteLine($"Part 2 answer is : {RunMachine(discs)}"); } private static int RunMachine(List<(int positions, int position)> discs) { for (int startTime = 0; ; ++startTime) { int ball = 0; foreach (var (positions, position) in discs) { ball++; var t = startTime + ball; if ((position + t) % positions != 0) { ball = 0; break; } } if (ball == discs.Count) return startTime; } } } }