Implemented Day 6 part 1 and 2 as well as their tests

This commit is contained in:
Jean-Francois Gilsdorf 2023-12-13 16:06:17 +01:00
parent 4f77341a7c
commit a5a47fb773
7 changed files with 144 additions and 1 deletions

View File

@ -13,6 +13,7 @@
<None Remove="Inputs\Day3.txt" />
<None Remove="Inputs\Day4.txt" />
<None Remove="Inputs\Day5.txt" />
<None Remove="Inputs\Day6.txt" />
</ItemGroup>
<ItemGroup>
@ -21,6 +22,7 @@
<EmbeddedResource Include="Inputs\Day3.txt" />
<EmbeddedResource Include="Inputs\Day4.txt" />
<EmbeddedResource Include="Inputs\Day5.txt" />
<EmbeddedResource Include="Inputs\Day6.txt" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,62 @@
using AdventOfCode2023.Day5;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AdventOfCode2023.Day6
{
public class Day6_1 : DayBase
{
string input = string.Empty;
Regex rxTime = new Regex(@"(?<=Time:[\s\d]+)\d+");
Regex rxDistance = new Regex(@"(?<=Distance:[\s\d]+)\d+");
public Day6_1()
{
input = GetInput("Day6.txt");
}
public Day6_1(string testInput)
{
input = testInput;
}
public override string Execute()
{
ulong winningMovesProduct = 0;
var times = rxTime.Matches(input).Select(x => int.Parse(x.Value)).ToArray();
var distances = rxDistance.Matches(input).Select(x => int.Parse(x.Value)).ToArray();
List<Race> races = new List<Race>();
for (int i = 0; i < times.Count(); i++)
{
races.Add(new Race(times[i], distances[i]));
}
foreach (var race in races)
{
var winningMoves = Enumerable.Range(0, race.Time).Count(x => x * (race.Time - x) > race.Distance);
winningMovesProduct = winningMovesProduct == 0 ? (ulong)winningMoves : (ulong)winningMoves * winningMovesProduct;
}
return winningMovesProduct.ToString();
}
}
internal class Race
{
public int Distance { get; }
public int Time { get; }
public Race(int time, int distance)
{
Time = time;
Distance = distance;
}
}
}

View File

@ -0,0 +1,55 @@
using AdventOfCode2023.Day5;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AdventOfCode2023.Day6
{
public class Day6_2 : DayBase
{
string input = string.Empty;
Regex rxTime = new Regex(@"(?<=Time:[\s\d]+)\d+");
Regex rxDistance = new Regex(@"(?<=Distance:[\s\d]+)\d+");
public Day6_2()
{
input = GetInput("Day6.txt");
}
public Day6_2(string testInput)
{
input = testInput;
}
public override string Execute()
{
ulong winningMovesProduct = 0;
var times = rxTime.Matches(input).Select(x => int.Parse(x.Value)).ToArray();
var distances = rxDistance.Matches(input).Select(x => int.Parse(x.Value)).ToArray();
var bigTime = ulong.Parse(string.Join("", times));
var bigDistance = ulong.Parse(string.Join("", distances));
var winningMoves = CreateRange(0, bigTime).Count(x => x * (bigTime - x) > bigDistance);
winningMovesProduct = winningMovesProduct == 0 ? (ulong)winningMoves : (ulong)winningMoves * winningMovesProduct;
return winningMovesProduct.ToString();
}
public IEnumerable<ulong> CreateRange(ulong start, ulong count)
{
var limit = start + count;
while (start < limit)
{
yield return start;
start++;
}
}
}
}

View File

@ -0,0 +1,2 @@
Time: 44 70 70 80
Distance: 283 1134 1134 1491

View File

@ -3,6 +3,7 @@ using AdventOfCode2023.Day2;
using AdventOfCode2023.Day3;
using AdventOfCode2023.Day4;
using AdventOfCode2023.Day5;
using AdventOfCode2023.Day6;
namespace AdventOfCode2023
{
@ -19,6 +20,8 @@ namespace AdventOfCode2023
new Day4_2(),
new Day5_1(),
new Day5_2(),
new Day6_1(),
new Day6_2(),
};
private static void ListDays()

View File

@ -124,5 +124,23 @@ namespace AdventOfCode2023Tests
}
#endregion
#region Day 6
[Test]
public void Day6_1Test()
{
var day6_1 = new Day6_1("Time: 7 15 30\r\nDistance: 9 40 200");
Assert.That(day6_1.Execute(), Is.EqualTo("288"));
}
[Test]
public void Day6_2Test()
{
var day6_2 = new Day6_2("Time: 7 15 30\r\nDistance: 9 40 200");
Assert.That(day6_2.Execute(), Is.EqualTo("71503"));
}
#endregion
}
}

View File

@ -3,4 +3,5 @@ global using AdventOfCode2023.Day1;
global using AdventOfCode2023.Day2;
global using AdventOfCode2023.Day3;
global using AdventOfCode2023.Day4;
global using AdventOfCode2023.Day5;
global using AdventOfCode2023.Day5;
global using AdventOfCode2023.Day6;