69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace AdventOfCode.Solutions._2023
|
|
{
|
|
public partial class Day06 : IChallange
|
|
{
|
|
public int Year => 2023;
|
|
public int Day => 6;
|
|
|
|
private readonly InputReader _inputReader;
|
|
|
|
public Day06(InputReader inputReader)
|
|
{
|
|
_inputReader = inputReader;
|
|
_inputReader.SetInput(this);
|
|
}
|
|
|
|
public async Task<string> 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<string> 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();
|
|
}
|
|
} |