using AdventOfCode.Core; using System; using System.Reflection; using System.Text.RegularExpressions; namespace AdventOfCode.Solutions._2023 { public partial class Day06(InputReader reader) : IChallange { private InputReader _inputReader = reader; public async Task GetSolutionPart1() { var data = (await _inputReader.ReadAsArrayString()).Select(line => GetDigets().Matches(line)).ToArray(); long totalValue = 1; for (int index = 0; index < data[0].Count; index++) { totalValue *= CalculateWays(int.Parse(data[0][index].Value), int.Parse(data[1][index].Value)); } return totalValue.ToString(); } public async Task GetSolutionPart2() { var data = (await _inputReader.ReadAsArrayString()).Select(line => GetDigets().Matches(line.Replace(" ", string.Empty))).ToArray(); return CalculateWays(long.Parse(data[0][0].Value), long.Parse(data[1][0].Value)).ToString(); } private long CalculateWays(long raceTime, long currentDistance) { long distance, waysToWin = 0, time = raceTime; if (raceTime % 2 == 1) { // uneven time = (time + 1) / 2; } else { time /= 2; time++; waysToWin++; } do { distance = time * (raceTime - time); if (distance > currentDistance) waysToWin += 2; time++; } while (distance > currentDistance); return waysToWin; } [GeneratedRegex(@"\d+")] private static partial Regex GetDigets(); } }