diff --git a/AdventOfCode2023/AdventOfCode2023.csproj b/AdventOfCode2023/AdventOfCode2023.csproj index ef8fcbb..ae4e331 100644 --- a/AdventOfCode2023/AdventOfCode2023.csproj +++ b/AdventOfCode2023/AdventOfCode2023.csproj @@ -13,6 +13,7 @@ + @@ -21,6 +22,7 @@ + diff --git a/AdventOfCode2023/Day6/Day6_1.cs b/AdventOfCode2023/Day6/Day6_1.cs new file mode 100644 index 0000000..6b6a042 --- /dev/null +++ b/AdventOfCode2023/Day6/Day6_1.cs @@ -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 races = new List(); + 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; + } + } +} diff --git a/AdventOfCode2023/Day6/Day6_2.cs b/AdventOfCode2023/Day6/Day6_2.cs new file mode 100644 index 0000000..bfdcb1f --- /dev/null +++ b/AdventOfCode2023/Day6/Day6_2.cs @@ -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 CreateRange(ulong start, ulong count) + { + var limit = start + count; + + while (start < limit) + { + yield return start; + start++; + } + } + } +} diff --git a/AdventOfCode2023/Inputs/Day6.txt b/AdventOfCode2023/Inputs/Day6.txt new file mode 100644 index 0000000..372f59c --- /dev/null +++ b/AdventOfCode2023/Inputs/Day6.txt @@ -0,0 +1,2 @@ +Time: 44 70 70 80 +Distance: 283 1134 1134 1491 \ No newline at end of file diff --git a/AdventOfCode2023/Program.cs b/AdventOfCode2023/Program.cs index 025e5ab..e451ae0 100644 --- a/AdventOfCode2023/Program.cs +++ b/AdventOfCode2023/Program.cs @@ -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() diff --git a/AdventOfCode2023Tests/UnitTests.cs b/AdventOfCode2023Tests/UnitTests.cs index a4f5c1f..88334fa 100644 --- a/AdventOfCode2023Tests/UnitTests.cs +++ b/AdventOfCode2023Tests/UnitTests.cs @@ -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 } } \ No newline at end of file diff --git a/AdventOfCode2023Tests/Usings.cs b/AdventOfCode2023Tests/Usings.cs index 9c238db..ea345b2 100644 --- a/AdventOfCode2023Tests/Usings.cs +++ b/AdventOfCode2023Tests/Usings.cs @@ -3,4 +3,5 @@ global using AdventOfCode2023.Day1; global using AdventOfCode2023.Day2; global using AdventOfCode2023.Day3; global using AdventOfCode2023.Day4; -global using AdventOfCode2023.Day5; \ No newline at end of file +global using AdventOfCode2023.Day5; +global using AdventOfCode2023.Day6; \ No newline at end of file