public static long Part2(IEnumerable <string> busIds) { if (busIds == null) { throw new ArgumentNullException(nameof(busIds)); } IEnumerable <(long, int)> validBusIdsWithIndexes = busIds .Select((busId, i) => (busId, i)) .Where(t => t.Item1 != "x") .Select(t => (long.Parse(t.Item1, CultureInfo.InvariantCulture), t.Item2)); return(ChineseRemainderTheorem.Solve( validBusIdsWithIndexes.Select(t => t.Item1).ToArray(), validBusIdsWithIndexes.Select(t => t.Item1 - t.Item2).ToArray() )); }
static long Part2(string[] lines) { int i = -1; List <Congruence> congruences = new List <Congruence>(); foreach (var l in lines[1].Split(',')) { i++; if (l == "x") { continue; } int m = int.Parse(l); congruences.Add(new Congruence(m - i, int.Parse(l))); } return(ChineseRemainderTheorem.Solve(congruences)); }
static void Main(string[] args) { var input = File.ReadAllLines("Input.txt"); int time = Convert.ToInt32(input[0]); var busses = new List <int>(); foreach (var bus in input[1].Split(',')) { if (Int32.TryParse(bus, out int busnum)) { busses.Add(busnum); } } var nextBus = (from busNumber in busses select(Number: busNumber, Time: GetNextBusTime(time, busNumber))) .OrderBy(x => x.Time).First(); var part1Answer = (nextBus.Time - time) * nextBus.Number; Console.WriteLine($"Part1: {part1Answer}"); // Part2: Notes // Start at first bus arrival from index 1. This is `t` // Take the next bus, look up the index. Figure out if t + index is divisible by busNumber // If true, keep going untill all numbers are exhausted // Recursion? -- No. // If false increment t by first bus number and try again // Forget all above, use chinese remained theroem copypasta var valueOffset = input[1].Split(',') .Select((value, offset) => (value, offset)) .Where(x => x.value != "x") .Select(x => (Value: Convert.ToInt64(x.value), Offset: x.offset)); var n = valueOffset.Select(x => x.Value).ToArray(); var a = valueOffset.Select(x => x.Value - x.Offset).ToArray(); a[0] = 0; var part2Answer = ChineseRemainderTheorem.Solve(n, a); Console.WriteLine($"Part2: {part2Answer}"); }