diff --git a/.editorconfig b/.editorconfig index f9b4e8c..0994a8a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,10 +1,9 @@ - [*] # Microsoft .NET properties csharp_indent_braces = false csharp_new_line_before_members_in_object_initializers = false -csharp_new_line_before_open_brace = accessors,anonymous_methods,control_blocks,events,indexers,lambdas,local_functions,methods,properties,types +csharp_new_line_before_open_brace = accessors, anonymous_methods, control_blocks, events, indexers, lambdas, local_functions, methods, properties, types csharp_preferred_modifier_order = private, public, protected, internal, file, new, readonly, abstract, virtual, sealed, static, override, extern, unsafe, volatile, async, required:suggestion csharp_prefer_braces = true:none csharp_preserve_single_line_blocks = true diff --git a/AdventOfCode.Common/DayHelper.cs b/AdventOfCode.Common/DayHelper.cs index 5a9359e..a32bbc5 100644 --- a/AdventOfCode.Common/DayHelper.cs +++ b/AdventOfCode.Common/DayHelper.cs @@ -14,8 +14,8 @@ public static class DayHelper } return null; } - - public static void RunDay(string eventName, int dayNumber) + + private static void RunDay(string eventName, int dayNumber) { Console.WriteLine($"Day {dayNumber:00}"); Console.WriteLine("------"); @@ -63,8 +63,8 @@ public static class DayHelper Console.WriteLine(ex.StackTrace); } } - - public static void RunDay(string eventName) + + private static void RunDay(string eventName) { for (int currentDay = 1; currentDay < 25; currentDay++) { @@ -74,7 +74,7 @@ public static class DayHelper Console.WriteLine(string.Empty); } } - + public static void RunDay(string eventName, string[] args) { if (args.Length > 0) diff --git a/AdventOfCode.slnx b/AdventOfCode.slnx index b76c546..a430248 100644 --- a/AdventOfCode.slnx +++ b/AdventOfCode.slnx @@ -1,13 +1,13 @@  - - - - - - - - - - - + + + + + + + + + + + \ No newline at end of file diff --git a/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj b/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj index d2ccc5d..3c7d317 100644 --- a/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj +++ b/AdventOfCode2017.Tests/AdventOfCode2017.Tests.csproj @@ -1,24 +1,24 @@  - - net8.0 - enable - AdventOfCode2017.Tests - - false - + + net8.0 + enable + AdventOfCode2017.Tests - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - + false + - - - + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + diff --git a/AdventOfCode2017.Tests/Day01_Tests.cs b/AdventOfCode2017.Tests/Day01_Tests.cs index 1fc5b35..a0c6382 100644 --- a/AdventOfCode2017.Tests/Day01_Tests.cs +++ b/AdventOfCode2017.Tests/Day01_Tests.cs @@ -9,7 +9,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "1122", }); + string result = day01.ResolvePart1(["1122"]); Assert.Equal("3", result); } @@ -19,7 +19,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "1111", }); + string result = day01.ResolvePart1(["1111"]); Assert.Equal("4", result); } @@ -29,7 +29,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "1234", }); + string result = day01.ResolvePart1(["1234"]); Assert.Equal("0", result); } @@ -39,7 +39,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "91212129", }); + string result = day01.ResolvePart1(["91212129"]); Assert.Equal("9", result); } @@ -47,13 +47,13 @@ public class Day01_Tests #endregion ResolvePart1 #region ResolvePart2 - + [Fact] public void ResolvePart2__Test1() { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "1212", }); + string result = day01.ResolvePart2(["1212"]); Assert.Equal("6", result); } @@ -63,7 +63,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "1221", }); + string result = day01.ResolvePart2(["1221"]); Assert.Equal("0", result); } @@ -73,7 +73,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "123425", }); + string result = day01.ResolvePart2(["123425"]); Assert.Equal("4", result); } @@ -83,7 +83,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "123123", }); + string result = day01.ResolvePart2(["123123"]); Assert.Equal("12", result); } @@ -93,7 +93,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "12131415", }); + string result = day01.ResolvePart2(["12131415"]); Assert.Equal("4", result); } diff --git a/AdventOfCode2017.Tests/Day02_Tests.cs b/AdventOfCode2017.Tests/Day02_Tests.cs index c28b107..1c8006b 100644 --- a/AdventOfCode2017.Tests/Day02_Tests.cs +++ b/AdventOfCode2017.Tests/Day02_Tests.cs @@ -7,11 +7,11 @@ public class Day02_Tests { Day02 day02 = new(); - string result = day02.ResolvePart1(new[] { + string result = day02.ResolvePart1([ "5 1 9 5", "7 5 3", "2 4 6 8", - }); + ]); Assert.Equal("18", result); } @@ -21,11 +21,11 @@ public class Day02_Tests { Day02 day02 = new(); - string result = day02.ResolvePart2(new[] { + string result = day02.ResolvePart2([ "5 9 2 8", "9 4 7 3", "3 8 6 5", - }); + ]); Assert.Equal("9", result); } diff --git a/AdventOfCode2017.Tests/packages.config b/AdventOfCode2017.Tests/packages.config deleted file mode 100644 index 102a45c..0000000 --- a/AdventOfCode2017.Tests/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/AdventOfCode2017/AdventOfCode2017.csproj b/AdventOfCode2017/AdventOfCode2017.csproj index cdeab94..a9ecb47 100644 --- a/AdventOfCode2017/AdventOfCode2017.csproj +++ b/AdventOfCode2017/AdventOfCode2017.csproj @@ -1,20 +1,20 @@  - - Exe - net8.0 - enable - AdventOfCode2017 - + + Exe + net8.0 + enable + AdventOfCode2017 + - - - PreserveNewest - - + + + PreserveNewest + + - - - + + + diff --git a/AdventOfCode2017/Day01.cs b/AdventOfCode2017/Day01.cs index 833db54..7a1eebf 100644 --- a/AdventOfCode2017/Day01.cs +++ b/AdventOfCode2017/Day01.cs @@ -48,10 +48,10 @@ public class Day01 : IDay { string input = inputs[0]; int value = 0; - for(int i = 0; i < input.Length; i++) + for (int i = 0; i < input.Length; i++) { int nextI = (i + 1) % input.Length; - if(input[i] == input[nextI]) + if (input[i] == input[nextI]) { value += (input[i] - '0'); } diff --git a/AdventOfCode2017/Day02.cs b/AdventOfCode2017/Day02.cs index 0dc6792..a950be2 100644 --- a/AdventOfCode2017/Day02.cs +++ b/AdventOfCode2017/Day02.cs @@ -51,10 +51,10 @@ public class Day02 : IDay public string ResolvePart1(string[] inputs) { int checksum = 0; - foreach(string input in inputs) + foreach (string input in inputs) { int[] row = input - .Split(new[] { " ", " " }, StringSplitOptions.RemoveEmptyEntries) + .Split([" ", " "], StringSplitOptions.RemoveEmptyEntries) .Select(cell => Convert.ToInt32(cell)) .ToArray(); int max = row.Max(); @@ -70,7 +70,7 @@ public class Day02 : IDay foreach (string input in inputs) { int[] row = input - .Split(new[] { " ", " " }, StringSplitOptions.RemoveEmptyEntries) + .Split([" ", " "], StringSplitOptions.RemoveEmptyEntries) .Select(cell => Convert.ToInt32(cell)) .ToArray(); diff --git a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj index 19f19e6..6d9a17c 100644 --- a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj +++ b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj @@ -1,24 +1,24 @@  - - net8.0 - enable - AdventOfCode2018.Tests + + net8.0 + enable + AdventOfCode2018.Tests - false - + false + - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + - - - + + + diff --git a/AdventOfCode2018.Tests/Day01_Tests.cs b/AdventOfCode2018.Tests/Day01_Tests.cs index f9e27b0..da68331 100644 --- a/AdventOfCode2018.Tests/Day01_Tests.cs +++ b/AdventOfCode2018.Tests/Day01_Tests.cs @@ -9,7 +9,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "+1", "-2", "+3", "+1", }); + string result = day01.ResolvePart1(["+1", "-2", "+3", "+1"]); Assert.Equal("3", result); } @@ -19,7 +19,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "+1", "+1", "+1", }); + string result = day01.ResolvePart1(["+1", "+1", "+1"]); Assert.Equal("3", result); } @@ -29,7 +29,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "+1", "+1", "-2", }); + string result = day01.ResolvePart1(["+1", "+1", "-2"]); Assert.Equal("0", result); } @@ -39,7 +39,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart1(new[] { "-1", "-2", "-3", }); + string result = day01.ResolvePart1(["-1", "-2", "-3"]); Assert.Equal("-6", result); } @@ -53,7 +53,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "+1", "-2", "+3", "+1", }); + string result = day01.ResolvePart2(["+1", "-2", "+3", "+1"]); Assert.Equal("2", result); } @@ -63,7 +63,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "+1", "-1", }); + string result = day01.ResolvePart2(["+1", "-1"]); Assert.Equal("0", result); } @@ -73,7 +73,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "+3", "+3", "+4", "-2", "-4", }); + string result = day01.ResolvePart2(["+3", "+3", "+4", "-2", "-4"]); Assert.Equal("10", result); } @@ -83,7 +83,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "-6", "+3", "+8", "+5", "-6", }); + string result = day01.ResolvePart2(["-6", "+3", "+8", "+5", "-6"]); Assert.Equal("5", result); } @@ -93,7 +93,7 @@ public class Day01_Tests { Day01 day01 = new(); - string result = day01.ResolvePart2(new[] { "+7", "+7", "-2", "-7", "-4", }); + string result = day01.ResolvePart2(["+7", "+7", "-2", "-7", "-4"]); Assert.Equal("14", result); } diff --git a/AdventOfCode2018.Tests/Day02_Tests.cs b/AdventOfCode2018.Tests/Day02_Tests.cs index a7b288c..205f264 100644 --- a/AdventOfCode2018.Tests/Day02_Tests.cs +++ b/AdventOfCode2018.Tests/Day02_Tests.cs @@ -7,7 +7,7 @@ public class Day02_Tests { Day02 day02 = new(); - string result = day02.ResolvePart1(new[] { + string result = day02.ResolvePart1([ "abcdef", "bababc", "abbcde", @@ -15,7 +15,7 @@ public class Day02_Tests "aabcdd", "abcdee", "ababab", - }); + ]); Assert.Equal("12", result); } @@ -25,7 +25,7 @@ public class Day02_Tests { Day02 day02 = new(); - string result = day02.ResolvePart2(new[] { + string result = day02.ResolvePart2([ "abcde", "fghij", "klmno", @@ -33,7 +33,7 @@ public class Day02_Tests "fguij", "axcye", "wvxyz", - }); + ]); Assert.Equal("fgij", result); } diff --git a/AdventOfCode2018.Tests/Day03_Tests.cs b/AdventOfCode2018.Tests/Day03_Tests.cs index 47a3685..d5ff34d 100644 --- a/AdventOfCode2018.Tests/Day03_Tests.cs +++ b/AdventOfCode2018.Tests/Day03_Tests.cs @@ -90,17 +90,17 @@ public class Day03_Tests } #endregion Claim_Overlaps - + [Fact] public void ResolvePart1__Test() { Day03 day03 = new(); - string result = day03.ResolvePart1(new[] { + string result = day03.ResolvePart1([ "#1 @ 1,3: 4x4", "#2 @ 3,1: 4x4", "#3 @ 5,5: 2x2", - }); + ]); Assert.Equal("4", result); } @@ -110,11 +110,11 @@ public class Day03_Tests { Day03 day03 = new(); - string result = day03.ResolvePart2(new[] { + string result = day03.ResolvePart2([ "#1 @ 1,3: 4x4", "#2 @ 3,1: 4x4", "#3 @ 5,5: 2x2", - }); + ]); Assert.Equal("3", result); } diff --git a/AdventOfCode2018.Tests/Day04_Tests.cs b/AdventOfCode2018.Tests/Day04_Tests.cs index 058d4a5..1f622d7 100644 --- a/AdventOfCode2018.Tests/Day04_Tests.cs +++ b/AdventOfCode2018.Tests/Day04_Tests.cs @@ -50,7 +50,7 @@ public class Day04_Tests [Fact] public void GuardEvent_FromStringArray__TestBase() { - List guardEvents = Day04.GuardEvent.FromStringArray(new[] { + List guardEvents = Day04.GuardEvent.FromStringArray([ "[1518-11-01 00:00] Guard #10 begins shift", "[1518-11-01 00:05] falls asleep", "[1518-11-01 00:25] wakes up", @@ -59,7 +59,7 @@ public class Day04_Tests "[1518-11-01 23:58] Guard #99 begins shift", "[1518-11-02 00:40] falls asleep", "[1518-11-02 00:50] wakes up", - }); + ]); Assert.Equal(10, guardEvents[0].ID); Assert.Equal(Day04.GuardEventType.ShiftBegin, guardEvents[0].Type); @@ -89,7 +89,7 @@ public class Day04_Tests [Fact] public void GuardEvent_FromStringArray__TestBaseUnsorted() { - List guardEvents = Day04.GuardEvent.FromStringArray(new[] { + List guardEvents = Day04.GuardEvent.FromStringArray([ "[1518-11-01 00:00] Guard #10 begins shift", "[1518-11-01 23:58] Guard #99 begins shift", "[1518-11-01 00:30] falls asleep", @@ -98,7 +98,7 @@ public class Day04_Tests "[1518-11-02 00:50] wakes up", "[1518-11-01 00:55] wakes up", "[1518-11-01 00:25] wakes up", - }); + ]); Assert.Equal(10, guardEvents[0].ID); Assert.Equal(Day04.GuardEventType.ShiftBegin, guardEvents[0].Type); @@ -126,13 +126,13 @@ public class Day04_Tests } #endregion GuardEvent_FromStringArray - + [Fact] public void ResolvePart1__BaseStatement() { Day04 day04 = new(); - string result = day04.ResolvePart1(new[] { + string result = day04.ResolvePart1([ "[1518-11-01 00:00] Guard #10 begins shift", "[1518-11-01 00:05] falls asleep", "[1518-11-01 00:25] wakes up", @@ -150,7 +150,7 @@ public class Day04_Tests "[1518-11-05 00:03] Guard #99 begins shift", "[1518-11-05 00:45] falls asleep", "[1518-11-05 00:55] wakes up", - }); + ]); Assert.Equal("240", result); } @@ -160,7 +160,7 @@ public class Day04_Tests { Day04 day04 = new(); - string result = day04.ResolvePart1(new[] { + string result = day04.ResolvePart1([ "[1518-11-04 00:36] falls asleep", "[1518-11-04 00:46] wakes up", "[1518-11-05 00:03] Guard #99 begins shift", @@ -178,7 +178,7 @@ public class Day04_Tests "[1518-11-01 00:55] wakes up", "[1518-11-01 23:58] Guard #99 begins shift", "[1518-11-05 00:55] wakes up", - }); + ]); Assert.Equal("240", result); } @@ -189,7 +189,7 @@ public class Day04_Tests { Day04 day04 = new(); - string result = day04.ResolvePart2(new[] { + string result = day04.ResolvePart2([ "[1518-11-01 00:00] Guard #10 begins shift", "[1518-11-01 00:05] falls asleep", "[1518-11-01 00:25] wakes up", @@ -207,7 +207,7 @@ public class Day04_Tests "[1518-11-05 00:03] Guard #99 begins shift", "[1518-11-05 00:45] falls asleep", "[1518-11-05 00:55] wakes up", - }); + ]); Assert.Equal("4455", result); } @@ -217,7 +217,7 @@ public class Day04_Tests { Day04 day04 = new(); - string result = day04.ResolvePart2(new[] { + string result = day04.ResolvePart2([ "[1518-11-04 00:36] falls asleep", "[1518-11-04 00:46] wakes up", "[1518-11-05 00:03] Guard #99 begins shift", @@ -235,7 +235,7 @@ public class Day04_Tests "[1518-11-01 00:55] wakes up", "[1518-11-01 23:58] Guard #99 begins shift", "[1518-11-05 00:55] wakes up", - }); + ]); Assert.Equal("4455", result); } diff --git a/AdventOfCode2018.Tests/Day05_Tests.cs b/AdventOfCode2018.Tests/Day05_Tests.cs index 579bbdd..6daee24 100644 --- a/AdventOfCode2018.Tests/Day05_Tests.cs +++ b/AdventOfCode2018.Tests/Day05_Tests.cs @@ -7,7 +7,7 @@ public class Day05_Tests { Day05 day05 = new(); - string result = day05.ResolvePart1(new[] { "dabAcCaCBAcCcaDA" }); + string result = day05.ResolvePart1(["dabAcCaCBAcCcaDA"]); Assert.Equal("10", result); } @@ -17,7 +17,7 @@ public class Day05_Tests { Day05 day05 = new(); - string result = day05.ResolvePart2(new[] { "dabAcCaCBAcCcaDA" }); + string result = day05.ResolvePart2(["dabAcCaCBAcCcaDA"]); Assert.Equal("4", result); } diff --git a/AdventOfCode2018.Tests/Day06_Tests.cs b/AdventOfCode2018.Tests/Day06_Tests.cs index 8181df5..28720a3 100644 --- a/AdventOfCode2018.Tests/Day06_Tests.cs +++ b/AdventOfCode2018.Tests/Day06_Tests.cs @@ -64,14 +64,14 @@ public class Day06_Tests { Day06 day06 = new(); - string result = day06.ResolvePart1(new[] { + string result = day06.ResolvePart1([ "1, 1", "1, 6", "8, 3", "3, 4", "5, 5", "8, 9", - }); + ]); Assert.Equal("17", result); } @@ -79,16 +79,16 @@ public class Day06_Tests [Fact] public void ResolvePart2__Test() { - Day06 day06 = new() { DistanceThresold = 32, }; + Day06 day06 = new() { DistanceThreshold = 32, }; - string result = day06.ResolvePart2(new[] { + string result = day06.ResolvePart2([ "1, 1", "1, 6", "8, 3", "3, 4", "5, 5", "8, 9", - }); + ]); Assert.Equal("16", result); } diff --git a/AdventOfCode2018.Tests/Day07_Tests.cs b/AdventOfCode2018.Tests/Day07_Tests.cs index 83e54ef..7c3c0bf 100644 --- a/AdventOfCode2018.Tests/Day07_Tests.cs +++ b/AdventOfCode2018.Tests/Day07_Tests.cs @@ -7,7 +7,7 @@ public class Day07_Tests { Day07 day07 = new(); - string result = day07.ResolvePart1(new[] { + string result = day07.ResolvePart1([ "Step C must be finished before step A can begin.", "Step C must be finished before step F can begin.", "Step A must be finished before step B can begin.", @@ -15,7 +15,7 @@ public class Day07_Tests "Step B must be finished before step E can begin.", "Step D must be finished before step E can begin.", "Step F must be finished before step E can begin.", - }); + ]); Assert.Equal("CABDFE", result); } @@ -25,7 +25,7 @@ public class Day07_Tests { Day07 day07 = new() { BaseCost = 0, NumberOfWorkers = 2 }; - string result = day07.ResolvePart2(new[] { + string result = day07.ResolvePart2([ "Step C must be finished before step A can begin.", "Step C must be finished before step F can begin.", "Step A must be finished before step B can begin.", @@ -33,7 +33,7 @@ public class Day07_Tests "Step B must be finished before step E can begin.", "Step D must be finished before step E can begin.", "Step F must be finished before step E can begin.", - }); + ]); Assert.Equal("15", result); } diff --git a/AdventOfCode2018.Tests/Day08_Tests.cs b/AdventOfCode2018.Tests/Day08_Tests.cs index e19fda6..979d162 100644 --- a/AdventOfCode2018.Tests/Day08_Tests.cs +++ b/AdventOfCode2018.Tests/Day08_Tests.cs @@ -28,13 +28,13 @@ public class Day08_Tests Assert.Single(result.Childs[1].Childs[0].Metadata); Assert.Equal(99, result.Childs[1].Childs[0].Metadata[0]); } - + [Fact] public void ResolvePart1__Test() { Day08 day = new(); - string result = day.ResolvePart1(new[] { "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", }); + string result = day.ResolvePart1(["2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2"]); Assert.Equal("138", result); } @@ -44,7 +44,7 @@ public class Day08_Tests { Day08 day = new(); - string result = day.ResolvePart2(new[] { "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", }); + string result = day.ResolvePart2(["2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2"]); Assert.Equal("66", result); } diff --git a/AdventOfCode2018.Tests/Day09_Tests.cs b/AdventOfCode2018.Tests/Day09_Tests.cs index 565e436..36a37ab 100644 --- a/AdventOfCode2018.Tests/Day09_Tests.cs +++ b/AdventOfCode2018.Tests/Day09_Tests.cs @@ -3,7 +3,7 @@ public class Day09_Tests { #region MarbleGame_PlayGame - + [Fact] public void MarbleGame_PlayGame__Test1() { @@ -69,15 +69,15 @@ public class Day09_Tests Assert.Equal(37305, highScore); } - + #endregion MarbleGame_PlayGame - + [Fact] public void ResolvePart1__Test1() { Day09 day = new(); - string result = day.ResolvePart1(new[] { "9 players; last marble is worth 25 points" }); + string result = day.ResolvePart1(["9 players; last marble is worth 25 points"]); Assert.Equal("32", result); } @@ -87,7 +87,7 @@ public class Day09_Tests { Day09 day = new(); - string result = day.ResolvePart1(new[] { "10 players; last marble is worth 1618 points" }); + string result = day.ResolvePart1(["10 players; last marble is worth 1618 points"]); Assert.Equal("8317", result); } @@ -97,7 +97,7 @@ public class Day09_Tests { Day09 day = new(); - string result = day.ResolvePart1(new[] { "13 players; last marble is worth 7999 points" }); + string result = day.ResolvePart1(["13 players; last marble is worth 7999 points"]); Assert.Equal("146373", result); } @@ -107,7 +107,7 @@ public class Day09_Tests { Day09 day = new(); - string result = day.ResolvePart1(new[] { "17 players; last marble is worth 1104 points" }); + string result = day.ResolvePart1(["17 players; last marble is worth 1104 points"]); Assert.Equal("2764", result); } @@ -117,7 +117,7 @@ public class Day09_Tests { Day09 day = new(); - string result = day.ResolvePart1(new[] { "21 players; last marble is worth 6111 points" }); + string result = day.ResolvePart1(["21 players; last marble is worth 6111 points"]); Assert.Equal("54718", result); } @@ -127,7 +127,7 @@ public class Day09_Tests { Day09 day = new(); - string result = day.ResolvePart1(new[] { "30 players; last marble is worth 5807 points" }); + string result = day.ResolvePart1(["30 players; last marble is worth 5807 points"]); Assert.Equal("37305", result); } diff --git a/AdventOfCode2018.Tests/Day10_Tests.cs b/AdventOfCode2018.Tests/Day10_Tests.cs index 9ce7c98..0abfd29 100644 --- a/AdventOfCode2018.Tests/Day10_Tests.cs +++ b/AdventOfCode2018.Tests/Day10_Tests.cs @@ -7,7 +7,7 @@ public class Day10_Tests { Day10 day = new() { Width = 12, Height = 10 }; - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "position=< 9, 1> velocity=< 0, 2>", "position=< 7, 0> velocity=<-1, 0>", "position=< 3, -2> velocity=<-1, 1>", @@ -39,7 +39,7 @@ public class Day10_Tests "position=< 5, 9> velocity=< 1, -2>", "position=<14, 7> velocity=<-2, 0>", "position=<-3, 6> velocity=< 2, -1>", - }); + ]); Assert.Equal( Environment.NewLine + "............" + @@ -59,7 +59,7 @@ public class Day10_Tests { Day10 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "position=< 9, 1> velocity=< 0, 2>", "position=< 7, 0> velocity=<-1, 0>", "position=< 3, -2> velocity=<-1, 1>", @@ -91,7 +91,7 @@ public class Day10_Tests "position=< 5, 9> velocity=< 1, -2>", "position=<14, 7> velocity=<-2, 0>", "position=<-3, 6> velocity=< 2, -1>", - }); + ]); Assert.Equal("3", result); } diff --git a/AdventOfCode2018.Tests/Day11_Tests.cs b/AdventOfCode2018.Tests/Day11_Tests.cs index 510003a..ceade8d 100644 --- a/AdventOfCode2018.Tests/Day11_Tests.cs +++ b/AdventOfCode2018.Tests/Day11_Tests.cs @@ -59,7 +59,7 @@ public class Day11_Tests public void ResolvePart2__Test1() { Day11 day = new(); - string result = day.ResolvePart2(new[] { "18" }); + string result = day.ResolvePart2(["18"]); Assert.Equal("90,269,16", result); } @@ -67,7 +67,7 @@ public class Day11_Tests public void ResolvePart2__Test2() { Day11 day = new(); - string result = day.ResolvePart2(new[] { "42" }); + string result = day.ResolvePart2(["42"]); Assert.Equal("232,251,12", result); } } \ No newline at end of file diff --git a/AdventOfCode2018.Tests/Day12_Tests.cs b/AdventOfCode2018.Tests/Day12_Tests.cs index 14f2095..c87c905 100644 --- a/AdventOfCode2018.Tests/Day12_Tests.cs +++ b/AdventOfCode2018.Tests/Day12_Tests.cs @@ -7,8 +7,7 @@ public class Day12_Tests { Day12 day = new(); - string result = day.ResolvePart1(new[] - { + string result = day.ResolvePart1([ "initial state: #..#.#..##......###...###", "", "...## => #", @@ -25,7 +24,7 @@ public class Day12_Tests "###.. => #", "###.# => #", "####. => #", - }); + ]); Assert.Equal("325", result); } diff --git a/AdventOfCode2018.Tests/Day13_Tests.cs b/AdventOfCode2018.Tests/Day13_Tests.cs index 6d97f53..5b2b5c4 100644 --- a/AdventOfCode2018.Tests/Day13_Tests.cs +++ b/AdventOfCode2018.Tests/Day13_Tests.cs @@ -7,7 +7,7 @@ public class Day13_Tests { Day13 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "|", "v", "|", @@ -15,7 +15,7 @@ public class Day13_Tests "|", "^", "|", - }); + ]); Assert.Equal("0,3", result); } @@ -25,14 +25,14 @@ public class Day13_Tests { Day13 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ @"/->-\ ", @"| | /----\", @"| /-+--+-\ |", @"| | | | v |", @"\-+-/ \-+--/", @" \------/ ", - }); + ]); Assert.Equal("7,3", result); } @@ -42,7 +42,7 @@ public class Day13_Tests { Day13 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ @"/>-<\ ", @"| | ", @"| /<+-\", @@ -50,7 +50,7 @@ public class Day13_Tests @"\>+/", - }); + ]); Assert.Equal("6,4", result); } diff --git a/AdventOfCode2018.Tests/Day14_Tests.cs b/AdventOfCode2018.Tests/Day14_Tests.cs index ef9f327..302a96a 100644 --- a/AdventOfCode2018.Tests/Day14_Tests.cs +++ b/AdventOfCode2018.Tests/Day14_Tests.cs @@ -9,7 +9,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart1(new[] { "9", }); + string result = day.ResolvePart1(["9"]); Assert.Equal("5158916779", result); } @@ -19,7 +19,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart1(new[] { "5", }); + string result = day.ResolvePart1(["5"]); Assert.Equal("0124515891", result); } @@ -29,7 +29,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart1(new[] { "18", }); + string result = day.ResolvePart1(["18"]); Assert.Equal("9251071085", result); } @@ -39,7 +39,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart1(new[] { "2018", }); + string result = day.ResolvePart1(["2018"]); Assert.Equal("5941429882", result); } @@ -53,7 +53,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart2(new[] { "51589", }); + string result = day.ResolvePart2(["51589"]); Assert.Equal("9", result); } @@ -63,7 +63,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart2(new[] { "01245", }); + string result = day.ResolvePart2(["01245"]); Assert.Equal("5", result); } @@ -73,7 +73,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart2(new[] { "92510", }); + string result = day.ResolvePart2(["92510"]); Assert.Equal("18", result); } @@ -83,7 +83,7 @@ public class Day14_Tests { Day14 day = new(); - string result = day.ResolvePart2(new[] { "59414", }); + string result = day.ResolvePart2(["59414"]); Assert.Equal("2018", result); } diff --git a/AdventOfCode2018.Tests/Day15_Tests.cs b/AdventOfCode2018.Tests/Day15_Tests.cs index 9a185f6..f16d511 100644 --- a/AdventOfCode2018.Tests/Day15_Tests.cs +++ b/AdventOfCode2018.Tests/Day15_Tests.cs @@ -9,7 +9,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "#######", "#.G...#", "#...EG#", @@ -17,7 +17,7 @@ public class Day15_Tests "#..G#E#", "#.....#", "#######", - }); + ]); Assert.Equal("27730", result); } @@ -27,7 +27,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "#######", "#G..#E#", "#E#E.E#", @@ -35,17 +35,17 @@ public class Day15_Tests "#...#E#", "#...E.#", "#######", - }); + ]); Assert.Equal("36334", result); } - + [Fact] public void ResolvePart1__Test3() { Day15 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "#######", "#E..EG#", "#.#G.E#", @@ -53,7 +53,7 @@ public class Day15_Tests "#G..#.#", "#..E#.#", "#######", - }); + ]); Assert.Equal("39514", result); } @@ -63,7 +63,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "#######", "#E.G#.#", "#.#G..#", @@ -71,7 +71,7 @@ public class Day15_Tests "#G..#.#", "#...E.#", "#######", - }); + ]); Assert.Equal("27755", result); } @@ -81,7 +81,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "#######", "#.E...#", "#.#..G#", @@ -89,7 +89,7 @@ public class Day15_Tests "#E#G#G#", "#...#G#", "#######", - }); + ]); Assert.Equal("28944", result); } @@ -99,7 +99,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "#########", "#G......#", "#.E.#...#", @@ -109,7 +109,7 @@ public class Day15_Tests "#.G...G.#", "#.....G.#", "#########", - }); + ]); Assert.Equal("18740", result); } @@ -123,7 +123,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "#######", "#.G...#", "#...EG#", @@ -131,17 +131,17 @@ public class Day15_Tests "#..G#E#", "#.....#", "#######", - }); + ]); Assert.Equal("4988", result); } - + [Fact] public void ResolvePart2__Test3() { Day15 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "#######", "#E..EG#", "#.#G.E#", @@ -149,7 +149,7 @@ public class Day15_Tests "#G..#.#", "#..E#.#", "#######", - }); + ]); Assert.Equal("31284", result); } @@ -159,7 +159,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "#######", "#E.G#.#", "#.#G..#", @@ -167,7 +167,7 @@ public class Day15_Tests "#G..#.#", "#...E.#", "#######", - }); + ]); Assert.Equal("3478", result); } @@ -177,7 +177,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "#######", "#.E...#", "#.#..G#", @@ -185,7 +185,7 @@ public class Day15_Tests "#E#G#G#", "#...#G#", "#######", - }); + ]); Assert.Equal("6474", result); } @@ -195,7 +195,7 @@ public class Day15_Tests { Day15 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "#########", "#G......#", "#.E.#...#", @@ -205,7 +205,7 @@ public class Day15_Tests "#.G...G.#", "#.....G.#", "#########", - }); + ]); Assert.Equal("1140", result); } diff --git a/AdventOfCode2018.Tests/Day16_Tests.cs b/AdventOfCode2018.Tests/Day16_Tests.cs index 70e89ca..adcf537 100644 --- a/AdventOfCode2018.Tests/Day16_Tests.cs +++ b/AdventOfCode2018.Tests/Day16_Tests.cs @@ -7,7 +7,7 @@ public class Day16_Tests { Day16 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "Before: [3, 2, 1, 1]", "9 2 1 2", "After: [3, 2, 2, 1]", @@ -18,7 +18,7 @@ public class Day16_Tests "", "", "Garbage", - }); + ]); Assert.Equal("2", result); } diff --git a/AdventOfCode2018.Tests/Day23_Tests.cs b/AdventOfCode2018.Tests/Day23_Tests.cs index fa4814f..585e722 100644 --- a/AdventOfCode2018.Tests/Day23_Tests.cs +++ b/AdventOfCode2018.Tests/Day23_Tests.cs @@ -7,7 +7,7 @@ public class Day23_Tests { Day23 day = new(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "pos=<0,0,0>, r=4", "pos=<1,0,0>, r=1", "pos=<4,0,0>, r=3", @@ -17,7 +17,7 @@ public class Day23_Tests "pos=<1,1,1>, r=1", "pos=<1,1,2>, r=1", "pos=<1,3,1>, r=1", - }); + ]); Assert.Equal("7", result); } @@ -27,14 +27,14 @@ public class Day23_Tests { Day23 day = new(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "pos=<10,12,12>, r=2", "pos=<12,14,12>, r=2", "pos=<16,12,12>, r=4", "pos=<14,14,14>, r=6", "pos=<50,50,50>, r=200", "pos=<10,10,10>, r=5", - }); + ]); Assert.Equal("36", result); } diff --git a/AdventOfCode2018.Tests/packages.config b/AdventOfCode2018.Tests/packages.config index e17ebef..a036fd9 100644 --- a/AdventOfCode2018.Tests/packages.config +++ b/AdventOfCode2018.Tests/packages.config @@ -1,5 +1,5 @@  - - + + \ No newline at end of file diff --git a/AdventOfCode2018/AdventOfCode2018.csproj b/AdventOfCode2018/AdventOfCode2018.csproj index dda2cc7..9a95a67 100644 --- a/AdventOfCode2018/AdventOfCode2018.csproj +++ b/AdventOfCode2018/AdventOfCode2018.csproj @@ -1,20 +1,20 @@  - - Exe - net8.0 - enable - AdventOfCode2018 - + + Exe + net8.0 + enable + AdventOfCode2018 + - - - PreserveNewest - - + + + PreserveNewest + + - - - + + + diff --git a/AdventOfCode2018/Day01.cs b/AdventOfCode2018/Day01.cs index b909c1d..6061a6d 100644 --- a/AdventOfCode2018/Day01.cs +++ b/AdventOfCode2018/Day01.cs @@ -63,8 +63,7 @@ public class Day01 : IDay int accumulator = 0; foreach (string input in inputs) { - int intInput; - if (int.TryParse(input.Substring(1), out intInput)) + if (int.TryParse(input.Substring(1), out int intInput)) { if (input[0] == '-') { @@ -90,8 +89,7 @@ public class Day01 : IDay foreach (string input in inputs) { accumulatorHistory.Add(accumulator); - int intInput; - if (int.TryParse(input.Substring(1), out intInput)) + if (int.TryParse(input.Substring(1), out int intInput)) { if (input[0] == '-') { diff --git a/AdventOfCode2018/Day02.cs b/AdventOfCode2018/Day02.cs index b82c8ee..25c3648 100644 --- a/AdventOfCode2018/Day02.cs +++ b/AdventOfCode2018/Day02.cs @@ -50,7 +50,7 @@ What letters are common between the two correct box IDs? (In the example above, public class Day02 : IDay { - private int CountOccurrencesOfLetter(string text, char letter) + private static int CountOccurrencesOfLetter(string text, char letter) { return text.Count(c => (c == letter)); } @@ -75,7 +75,7 @@ public class Day02 : IDay int tripletsCount = 0; foreach (string input in inputs) { - var hasPairsAndTriplets = HasPairsAndTriplets(input); + Tuple hasPairsAndTriplets = HasPairsAndTriplets(input); if (hasPairsAndTriplets.Item1) { pairsCount++; } if (hasPairsAndTriplets.Item2) { tripletsCount++; } } @@ -104,11 +104,10 @@ public class Day02 : IDay { for (int j = (i + 1); j < inputs.Length; j++) { - var result = CompareIDPair(inputs[i], inputs[j]); + Tuple result = CompareIDPair(inputs[i], inputs[j]); if (result.Item1 == 1) { return result.Item2; } } } return string.Empty; } - } \ No newline at end of file diff --git a/AdventOfCode2018/Day03.cs b/AdventOfCode2018/Day03.cs index 194489b..bd6d7dd 100644 --- a/AdventOfCode2018/Day03.cs +++ b/AdventOfCode2018/Day03.cs @@ -60,7 +60,7 @@ public class Day03 : IDay { public string ResolvePart1(string[] inputs) { - List claims = inputs.Select(i => Claim.FromString(i)).ToList(); + List claims = inputs.Select(Claim.FromString).ToList(); const int edgeSize = 1000; int[,] cells = new int[edgeSize, edgeSize]; @@ -92,9 +92,9 @@ public class Day03 : IDay public string ResolvePart2(string[] inputs) { - List claims = inputs.Select(i => Claim.FromString(i)).ToList(); + List claims = inputs.Select(Claim.FromString).ToList(); - Claim unoverlappingClaim = null; + Claim unOverlappingClaim = null; for (int i = 0; i < claims.Count; i++) { bool overlaps = false; @@ -109,29 +109,43 @@ public class Day03 : IDay } if (overlaps == false) { - unoverlappingClaim = claims[i]; + unOverlappingClaim = claims[i]; break; } } - return unoverlappingClaim.ID.ToString(); + return unOverlappingClaim.ID.ToString(); } public class Claim { - public int ID { get; set; } + public int ID { get; private set; } - public int Left { get; set; } - public int Top { get; set; } + public int Left { get; private set; } + public int Top { get; private set; } - public int Width { get; set; } - public int Height { get; set; } + public int Width { get; private set; } + public int Height { get; private set; } - public int MinX { get { return Left; } } - public int MaxX { get { return Left + Width; } } + private int MinX + { + get { return Left; } + } - public int MinY { get { return Top; } } - public int MaxY { get { return Top + Height; } } + private int MaxX + { + get { return Left + Width; } + } + + private int MinY + { + get { return Top; } + } + + private int MaxY + { + get { return Top + Height; } + } public int GetArea() { @@ -141,7 +155,7 @@ public class Day03 : IDay public static Claim FromString(string strClaim) { Claim claim = new(); - string[] parts = strClaim.Split(new[] { " @ ", ",", ": ", "x", }, StringSplitOptions.None); + string[] parts = strClaim.Split([" @ ", ",", ": ", "x"], StringSplitOptions.None); claim.ID = Convert.ToInt32(parts[0].Substring(1)); claim.Left = Convert.ToInt32(parts[1]); claim.Top = Convert.ToInt32(parts[2]); diff --git a/AdventOfCode2018/Day04.cs b/AdventOfCode2018/Day04.cs index 93518a9..b9b9bef 100644 --- a/AdventOfCode2018/Day04.cs +++ b/AdventOfCode2018/Day04.cs @@ -68,14 +68,14 @@ public class Day04 : IDay public string ResolvePart1(string[] inputs) { List guardEvents = GuardEvent.FromStringArray(inputs); - Dictionary dictFullHistogram = BuildFullHistorgram(guardEvents); + Dictionary dictFullHistogram = BuildFullHistogram(guardEvents); // Find sleepier guard GuardSleepHistogram highestSleeperHistogram = null; long highestTotalSleep = long.MinValue; foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values) { - int totalSleep = guardHistogram.SleepOnMunute.Sum(); + int totalSleep = guardHistogram.SleepOnMinute.Sum(); if (totalSleep > highestTotalSleep) { @@ -89,10 +89,10 @@ public class Day04 : IDay int maxSleepMinuteValue = int.MinValue; for (int i = 0; i < GuardSleepHistogram.MinutesOnHour; i++) { - if (highestSleeperHistogram.SleepOnMunute[i] > maxSleepMinuteValue) + if (highestSleeperHistogram.SleepOnMinute[i] > maxSleepMinuteValue) { maxSleepMinute = i; - maxSleepMinuteValue = highestSleeperHistogram.SleepOnMunute[i]; + maxSleepMinuteValue = highestSleeperHistogram.SleepOnMinute[i]; } } @@ -103,7 +103,7 @@ public class Day04 : IDay public string ResolvePart2(string[] inputs) { List guardEvents = GuardEvent.FromStringArray(inputs); - Dictionary dictFullHistogram = BuildFullHistorgram(guardEvents); + Dictionary dictFullHistogram = BuildFullHistogram(guardEvents); int selectedGuardID = int.MinValue; int selectedMinute = int.MinValue; @@ -112,9 +112,9 @@ public class Day04 : IDay { foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values) { - if (guardHistogram.SleepOnMunute[i] > maxSleepMinuteValue) + if (guardHistogram.SleepOnMinute[i] > maxSleepMinuteValue) { - maxSleepMinuteValue = guardHistogram.SleepOnMunute[i]; + maxSleepMinuteValue = guardHistogram.SleepOnMinute[i]; selectedGuardID = guardHistogram.ID; selectedMinute = i; } @@ -125,7 +125,7 @@ public class Day04 : IDay return result.ToString(); } - private static Dictionary BuildFullHistorgram(List guardEvents) + private static Dictionary BuildFullHistogram(List guardEvents) { Dictionary dictFullHistogram = new(); foreach (IGrouping group in guardEvents.GroupBy(guardEvent => guardEvent.Date.DayOfYear)) @@ -156,10 +156,8 @@ public class Day04 : IDay foreach (GuardSleepHistogram dayGuardHistogram in dictDayHistogram.Values) { - GuardSleepHistogram guardHistogram; - if (dictFullHistogram.ContainsKey(dayGuardHistogram.ID)) + if (dictFullHistogram.TryGetValue(dayGuardHistogram.ID, out GuardSleepHistogram guardHistogram)) { - guardHistogram = dictFullHistogram[dayGuardHistogram.ID]; guardHistogram.AddHistogram(dayGuardHistogram); } else @@ -181,14 +179,14 @@ public class Day04 : IDay public class GuardEvent { - public DateTime Date { get; set; } - public int? ID { get; set; } - public GuardEventType Type { get; set; } + public DateTime Date { get; private set; } + public int? ID { get; private set; } + public GuardEventType Type { get; private set; } public static GuardEvent FromString(string strEvent) { GuardEvent guardEvent = new(); - string[] parts = strEvent.Split(new[] { "[", "-", " ", ":", "]", "#", }, StringSplitOptions.RemoveEmptyEntries); + string[] parts = strEvent.Split(["[", "-", " ", ":", "]", "#"], StringSplitOptions.RemoveEmptyEntries); guardEvent.Date = new DateTime( Convert.ToInt32(parts[0]), Convert.ToInt32(parts[1]), @@ -216,7 +214,7 @@ public class Day04 : IDay public static List FromStringArray(string[] strEvents) { List guardEvents = strEvents - .Select(strEvent => FromString(strEvent)) + .Select(FromString) .OrderBy(guardEvent => guardEvent.Date) .ToList(); @@ -240,14 +238,14 @@ public class Day04 : IDay public class GuardSleepHistogram { public const int MinutesOnHour = 60; - public int ID { get; set; } - public int[] SleepOnMunute { get; } = new int[MinutesOnHour]; + public int ID { get; init; } + public int[] SleepOnMinute { get; } = new int[MinutesOnHour]; public void FallSleep(int minute) { for (int i = minute; i < MinutesOnHour; i++) { - SleepOnMunute[i] = 1; + SleepOnMinute[i] = 1; } } @@ -255,7 +253,7 @@ public class Day04 : IDay { for (int i = minute; i < MinutesOnHour; i++) { - SleepOnMunute[i] = 0; + SleepOnMinute[i] = 0; } } @@ -263,7 +261,7 @@ public class Day04 : IDay { for (int i = 0; i < MinutesOnHour; i++) { - SleepOnMunute[i] += histogram.SleepOnMunute[i]; + SleepOnMinute[i] += histogram.SleepOnMinute[i]; } } } diff --git a/AdventOfCode2018/Day05.cs b/AdventOfCode2018/Day05.cs index 75bd3de..d0017ab 100644 --- a/AdventOfCode2018/Day05.cs +++ b/AdventOfCode2018/Day05.cs @@ -108,7 +108,7 @@ public class Day05 : IDay public string ResolvePart2(string[] inputs) { string input = inputs[0]; - List allUnitTypes = input.Select(c => char.ToLower(c)).Distinct().ToList(); + List allUnitTypes = input.Select(char.ToLower).Distinct().ToList(); int minPolymerLenght = int.MaxValue; foreach (char unitType in allUnitTypes) diff --git a/AdventOfCode2018/Day06.cs b/AdventOfCode2018/Day06.cs index a61a218..29dec71 100644 --- a/AdventOfCode2018/Day06.cs +++ b/AdventOfCode2018/Day06.cs @@ -96,7 +96,7 @@ public class Day06 : IDay { return inputs .Where(input => string.IsNullOrEmpty(input) == false) - .Select(input => ChronoPoint.FromString(input)) + .Select(ChronoPoint.FromString) .ToList(); } @@ -183,12 +183,12 @@ public class Day06 : IDay return areaInRange; } - public int DistanceThresold { get; set; } = 10000; + public int DistanceThreshold { get; init; } = 10000; public string ResolvePart2(string[] inputs) { List points = InputsToPoints(inputs); - int areaInRange = AreaInThresold(points, DistanceThresold); + int areaInRange = AreaInThresold(points, DistanceThreshold); return areaInRange.ToString(); } @@ -200,7 +200,7 @@ public class Day06 : IDay public static ChronoPoint FromString(string strPoint) { if (string.IsNullOrEmpty(strPoint)) { return null; } - string[] parts = strPoint.Split(new[] { ", ", }, StringSplitOptions.RemoveEmptyEntries); + string[] parts = strPoint.Split([", "], StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) { return null; } ChronoPoint point = new() { X = Convert.ToInt32(parts[0]), diff --git a/AdventOfCode2018/Day07.cs b/AdventOfCode2018/Day07.cs index 454338b..e00231a 100644 --- a/AdventOfCode2018/Day07.cs +++ b/AdventOfCode2018/Day07.cs @@ -89,11 +89,11 @@ public class Day07 : IDay foreach (string input in inputs) { if (string.IsNullOrEmpty(input)) { continue; } - string[] parts = input.Split(new[] { + string[] parts = input.Split([ "Step ", " must be finished before step ", " can begin.", - }, StringSplitOptions.RemoveEmptyEntries); + ], StringSplitOptions.RemoveEmptyEntries); instructions.AddNodeRelation(parts[1].ToUpper(), parts[0].ToUpper()); } foreach (InstructionNode node in instructions.Nodes.Values) @@ -118,8 +118,8 @@ public class Day07 : IDay return sbInstructions.ToString(); } - public int BaseCost { get; set; } = 60; - public int NumberOfWorkers { get; set; } = 5; + public int BaseCost { get; init; } = 60; + public int NumberOfWorkers { get; init; } = 5; public string ResolvePart2(string[] inputs) { @@ -130,9 +130,9 @@ public class Day07 : IDay public class InstructionNode { - public string NodeID { get; set; } + public string NodeID { get; init; } - public List PreviousNodeIDs { get; } = new(); + public List PreviousNodeIDs { get; } = []; public int Cost { get; set; } @@ -148,16 +148,16 @@ public class Day07 : IDay } } - public class Instructions + private class Instructions { public Dictionary Nodes { get; } = new(); - public InstructionNode GetNode(string nodeID) + private InstructionNode GetNode(string nodeID) { - InstructionNode node = null; - if (Nodes.ContainsKey(nodeID)) + InstructionNode node; + if (Nodes.TryGetValue(nodeID, out InstructionNode nodeAux)) { - node = Nodes[nodeID]; + node = nodeAux; } else { @@ -176,7 +176,7 @@ public class Day07 : IDay public List SortInstructions() { - List finalNodes = new(); + List finalNodes = []; foreach (InstructionNode node in Nodes.Values) { @@ -204,8 +204,8 @@ public class Day07 : IDay private class SimulatedWorker { - public InstructionNode CurrentInstruction { get; set; } - public int ElapsedTime { get; set; } + public InstructionNode CurrentInstruction { get; private set; } + private int ElapsedTime { get; set; } public void SetInstruction(InstructionNode instruction) { @@ -242,7 +242,6 @@ public class Day07 : IDay workers.Add(new SimulatedWorker()); } - bool anyWorkerWitoutWork; do { bool anyWorkDone = false; @@ -255,8 +254,8 @@ public class Day07 : IDay } if (anyWorkDone) { totalElapsedTime++; } - anyWorkerWitoutWork = workers.Any(w => w.CurrentInstruction == null); - if (anyWorkerWitoutWork) + bool anyWorkerWithoutWork = workers.Any(w => w.CurrentInstruction == null); + if (anyWorkerWithoutWork) { List unusedNodes = Nodes.Values .Where(n => diff --git a/AdventOfCode2018/Day08.cs b/AdventOfCode2018/Day08.cs index f93f692..b362d66 100644 --- a/AdventOfCode2018/Day08.cs +++ b/AdventOfCode2018/Day08.cs @@ -77,13 +77,13 @@ public class Day08 : IDay public class IntStream { - private int[] _values; + private readonly int[] _values; private int _index; public IntStream(string strValues) { _values = strValues - .Split(new[] { " ", }, StringSplitOptions.RemoveEmptyEntries) + .Split([" "], StringSplitOptions.RemoveEmptyEntries) .Select(strVal => Convert.ToInt32(strVal)) .ToArray(); _index = 0; @@ -99,9 +99,9 @@ public class Day08 : IDay public class ChronoLicenceNode { - public List Childs { get; } = new(); + public List Childs { get; } = []; - public List Metadata { get; } = new(); + public List Metadata { get; } = []; public static ChronoLicenceNode BuildFromIntStream(IntStream stream) { diff --git a/AdventOfCode2018/Day09.cs b/AdventOfCode2018/Day09.cs index 3c3a701..8e077cb 100644 --- a/AdventOfCode2018/Day09.cs +++ b/AdventOfCode2018/Day09.cs @@ -75,7 +75,7 @@ public class Day09 : IDay private static string CalculateHighScore(string input, long factor) { - string[] parts = input.Split(new[] { " players; last marble is worth ", " points" }, StringSplitOptions.RemoveEmptyEntries); + string[] parts = input.Split([" players; last marble is worth ", " points"], StringSplitOptions.RemoveEmptyEntries); long numberOfPlayers = Convert.ToInt32(parts[0]); long lastMarble = Convert.ToInt32(parts[1]) * factor; MarbleGame marbleGame = new(); @@ -86,14 +86,14 @@ public class Day09 : IDay public class Marble { - public long Value { get; set; } + public long Value { get; init; } public Marble Previous { get; set; } public Marble Next { get; set; } } public class MarbleGame { - public Dictionary Scores { get; } = new(); + private Dictionary Scores { get; } = new(); private Marble _firstMarble; private Marble _currentMarble; @@ -141,20 +141,15 @@ public class Day09 : IDay } } - public void PrintStatus() + private void PrintStatus() { Console.Write("[{0}] ", _currentPlayer); Marble marble = _firstMarble; do { - if (_currentMarble.Value == marble.Value) - { - Console.Write("({0}) ", marble.Value); - } - else - { - Console.Write("{0} ", marble.Value); - } + Console.Write(_currentMarble.Value == marble.Value + ? "({0}) " + : "{0} ", marble.Value); marble = marble.Next; } while (marble.Value != 0); Console.WriteLine(); diff --git a/AdventOfCode2018/Day10.cs b/AdventOfCode2018/Day10.cs index 79b4abd..e08a026 100644 --- a/AdventOfCode2018/Day10.cs +++ b/AdventOfCode2018/Day10.cs @@ -156,13 +156,13 @@ Impressed by your sub-hour communication capabilities, the Elves are curious: ex public class Day10 : IDay { - public int Width { get; set; } = 65; - public int Height { get; set; } = 12; + public int Width { get; init; } = 65; + public int Height { get; init; } = 12; public string ResolvePart1(string[] inputs) { LightField lightField = new(inputs); - int t = lightField.SearchSmallerBoundindBox(); + int t = lightField.SearchSmallerBoundingBox(); string result = lightField.Render(t, Width, Height); return result; } @@ -170,20 +170,20 @@ public class Day10 : IDay public string ResolvePart2(string[] inputs) { LightField lightField = new(inputs); - int t = lightField.SearchSmallerBoundindBox(); + int t = lightField.SearchSmallerBoundingBox(); return t.ToString(); } public class LightPoint { - public int X { get; set; } - public int Y { get; set; } - public int VX { get; set; } - public int VY { get; set; } + private int X { get; init; } + private int Y { get; init; } + private int VX { get; init; } + private int VY { get; init; } public static LightPoint FromString(string strPoint) { - string[] parts = strPoint.Split(new[] { "position=<", " ", ",", "> velocity=<", ">" }, StringSplitOptions.RemoveEmptyEntries); + string[] parts = strPoint.Split(["position=<", " ", ",", "> velocity=<", ">"], StringSplitOptions.RemoveEmptyEntries); LightPoint point = new() { X = Convert.ToInt32(parts[0]), Y = Convert.ToInt32(parts[1]), @@ -204,16 +204,11 @@ public class Day10 : IDay } } - public class LightField + private class LightField(string[] strPoints) { - private readonly List _points; + private readonly List _points = strPoints.Select(LightPoint.FromString).ToList(); - public LightField(string[] strPoints) - { - _points = strPoints.Select(strPoint => LightPoint.FromString(strPoint)).ToList(); - } - - public int SearchSmallerBoundindBox() + public int SearchSmallerBoundingBox() { int minHeight = int.MaxValue; int minT = 0; @@ -288,14 +283,7 @@ public class Day10 : IDay sb.AppendLine(); for (int i = 0; i < width; i++) { - if (field[i, j] > 0) - { - sb.Append("#"); - } - else - { - sb.Append("."); - } + sb.Append(field[i, j] > 0 ? "#" : "."); } } return sb.ToString(); diff --git a/AdventOfCode2018/Day11.cs b/AdventOfCode2018/Day11.cs index bc77313..1895dfb 100644 --- a/AdventOfCode2018/Day11.cs +++ b/AdventOfCode2018/Day11.cs @@ -190,7 +190,7 @@ public class Day11 : IDay return powerLevel; } - public static void SearchBestRegion(int width, int height, int serial, out int x, out int y, out int size) + private static void SearchBestRegion(int width, int height, int serial, out int x, out int y, out int size) { int[,] summationFiled = GenerateSumationField(width, height, serial); int bestPowerLevel = int.MinValue; diff --git a/AdventOfCode2018/Day12.cs b/AdventOfCode2018/Day12.cs index 4f9a5e6..a57f2bd 100644 --- a/AdventOfCode2018/Day12.cs +++ b/AdventOfCode2018/Day12.cs @@ -96,20 +96,20 @@ public class Day12 : IDay private class PlantRule { - public bool Minus2 { get; set; } - public bool Minus1 { get; set; } - public bool Current { get; set; } - public bool Plus1 { get; set; } - public bool Plus2 { get; set; } + public bool Minus2 { get; init; } + public bool Minus1 { get; init; } + public bool Current { get; init; } + public bool Plus1 { get; init; } + public bool Plus2 { get; init; } - public bool Result { get; set; } + public bool Result { get; init; } } private const int SideMargin = 5; private const int SideProcessMargin = 2; - private List _initialState = new(); - private List _rules = new(); + private readonly List _initialState = []; + private readonly List _rules = []; private long _offsetField; private bool[] _field; private bool[] _workField; @@ -126,9 +126,8 @@ public class Day12 : IDay for (int i = 2; i < inputs.Length; i++) { if (string.IsNullOrEmpty(inputs[i])) { continue; } - string[] parts = inputs[i].Split(new[] { " => " }, StringSplitOptions.RemoveEmptyEntries); - _rules.Add(new PlantRule - { + string[] parts = inputs[i].Split([" => "], StringSplitOptions.RemoveEmptyEntries); + _rules.Add(new PlantRule { Minus2 = (parts[0][0] == '#'), Minus1 = (parts[0][1] == '#'), Current = (parts[0][2] == '#'), @@ -151,9 +150,7 @@ public class Day12 : IDay private void SwapFields() { - bool[] aux = _field; - _field = _workField; - _workField = aux; + (_field, _workField) = (_workField, _field); } private void RecenterField() @@ -236,8 +233,7 @@ public class Day12 : IDay rule.Minus1 == minus1 && rule.Current == current && rule.Plus1 == plus1 && - rule.Plus2 == plus2 && - true + rule.Plus2 == plus2 ) { result = rule.Result; diff --git a/AdventOfCode2018/Day13.cs b/AdventOfCode2018/Day13.cs index 893aa28..d1a07fe 100644 --- a/AdventOfCode2018/Day13.cs +++ b/AdventOfCode2018/Day13.cs @@ -372,7 +372,7 @@ public class Day13 : IDay private int _width; private int _height; private char[,] _grid; - private List _trains = new(); + private readonly List _trains = []; private void Initialize(string[] inputs) { @@ -387,8 +387,7 @@ public class Day13 : IDay char cell = inputs[j][i]; if (cell == '^') { - _trains.Add(new Train - { + _trains.Add(new Train { X = i, Y = j, Direction = TrainDirection.North, @@ -398,8 +397,7 @@ public class Day13 : IDay } if (cell == 'v') { - _trains.Add(new Train - { + _trains.Add(new Train { X = i, Y = j, Direction = TrainDirection.South, @@ -409,8 +407,7 @@ public class Day13 : IDay } if (cell == '<') { - _trains.Add(new Train - { + _trains.Add(new Train { X = i, Y = j, Direction = TrainDirection.West, @@ -420,8 +417,7 @@ public class Day13 : IDay } if (cell == '>') { - _trains.Add(new Train - { + _trains.Add(new Train { X = i, Y = j, Direction = TrainDirection.East, @@ -502,5 +498,4 @@ public class Day13 : IDay Console.WriteLine(); } } - } \ No newline at end of file diff --git a/AdventOfCode2018/Day15.cs b/AdventOfCode2018/Day15.cs index 8ef4ff3..394969d 100644 --- a/AdventOfCode2018/Day15.cs +++ b/AdventOfCode2018/Day15.cs @@ -377,18 +377,18 @@ public class Day15 : IDay { public enum EntityType { Elf, Goblin, } - public bool Show { get; set; } = false; + private bool Show { get; set; } = false; public class Entity { - public const int GoblinAttackPower = 3; + private const int GoblinAttackPower = 3; public static int ElfAttackPower { get; set; } = 3; - public const int InitialHealth = 200; + private const int InitialHealth = 200; - public EntityType Type { get; set; } + public EntityType Type { get; init; } public int X { get; set; } public int Y { get; set; } - public int Health { get; set; } = InitialHealth; + public int Health { get; private set; } = InitialHealth; public bool IsAlive() { @@ -401,18 +401,17 @@ public class Day15 : IDay (other.X + 1 == X && other.Y == Y) || (other.X - 1 == X && other.Y == Y) || (other.X == X && other.Y + 1 == Y) || - (other.X == X && other.Y - 1 == Y) || - false + (other.X == X && other.Y - 1 == Y) ) { return true; } return false; } - + public void Attack(char[,] map, Entity other) { - if(Type == EntityType.Elf) + if (Type == EntityType.Elf) { other.Health -= ElfAttackPower; } @@ -435,13 +434,12 @@ public class Day15 : IDay } } - public class Target + private class Target { - public int X { get; set; } - public int Y { get; set; } + public int X { get; init; } + public int Y { get; init; } public int Distance { get; set; } - public int Priority { get; set; } - public Entity Entity { get; set; } + public int Priority { get; init; } } private int _width; @@ -450,7 +448,7 @@ public class Day15 : IDay private List _entities; private BreadthFirstSearchGrid _search; private int _rounds; - + private void Init(string[] inputs) { _height = inputs.Length; @@ -473,8 +471,7 @@ public class Day15 : IDay } else if (cell == 'E') { - _entities.Add(new Entity - { + _entities.Add(new Entity { Type = EntityType.Elf, X = i, Y = j, @@ -483,8 +480,7 @@ public class Day15 : IDay } else if (cell == 'G') { - _entities.Add(new Entity - { + _entities.Add(new Entity { Type = EntityType.Goblin, X = i, Y = j, @@ -505,7 +501,7 @@ public class Day15 : IDay .ThenBy(e => e.X); } - public IEnumerable GetTargetEntities(Entity entity) + private IEnumerable GetTargetEntities(Entity entity) { return _entities .Where(e => e.IsAlive() && e.Type != entity.Type) @@ -513,30 +509,28 @@ public class Day15 : IDay .ThenBy(e => e.X); } - public Entity GetBestInRangeTarget(Entity entity, IEnumerable targets) + private Entity GetBestInRangeTarget(Entity entity, IEnumerable targets) { return targets - .Where(e => entity.InRangeOf(e)) + .Where(entity.InRangeOf) .OrderBy(e => e.Health) .ThenBy(e => e.Y) .ThenBy(e => e.X) .FirstOrDefault(); } - private void AddTarget(List targets, int targetX, int targetY, int priority, Entity entity) + private void AddTarget(List targets, int targetX, int targetY, int priority) { if (targetX >= 0 && targetX < _width && targetY >= 0 && targetY < _height && _map[targetX, targetY] == '.') { int distance = _search.QueryDistance(targetX, targetY); if (distance >= 0) { - targets.Add(new Target - { + targets.Add(new Target { X = targetX, Y = targetY, Distance = distance, Priority = priority, - Entity = entity, }); } } @@ -552,7 +546,7 @@ public class Day15 : IDay foreach (Entity entity in entities) { if (entity.IsAlive() == false) { continue; } - IEnumerable entitiesTargets = GetTargetEntities(entity); + IEnumerable entitiesTargets = GetTargetEntities(entity).ToList(); if (entitiesTargets.Any() == false) { running = false; @@ -573,19 +567,18 @@ public class Day15 : IDay int priority = 0; foreach (Entity entityTarget in entitiesTargets) { - AddTarget(targets, entityTarget.X, entityTarget.Y - 1, priority++, entityTarget); - AddTarget(targets, entityTarget.X - 1, entityTarget.Y, priority++, entityTarget); - AddTarget(targets, entityTarget.X + 1, entityTarget.Y, priority++, entityTarget); - AddTarget(targets, entityTarget.X, entityTarget.Y + 1, priority++, entityTarget); + AddTarget(targets, entityTarget.X, entityTarget.Y - 1, priority++); + AddTarget(targets, entityTarget.X - 1, entityTarget.Y, priority++); + AddTarget(targets, entityTarget.X + 1, entityTarget.Y, priority++); + AddTarget(targets, entityTarget.X, entityTarget.Y + 1, priority++); } Target bestTarget = targets.OrderBy(t => t.Distance).ThenBy(t => t.Priority).FirstOrDefault(); if (bestTarget != null) { _search.SearchCharGrid(_map, '.', bestTarget.X, bestTarget.Y); targets.Clear(); - Target dirTarget; - dirTarget = new Target { X = entity.X, Y = entity.Y - 1, Priority = 0, }; + Target dirTarget = new() { X = entity.X, Y = entity.Y - 1, Priority = 0, }; dirTarget.Distance = _search.QueryDistance(dirTarget.X, dirTarget.Y); if (dirTarget.Distance >= 0) targets.Add(dirTarget); @@ -609,7 +602,7 @@ public class Day15 : IDay { throw new Exception("No possible direction"); } - + entity.MoveTo(_map, finalTarget.X, finalTarget.Y); // Attack @@ -620,10 +613,10 @@ public class Day15 : IDay } } } - if(running == false) { break; } + if (running == false) { break; } _rounds++; if (Show) { PrintBattlefield(); } - } while (running); + } while (true); if (Show) { PrintBattlefield(); } } @@ -637,7 +630,8 @@ public class Day15 : IDay { Console.Write(_map[i, j]); } - IEnumerable entitiesOnLine = _entities.Where(e => e.IsAlive() && e.Y == j).OrderBy(e => e.X); + int j1 = j; + IEnumerable entitiesOnLine = _entities.Where(e => e.IsAlive() && e.Y == j1).OrderBy(e => e.X); foreach (Entity entity in entitiesOnLine) { Console.Write(" {0}({1})", (entity.Type == EntityType.Elf) ? 'E' : 'G', entity.Health); @@ -683,16 +677,15 @@ public class Day15 : IDay private class BFSCell { public bool Visited { get; set; } - public BFSCell CameFrom { get; set; } public int Distance { get; set; } = -1; - public int X { get; set; } - public int Y { get; set; } + public int X { get; init; } + public int Y { get; init; } } private readonly BFSCell[,] _grid; private readonly int _width; private readonly int _height; - + public BreadthFirstSearchGrid(int width, int height) { _grid = new BFSCell[width, height]; @@ -707,15 +700,14 @@ public class Day15 : IDay } } - public void Reset() + private void Reset() { - for(int j =0;j< _height; j++) + for (int j = 0; j < _height; j++) { - for(int i = 0; i < _width; i++) + for (int i = 0; i < _width; i++) { BFSCell cell = _grid[i, j]; cell.Visited = false; - cell.CameFrom = null; cell.Distance = -1; } } @@ -724,7 +716,7 @@ public class Day15 : IDay private void ProcessCell(char[,] grid, char empty, Queue frontier, BFSCell current, int nextX, int nextY) { if (nextX < 0 || nextX >= _width || nextY < 0 || nextY >= _height) { return; } - if (grid[nextX, nextY] == empty || current== null) + if (grid[nextX, nextY] == empty || current == null) { BFSCell cell = _grid[nextX, nextY]; if (cell.Visited == false) @@ -732,7 +724,6 @@ public class Day15 : IDay frontier.Enqueue(cell); cell.Visited = true; cell.Distance = (current?.Distance ?? -1) + 1; - cell.CameFrom = current; } } } diff --git a/AdventOfCode2018/Day16.cs b/AdventOfCode2018/Day16.cs index 1988313..2c40861 100644 --- a/AdventOfCode2018/Day16.cs +++ b/AdventOfCode2018/Day16.cs @@ -160,17 +160,11 @@ public class Day16 : IDay private readonly List _instructions; - private class ChronoInstruction + private class ChronoInstruction(string opName, Action opFunc) { public int OpCode { get; set; } = -1; - public string OpName { get; } - public Action OpFunc { get; } - - public ChronoInstruction(string opName, Action opFunc) - { - OpName = opName; - OpFunc = opFunc; - } + public string OpName { get; } = opName; + public Action OpFunc { get; } = opFunc; public Dictionary OpCodeHistogram { get; } = new(); @@ -191,7 +185,7 @@ public class Day16 : IDay public ChronoMachine() { _registers = new int[4]; - _instructions = new List { + _instructions = [ new("addr", Op_AddR), new("addi", Op_AddI), new("mulr", Op_MulR), @@ -208,7 +202,7 @@ public class Day16 : IDay new("eqir", Op_EqIR), new("eqri", Op_EqRI), new("eqrr", Op_EqRR), - }; + ]; } public void ResetRegisters() @@ -341,7 +335,7 @@ public class Day16 : IDay public void InitOpCodes(bool debug = false) { - if(debug) + if (debug) { foreach (ChronoInstruction instruction in _instructions) { @@ -374,7 +368,7 @@ public class Day16 : IDay instruction.OpCode = opCode; _dictInstructions.Add(opCode, instruction); - if(debug) { Console.WriteLine($"{instruction.OpName}: {instruction.OpCode}"); } + if (debug) { Console.WriteLine($"{instruction.OpName}: {instruction.OpCode}"); } } } } diff --git a/AdventOfCode2018/Day23.cs b/AdventOfCode2018/Day23.cs index f9607ae..ef92cd1 100644 --- a/AdventOfCode2018/Day23.cs +++ b/AdventOfCode2018/Day23.cs @@ -69,7 +69,7 @@ public class Day23 : IDay { List nanoBots = NanoBot.ListFromStrings(inputs); NanoBot bestNanoBot = nanoBots.OrderBy(nanoBot => nanoBot.Range).LastOrDefault(); - int countInRange = nanoBots.Where(nanoBot => bestNanoBot.InRange(nanoBot)).Count(); + int countInRange = nanoBots.Count(nanoBot => bestNanoBot.InRange(nanoBot)); return countInRange.ToString(); } @@ -82,7 +82,7 @@ public class Day23 : IDay long minX = long.MaxValue; long minY = long.MaxValue; long minZ = long.MaxValue; - foreach(NanoBot nanoBot in nanoBots) + foreach (NanoBot nanoBot in nanoBots) { if (nanoBot.X < minX) { minX = nanoBot.X; } if (nanoBot.X > maxX) { maxX = nanoBot.X; } @@ -95,7 +95,7 @@ public class Day23 : IDay long sizeY = maxY - minY; long sizeZ = maxZ - minZ; long scale = Math.Min(sizeX, Math.Min(sizeY, sizeZ)); - + do { scale /= 2; @@ -112,11 +112,11 @@ public class Day23 : IDay for (long i = minX; i <= maxX; i += scale) { int count = 0; - foreach(NanoBot nanoBot in nanoBots) + foreach (NanoBot nanoBot in nanoBots) { if (nanoBot.InRange(i, j, k, scale)) { count++; } } - if(count> bestCount) + if (count > bestCount) { bestX = i; bestY = j; @@ -134,7 +134,7 @@ public class Day23 : IDay minZ = bestZ - scale; maxZ = bestZ + scale; - if(scale == 1) + if (scale == 1) { long distance = bestX + bestY + bestZ; return distance.ToString(); @@ -145,14 +145,14 @@ public class Day23 : IDay public class NanoBot { - public long X { get; set; } - public long Y { get; set; } - public long Z { get; set; } - public long Range { get; set; } + public long X { get; private init; } + public long Y { get; private init; } + public long Z { get; private init; } + public long Range { get; private init; } - public static NanoBot FromString(string strInput) + private static NanoBot FromString(string strInput) { - string[] parts = strInput.Split(new[] { "pos=<", ",", ">, r=", }, StringSplitOptions.RemoveEmptyEntries); + string[] parts = strInput.Split(["pos=<", ",", ">, r="], StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 4) { return null; } NanoBot nanoBot = new() { X = Convert.ToInt64(parts[0]), @@ -166,18 +166,18 @@ public class Day23 : IDay public static List ListFromStrings(string[] inputs) { List nanoBots = inputs - .Select(strInput => FromString(strInput)) + .Select(FromString) .Where(nanoBot => nanoBot != null) .ToList(); return nanoBots; } - public long ManhattanDistance(NanoBot other) + private long ManhattanDistance(NanoBot other) { return ManhattanDistance(other.X, other.Y, other.Z); } - public long ManhattanDistance(long x, long y, long z) + private long ManhattanDistance(long x, long y, long z) { long distance = Math.Abs(X - x) + Math.Abs(Y - y) + Math.Abs(Z - z); return distance; diff --git a/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj b/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj index 04f415a..cb63640 100644 --- a/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj +++ b/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj @@ -1,24 +1,24 @@ - - net8.0 - enable - AdventOfCode2020.Tests + + net8.0 + enable + AdventOfCode2020.Tests - false - + false + - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + - - - + + + diff --git a/AdventOfCode2020.Tests/Day01_Tests.cs b/AdventOfCode2020.Tests/Day01_Tests.cs index 42c1f83..d1cf8e7 100644 --- a/AdventOfCode2020.Tests/Day01_Tests.cs +++ b/AdventOfCode2020.Tests/Day01_Tests.cs @@ -7,16 +7,16 @@ public class Day01_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day01(); + Day01 day = new Day01(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "1721", "979", "366", "299", "675", "1456", - }); + ]); Assert.Equal("514579", result); } @@ -28,16 +28,16 @@ public class Day01_Tests [Fact] public void ResolvePart2__Example() { - var day = new Day01(); + Day01 day = new Day01(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "1721", "979", "366", "299", "675", "1456", - }); + ]); Assert.Equal("241861950", result); } diff --git a/AdventOfCode2020.Tests/Day02_Tests.cs b/AdventOfCode2020.Tests/Day02_Tests.cs index 60b6a8e..10bc2a8 100644 --- a/AdventOfCode2020.Tests/Day02_Tests.cs +++ b/AdventOfCode2020.Tests/Day02_Tests.cs @@ -7,13 +7,13 @@ public class Day02_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day02(); + Day02 day = new Day02(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "1-3 a: abcde", "1-3 b: cdefg", "2-9 c: ccccccccc", - }); + ]); Assert.Equal("2", result); } @@ -25,13 +25,13 @@ public class Day02_Tests [Fact] public void ResolvePart2__Example() { - var day = new Day02(); + Day02 day = new Day02(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "1-3 a: abcde", "1-3 b: cdefg", "2-9 c: ccccccccc", - }); + ]); Assert.Equal("1", result); } diff --git a/AdventOfCode2020.Tests/Day03_Tests.cs b/AdventOfCode2020.Tests/Day03_Tests.cs index 474eee5..24f164e 100644 --- a/AdventOfCode2020.Tests/Day03_Tests.cs +++ b/AdventOfCode2020.Tests/Day03_Tests.cs @@ -5,9 +5,9 @@ public class Day03_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day03(); + Day03 day = new Day03(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "..##.......", "#...#...#..", ".#....#..#.", @@ -19,7 +19,7 @@ public class Day03_Tests "#.##...#...", "#...##....#", ".#..#...#.#", - }); + ]); Assert.Equal("7", result); } @@ -27,9 +27,9 @@ public class Day03_Tests [Fact] public void ResolvePart2__Example() { - var day = new Day03(); + Day03 day = new Day03(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "..##.......", "#...#...#..", ".#....#..#.", @@ -41,7 +41,7 @@ public class Day03_Tests "#.##...#...", "#...##....#", ".#..#...#.#", - }); + ]); Assert.Equal("336", result); } diff --git a/AdventOfCode2020.Tests/Day04_Tests.cs b/AdventOfCode2020.Tests/Day04_Tests.cs index 5ec0da9..bf974be 100644 --- a/AdventOfCode2020.Tests/Day04_Tests.cs +++ b/AdventOfCode2020.Tests/Day04_Tests.cs @@ -5,9 +5,9 @@ public class Day04_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day04(); + Day04 day = new Day04(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd", "byr:1937 iyr:2017 cid:147 hgt:183cm", "", @@ -21,7 +21,7 @@ public class Day04_Tests "", "hcl:#cfa07d eyr:2025 pid:166559648", "iyr:2011 ecl:brn hgt:59in", - }); + ]); Assert.Equal("2", result); } @@ -29,9 +29,9 @@ public class Day04_Tests [Fact] public void ResolvePart2__ExampleInvalid() { - var day = new Day04(); + Day04 day = new Day04(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "eyr:1972 cid:100", "hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926", "", @@ -45,7 +45,7 @@ public class Day04_Tests "hgt:59cm ecl:zzz", "eyr:2038 hcl:74454a iyr:2023", "pid:3556412378 byr:2007", - }); + ]); Assert.Equal("0", result); } @@ -53,9 +53,9 @@ public class Day04_Tests [Fact] public void ResolvePart2__ExampleValid() { - var day = new Day04(); + Day04 day = new Day04(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980", "hcl:#623a2f", "", @@ -68,7 +68,7 @@ public class Day04_Tests "eyr:2022", "", "iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719", - }); + ]); Assert.Equal("4", result); } diff --git a/AdventOfCode2020.Tests/Day05_Tests.cs b/AdventOfCode2020.Tests/Day05_Tests.cs index d2de686..191b112 100644 --- a/AdventOfCode2020.Tests/Day05_Tests.cs +++ b/AdventOfCode2020.Tests/Day05_Tests.cs @@ -5,11 +5,11 @@ public class Day05_Tests [Fact] public void ResolvePart1__Example1() { - var day = new Day05(); + Day05 day = new Day05(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "FBFBBFFRLR", - }); + ]); Assert.Equal("357", result); } @@ -17,11 +17,11 @@ public class Day05_Tests [Fact] public void ResolvePart1__Example2() { - var day = new Day05(); + Day05 day = new Day05(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "BFFFBBFRRR", - }); + ]); Assert.Equal("567", result); } @@ -29,11 +29,11 @@ public class Day05_Tests [Fact] public void ResolvePart1__Example3() { - var day = new Day05(); + Day05 day = new Day05(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "FFFBBBFRRR", - }); + ]); Assert.Equal("119", result); } @@ -41,11 +41,11 @@ public class Day05_Tests [Fact] public void ResolvePart1__Example4() { - var day = new Day05(); + Day05 day = new Day05(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "BBFFBBFRLL", - }); + ]); Assert.Equal("820", result); } diff --git a/AdventOfCode2020.Tests/Day06_Tests.cs b/AdventOfCode2020.Tests/Day06_Tests.cs index 7c7dcc4..bc3391a 100644 --- a/AdventOfCode2020.Tests/Day06_Tests.cs +++ b/AdventOfCode2020.Tests/Day06_Tests.cs @@ -5,9 +5,9 @@ public class Day06_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day06(); + Day06 day = new Day06(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "abc", "", "a", @@ -23,7 +23,7 @@ public class Day06_Tests "a", "", "b", - }); + ]); Assert.Equal("11", result); } @@ -31,9 +31,9 @@ public class Day06_Tests [Fact] public void ResolvePart2__Example() { - var day = new Day06(); + Day06 day = new Day06(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "abc", "", "a", @@ -49,7 +49,7 @@ public class Day06_Tests "a", "", "b", - }); + ]); Assert.Equal("6", result); } diff --git a/AdventOfCode2020.Tests/Day07_Tests.cs b/AdventOfCode2020.Tests/Day07_Tests.cs index 22605c8..f518948 100644 --- a/AdventOfCode2020.Tests/Day07_Tests.cs +++ b/AdventOfCode2020.Tests/Day07_Tests.cs @@ -5,9 +5,9 @@ public class Day07_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day07(); + Day07 day = new Day07(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "light red bags contain 1 bright white bag, 2 muted yellow bags.", "dark orange bags contain 3 bright white bags, 4 muted yellow bags.", "bright white bags contain 1 shiny gold bag.", @@ -17,7 +17,7 @@ public class Day07_Tests "vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.", "faded blue bags contain no other bags.", "dotted black bags contain no other bags.", - }); + ]); Assert.Equal("4", result); } @@ -25,9 +25,9 @@ public class Day07_Tests [Fact] public void ResolvePart2__Example() { - var day = new Day07(); + Day07 day = new Day07(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "shiny gold bags contain 2 dark red bags.", "dark red bags contain 2 dark orange bags.", "dark orange bags contain 2 dark yellow bags.", @@ -35,7 +35,7 @@ public class Day07_Tests "dark green bags contain 2 dark blue bags.", "dark blue bags contain 2 dark violet bags.", "dark violet bags contain no other bags.", - }); + ]); Assert.Equal("126", result); } diff --git a/AdventOfCode2020.Tests/Day08_Tests.cs b/AdventOfCode2020.Tests/Day08_Tests.cs index 72ec255..8cd0038 100644 --- a/AdventOfCode2020.Tests/Day08_Tests.cs +++ b/AdventOfCode2020.Tests/Day08_Tests.cs @@ -5,9 +5,9 @@ public class Day08_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day08(); + Day08 day = new Day08(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "nop +0", "acc +1", "jmp +4", @@ -17,7 +17,7 @@ public class Day08_Tests "acc +1", "jmp -4", "acc +6", - }); + ]); Assert.Equal("5", result); } @@ -25,9 +25,9 @@ public class Day08_Tests [Fact] public void ResolvePart2__Example() { - var day = new Day08(); + Day08 day = new Day08(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "nop +0", "acc +1", "jmp +4", @@ -37,7 +37,7 @@ public class Day08_Tests "acc +1", "jmp -4", "acc +6", - }); + ]); Assert.Equal("8", result); } diff --git a/AdventOfCode2020.Tests/Day09_Tests.cs b/AdventOfCode2020.Tests/Day09_Tests.cs index e790065..01705ff 100644 --- a/AdventOfCode2020.Tests/Day09_Tests.cs +++ b/AdventOfCode2020.Tests/Day09_Tests.cs @@ -5,9 +5,9 @@ public class Day09_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day09(); + Day09 day = new Day09(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "35", "20", "15", @@ -28,7 +28,7 @@ public class Day09_Tests "277", "309", "576", - }); + ]); Assert.Equal("127", result); } @@ -36,9 +36,9 @@ public class Day09_Tests [Fact] public void ResolvePart2__Example() { - var day = new Day09(); + Day09 day = new Day09(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "35", "20", "15", @@ -59,7 +59,7 @@ public class Day09_Tests "277", "309", "576", - }); + ]); Assert.Equal("62", result); } diff --git a/AdventOfCode2020/AdventOfCode2020.csproj b/AdventOfCode2020/AdventOfCode2020.csproj index e588a5d..40ec99a 100644 --- a/AdventOfCode2020/AdventOfCode2020.csproj +++ b/AdventOfCode2020/AdventOfCode2020.csproj @@ -1,20 +1,20 @@ - - Exe - net8.0 - enable - AdventOfCode2020 - + + Exe + net8.0 + enable + AdventOfCode2020 + - - - PreserveNewest - - + + + PreserveNewest + + - - - + + + diff --git a/AdventOfCode2020/Day03.cs b/AdventOfCode2020/Day03.cs index 0aeff6e..725909a 100644 --- a/AdventOfCode2020/Day03.cs +++ b/AdventOfCode2020/Day03.cs @@ -112,5 +112,4 @@ public class Day03 : IDay } return treeCnt; } - } \ No newline at end of file diff --git a/AdventOfCode2020/Day04.cs b/AdventOfCode2020/Day04.cs index c5eebfd..fe0b9ec 100644 --- a/AdventOfCode2020/Day04.cs +++ b/AdventOfCode2020/Day04.cs @@ -118,7 +118,7 @@ iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719 Count the number of valid passports - those that have all required fields and valid values. Continue to treat cid as optional. In your batch file, how many passports are valid? - + */ public class Day04 : IDay @@ -127,7 +127,7 @@ public class Day04 : IDay { List> passports = Passports_Parse(inputs); int cnt = 0; - List neededFields = new() { + List neededFields = [ "byr", // Birth Year "iyr", // Issue Year "eyr", // Expiration Year @@ -135,7 +135,7 @@ public class Day04 : IDay "hcl", // Hair Color "ecl", // Eye Color "pid", // Passport ID - }; + ]; foreach (Dictionary passport in passports) { @@ -167,7 +167,7 @@ public class Day04 : IDay private List> Passports_Parse(string[] inputs) { - List> passports = new(); + List> passports = []; Dictionary passport = new(); foreach (string input in inputs) { @@ -257,5 +257,4 @@ public class Day04 : IDay if (input.All(char.IsNumber) == false) { return null; } return Convert.ToInt32(input); } - } \ No newline at end of file diff --git a/AdventOfCode2020/Day05.cs b/AdventOfCode2020/Day05.cs index 0e204a7..544d859 100644 --- a/AdventOfCode2020/Day05.cs +++ b/AdventOfCode2020/Day05.cs @@ -169,8 +169,8 @@ public class Day05 : IDay private Seat Seat_Parse(string input) { if (input.Length != 10 || - input.All(c => c == 'F' || c == 'B' || c == 'L' || c == 'R') == false || - false) + input.All(c => c == 'F' || c == 'B' || c == 'L' || c == 'R') == false + ) { return null; } diff --git a/AdventOfCode2020/Day06.cs b/AdventOfCode2020/Day06.cs index 70bc44c..c19c5ba 100644 --- a/AdventOfCode2020/Day06.cs +++ b/AdventOfCode2020/Day06.cs @@ -67,10 +67,7 @@ public class Day06 : IDay foreach (char c in input) { - if (groupMap.ContainsKey(c) == false) - { - groupMap.Add(c, true); - } + groupMap.TryAdd(c, true); } } if (groupMap.Count > 0) @@ -78,7 +75,7 @@ public class Day06 : IDay groupMaps.Add(groupMap); } - int total = groupMaps.Sum(groupMap => groupMap.Count); + int total = groupMaps.Sum(dict => dict.Count); return total.ToString(); } @@ -101,13 +98,9 @@ public class Day06 : IDay groupCount++; foreach (char c in input) { - if (groupMap.ContainsKey(c) == false) + if (groupMap.TryAdd(c, 1) == false) { - groupMap.Add(c, 1); - } - else - { - groupMap[c] = groupMap[c] + 1; + groupMap[c] += 1; } } } diff --git a/AdventOfCode2020/Day07.cs b/AdventOfCode2020/Day07.cs index 5f0f903..5309cf3 100644 --- a/AdventOfCode2020/Day07.cs +++ b/AdventOfCode2020/Day07.cs @@ -72,15 +72,15 @@ public class Day07 : IDay { string myBagColor = "shiny gold"; - List rules = new(); + List rules = []; foreach (string input in inputs) { BaggageRule rule = BaggageRule_Parse(input); rules.Add(rule); } - List bagColorsToCheck = new() { myBagColor }; - List bagColorsChecked = new() { myBagColor }; + List bagColorsToCheck = [myBagColor]; + List bagColorsChecked = [myBagColor]; int cntBagColors = 0; while (bagColorsToCheck.Count > 0) { @@ -108,7 +108,7 @@ public class Day07 : IDay { string myBagColor = "shiny gold"; - List rules = new(); + List rules = []; foreach (string input in inputs) { BaggageRule rule = BaggageRule_Parse(input); @@ -116,30 +116,31 @@ public class Day07 : IDay } Dictionary dictRules = rules.ToDictionary(x => x.BagColor); - int cnt = BaggageRule_CountChilds(myBagColor, dictRules); + int cnt = BaggageRule_CountChildren(myBagColor, dictRules); return cnt.ToString(); } - public class BaggageContainRule + private class BaggageContainRule { public string BagColor { get; set; } - public int Count { get; set; } + public int Count { get; init; } } - public class BaggageRule + private class BaggageRule { public string BagColor { get; set; } - public List Contain { get; set; } + public List Contain { get; init; } } - public BaggageRule BaggageRule_Parse(string input) + private BaggageRule BaggageRule_Parse(string input) { string[] words = input.Split(' '); string status = "Parse Color 1"; - BaggageRule rule = new(); - rule.Contain = new List(); + BaggageRule rule = new() { + Contain = [], + }; BaggageContainRule containRule = null; string color1 = string.Empty; @@ -164,9 +165,14 @@ public class Day07 : IDay status = "Parse Contain count"; break; case "Parse Contain count": - if (word == "no") { status = "End"; break; } - containRule = new BaggageContainRule(); - containRule.Count = Convert.ToInt32(word); + if (word == "no") + { + status = "End"; + break; + } + containRule = new BaggageContainRule { + Count = Convert.ToInt32(word), + }; status = "Parse Contain color 1"; break; case "Parse Contain color 1": @@ -179,7 +185,11 @@ public class Day07 : IDay status = "Parse Contain continue"; break; case "Parse Contain continue": - if (word == "bag," || word == "bags,") { status = "Parse Contain count"; break; } + if (word == "bag," || word == "bags,") + { + status = "Parse Contain count"; + break; + } status = "End"; break; case "End": @@ -189,13 +199,13 @@ public class Day07 : IDay return rule; } - public int BaggageRule_CountChilds(string color, Dictionary dictRules) + private int BaggageRule_CountChildren(string color, Dictionary dictRules) { int cnt = 0; BaggageRule rule = dictRules[color]; foreach (BaggageContainRule containRule in rule.Contain) { - cnt += (BaggageRule_CountChilds(containRule.BagColor, dictRules) + 1) * containRule.Count; + cnt += (BaggageRule_CountChildren(containRule.BagColor, dictRules) + 1) * containRule.Count; } return cnt; } diff --git a/AdventOfCode2020/Day08.cs b/AdventOfCode2020/Day08.cs index 33c8bdf..e405787 100644 --- a/AdventOfCode2020/Day08.cs +++ b/AdventOfCode2020/Day08.cs @@ -124,14 +124,14 @@ public class Day08 : IDay return vm.Accumulator.ToString(); } - public enum OpCode + private enum OpCode { Acc, Jmp, Nop, } - public class Instruction + private class Instruction { public OpCode OpCode { get; set; } public int Value { get; set; } @@ -154,14 +154,9 @@ public class Day08 : IDay OpCode = OpCode.Nop; } - if (parts[1].StartsWith("+")) - { - Value = Convert.ToInt32(parts[1].Substring(1)); - } - else - { - Value = Convert.ToInt32(parts[1]); - } + Value = Convert.ToInt32(parts[1].StartsWith("+") + ? parts[1].Substring(1) + : parts[1]); Executed = false; } } @@ -176,7 +171,7 @@ public class Day08 : IDay public VM(string[] inputs) { - Instructions = new List(); + Instructions = []; foreach (string input in inputs) { Instructions.Add(new Instruction(input)); diff --git a/AdventOfCode2020/Day09.cs b/AdventOfCode2020/Day09.cs index 79dd59a..37707c8 100644 --- a/AdventOfCode2020/Day09.cs +++ b/AdventOfCode2020/Day09.cs @@ -163,5 +163,4 @@ public class Day09 : IDay return firstInvalid; } - } \ No newline at end of file diff --git a/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj b/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj index 5352aa4..916f355 100644 --- a/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj +++ b/AdventOfCode2023.Tests/AdventOfCode2023.Tests.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -19,7 +19,7 @@ - + diff --git a/AdventOfCode2023.Tests/Day01_Tests.cs b/AdventOfCode2023.Tests/Day01_Tests.cs index 0816928..f8e1e9c 100644 --- a/AdventOfCode2023.Tests/Day01_Tests.cs +++ b/AdventOfCode2023.Tests/Day01_Tests.cs @@ -5,24 +5,24 @@ public class Day01_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day01(); + Day01? day = new Day01(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "1abc2", "pqr3stu8vwx", "a1b2c3d4e5f", "treb7uchet", - }); + ]); Assert.Equal("142", result); } - + [Fact] public void ResolvePart2__Example() { - var day = new Day01(); + Day01? day = new Day01(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "two1nine", "eightwothree", "abcone2threexyz", @@ -30,7 +30,7 @@ public class Day01_Tests "4nineeightseven2", "zoneight234", "7pqrstsixteen", - }); + ]); Assert.Equal("281", result); } diff --git a/AdventOfCode2023.Tests/Day02_Tests.cs b/AdventOfCode2023.Tests/Day02_Tests.cs index fd7bf31..b4c0985 100644 --- a/AdventOfCode2023.Tests/Day02_Tests.cs +++ b/AdventOfCode2023.Tests/Day02_Tests.cs @@ -18,7 +18,7 @@ public class Day02_Tests Assert.Equal(0, game.Sets[2].Blue); Assert.Equal(2, game.Sets[2].Green); } - + [Fact] public void Game_FromString__ValidExample2() { @@ -35,35 +35,35 @@ public class Day02_Tests Assert.Equal(0, game.Sets[2].Blue); Assert.Equal(5, game.Sets[2].Green); } - + [Fact] public void ResolvePart1__Example() { Day02 day = new(); - - string result = day.ResolvePart1(new[] { + + string result = day.ResolvePart1([ "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue", "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red", "Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red", "Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", - }); + ]); Assert.Equal("8", result); } - + [Fact] public void ResolvePart2__Example() { Day02 day = new(); - - string result = day.ResolvePart2(new[] { + + string result = day.ResolvePart2([ "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue", "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red", "Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red", "Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", - }); + ]); Assert.Equal("2286", result); } diff --git a/AdventOfCode2023.Tests/Day03_Tests.cs b/AdventOfCode2023.Tests/Day03_Tests.cs index 09f2793..214c74c 100644 --- a/AdventOfCode2023.Tests/Day03_Tests.cs +++ b/AdventOfCode2023.Tests/Day03_Tests.cs @@ -5,26 +5,26 @@ public class Day03_Tests [Fact] public void SearchNextSchemaNumber__NoNumbers__Null() { - string[] inputs = new[] { + string[] inputs = [ "..........", "..........", "..........", - }; + ]; Day03.SchemaNumber? number = Day03.SearchNextSchemaNumber(inputs, 0, 0); - + Assert.Null(number); } [Fact] public void SearchNextSchemaNumber__OneNumber__Valid() { - string[] inputs = new[] { + string[] inputs = [ "..........", "....420...", "..........", - }; + ]; Day03.SchemaNumber? number = Day03.SearchNextSchemaNumber(inputs, 0, 0); - + Assert.NotNull(number); Assert.Equal(1, number.Value.Row); Assert.Equal(4, number.Value.Column); @@ -35,13 +35,13 @@ public class Day03_Tests [Fact] public void SearchNextSchemaNumber__TwoNumbersSkipFirst__ValidSecond() { - string[] inputs = new[] { + string[] inputs = [ "69........", "....420...", "..........", - }; + ]; Day03.SchemaNumber? number = Day03.SearchNextSchemaNumber(inputs, 0, 4); - + Assert.NotNull(number); Assert.Equal(1, number.Value.Row); Assert.Equal(4, number.Value.Column); @@ -49,7 +49,7 @@ public class Day03_Tests Assert.Equal(420, number.Value.Value); } - private string[] _example = new[] { + private string[] _example = [ "467..114..", "...*......", "..35..633.", @@ -60,24 +60,24 @@ public class Day03_Tests "......755.", "...$.*....", ".664.598..", - }; - + ]; + [Fact] public void ResolvePart1__Example() { Day03 day = new(); - + string result = day.ResolvePart1(_example); Assert.Equal("4361", result); } - - + + [Fact] public void ResolvePart2__Example() { Day03 day = new(); - + string result = day.ResolvePart2(_example); Assert.Equal("467835", result); diff --git a/AdventOfCode2023.Tests/Day04_Tests.cs b/AdventOfCode2023.Tests/Day04_Tests.cs index d998d2e..28e360b 100644 --- a/AdventOfCode2023.Tests/Day04_Tests.cs +++ b/AdventOfCode2023.Tests/Day04_Tests.cs @@ -2,20 +2,20 @@ namespace AdventOfCode2023.Tests; public class Day04_Tests { - private readonly string[] _example = { + private readonly string[] _example = [ "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53", "Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19", "Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1", "Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83", "Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36", "Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", - }; + ]; [Fact] public void ResolvePart1__Example() { Day04 day = new(); - + string result = day.ResolvePart1(_example); Assert.Equal("13", result); @@ -25,7 +25,7 @@ public class Day04_Tests public void ResolvePart2__Example() { Day04 day = new(); - + string result = day.ResolvePart2(_example); Assert.Equal("30", result); diff --git a/AdventOfCode2023.Tests/Day05_Tests.cs b/AdventOfCode2023.Tests/Day05_Tests.cs index 8affeed..7de4918 100644 --- a/AdventOfCode2023.Tests/Day05_Tests.cs +++ b/AdventOfCode2023.Tests/Day05_Tests.cs @@ -2,7 +2,7 @@ namespace AdventOfCode2023.Tests; public class Day05_Tests { - private readonly string[] _example = { + private readonly string[] _example = [ "seeds: 79 14 55 13", "", "seed-to-soil map:", @@ -36,13 +36,13 @@ public class Day05_Tests "humidity-to-location map:", "60 56 37", "56 93 4", - }; + ]; [Fact] public void ResolvePart1__Example() { Day05 day = new(); - + string result = day.ResolvePart1(_example); Assert.Equal("35", result); @@ -52,7 +52,7 @@ public class Day05_Tests public void ResolvePart2__Example() { Day05 day = new(); - + string result = day.ResolvePart2(_example); Assert.Equal("46", result); @@ -61,22 +61,22 @@ public class Day05_Tests [Fact] public void AlmanacMapping_ParseNext__Empty__Null() { - Day05.LinesReader reader = new(Array.Empty()); + Day05.LinesReader reader = new([]); Day05.AlmanacMapping? mapping = Day05.AlmanacMapping.ParseNext(reader); - + Assert.Null(mapping); } - + [Fact] public void AlmanacMapping_ParseNext__Example1() { - Day05.LinesReader reader = new(new[] { + Day05.LinesReader reader = new([ "seed-to-soil map:", "50 98 2", "52 50 48", - }); + ]); Day05.AlmanacMapping? mapping = Day05.AlmanacMapping.ParseNext(reader); - + Assert.NotNull(mapping); Assert.Equal("seed-to-soil", mapping.Name); Assert.Equal(2, mapping.RangeMappings.Count); @@ -90,11 +90,11 @@ public class Day05_Tests const long value1 = 100; long valueMapped1 = mapping.Apply(value1); Assert.Equal(100, valueMapped1); - + const long value2 = 99; long valueMapped2 = mapping.Apply(value2); Assert.Equal(51, valueMapped2); - + const long value3 = 45; long valueMapped3 = mapping.Apply(value3); Assert.Equal(45, valueMapped3); @@ -109,7 +109,7 @@ public class Day05_Tests DestinationStart = 1000, Length = 10, }; - + // ..........■■■■■■■■■■.......... // #####......................... Day05.AlmanacRangeMapping range_Lower = new() { @@ -124,7 +124,7 @@ public class Day05_Tests Assert.Equal(5, range_Lower_Result.PreClip.Value.Length); Assert.Null(range_Lower_Result.Clipped); Assert.Null(range_Lower_Result.PostClip); - + // ..........■■■■■■■■■■.......... // ##########.................... Day05.AlmanacRangeMapping range_LowerTouching = new() { @@ -139,7 +139,7 @@ public class Day05_Tests Assert.Equal(10, range_LowerTouching_Result.PreClip.Value.Length); Assert.Null(range_LowerTouching_Result.Clipped); Assert.Null(range_LowerTouching_Result.PostClip); - + // ..........■■■■■■■■■■.......... // .........................##### Day05.AlmanacRangeMapping range_Upper = new() { @@ -154,7 +154,7 @@ public class Day05_Tests Assert.Equal(25, range_Upper_Result.PostClip.Value.OriginStart); Assert.Equal(25, range_Upper_Result.PostClip.Value.DestinationStart); Assert.Equal(5, range_Upper_Result.PostClip.Value.Length); - + // ..........■■■■■■■■■■.......... // ....................########## Day05.AlmanacRangeMapping range_UpperTouching = new() { @@ -169,7 +169,7 @@ public class Day05_Tests Assert.Equal(20, range_UpperTouching_Result.PostClip.Value.OriginStart); Assert.Equal(20, range_UpperTouching_Result.PostClip.Value.DestinationStart); Assert.Equal(10, range_UpperTouching_Result.PostClip.Value.Length); - + // ..........■■■■■■■■■■.......... // ..........$$$$$$$$$$.......... Day05.AlmanacRangeMapping range_IntersectCover = new() { @@ -184,7 +184,7 @@ public class Day05_Tests Assert.Equal(1000, range_IntersectCover_Result.Clipped.Value.DestinationStart); Assert.Equal(10, range_IntersectCover_Result.Clipped.Value.Length); Assert.Null(range_IntersectCover_Result.PostClip); - + // ..........■■■■■■■■■■.......... // ...............$$$$$.......... Day05.AlmanacRangeMapping range_IntersectInsideToEnd = new() { @@ -199,7 +199,7 @@ public class Day05_Tests Assert.Equal(1005, range_IntersectInsideToEnd_Result.Clipped.Value.DestinationStart); Assert.Equal(5, range_IntersectInsideToEnd_Result.Clipped.Value.Length); Assert.Null(range_IntersectInsideToEnd_Result.PostClip); - + // ..........■■■■■■■■■■.......... // ...............$$$$$#####..... Day05.AlmanacRangeMapping range_IntersectInsideToOutside = new() { @@ -217,7 +217,7 @@ public class Day05_Tests Assert.Equal(20, range_IntersectInsideToOutside_Result.PostClip.Value.OriginStart); Assert.Equal(20, range_IntersectInsideToOutside_Result.PostClip.Value.DestinationStart); Assert.Equal(5, range_IntersectInsideToOutside_Result.PostClip.Value.Length); - + // ..........■■■■■■■■■■.......... // .....#####$$$$$............... Day05.AlmanacRangeMapping range_IntersectOutsideToInside = new() { @@ -235,7 +235,7 @@ public class Day05_Tests Assert.Equal(1000, range_IntersectOutsideToInside_Result.Clipped.Value.DestinationStart); Assert.Equal(5, range_IntersectOutsideToInside_Result.Clipped.Value.Length); Assert.Null(range_IntersectOutsideToInside_Result.PostClip); - + // ..........■■■■■■■■■■.......... // .....#####$$$$$$$$$$.......... Day05.AlmanacRangeMapping range_IntersectOutsideToEnd = new() { @@ -253,7 +253,7 @@ public class Day05_Tests Assert.Equal(1000, range_IntersectOutsideToEnd_Result.Clipped.Value.DestinationStart); Assert.Equal(10, range_IntersectOutsideToEnd_Result.Clipped.Value.Length); Assert.Null(range_IntersectOutsideToEnd_Result.PostClip); - + // ..........■■■■■■■■■■.......... // .....#####$$$$$$$$$$#####..... Day05.AlmanacRangeMapping range_IntersectOutsideToOutside = new() { diff --git a/AdventOfCode2023.Tests/Day06_Tests.cs b/AdventOfCode2023.Tests/Day06_Tests.cs index eef645d..7b54544 100644 --- a/AdventOfCode2023.Tests/Day06_Tests.cs +++ b/AdventOfCode2023.Tests/Day06_Tests.cs @@ -2,16 +2,16 @@ namespace AdventOfCode2023.Tests; public class Day06_Tests { - private readonly string[] _example = { + private readonly string[] _example = [ "Time: 7 15 30", "Distance: 9 40 200", - }; + ]; [Fact] public void ResolvePart1__Example() { Day06 day = new(); - + string result = day.ResolvePart1(_example); Assert.Equal("288", result); @@ -21,7 +21,7 @@ public class Day06_Tests public void ResolvePart2__Example() { Day06 day = new(); - + string result = day.ResolvePart2(_example); Assert.Equal("71503", result); diff --git a/AdventOfCode2023.Tests/Day07_Tests.cs b/AdventOfCode2023.Tests/Day07_Tests.cs index 3215234..f8fc111 100644 --- a/AdventOfCode2023.Tests/Day07_Tests.cs +++ b/AdventOfCode2023.Tests/Day07_Tests.cs @@ -2,19 +2,19 @@ namespace AdventOfCode2023.Tests; public class Day07_Tests { - private readonly string[] _example = { + private readonly string[] _example = [ "32T3K 765", "T55J5 684", "KK677 28", "KTJJT 220", "QQQJA 483", - }; + ]; [Fact] public void ResolvePart1__Example() { Day07 day = new(); - + string result = day.ResolvePart1(_example); Assert.Equal("6440", result); @@ -24,7 +24,7 @@ public class Day07_Tests public void ResolvePart2__Example() { Day07 day = new(); - + string result = day.ResolvePart2(_example); Assert.Equal("5905", result); @@ -35,26 +35,26 @@ public class Day07_Tests { Day07.CamelCard card_FiveOfAKind = new("AAAAA"); Assert.Equal(Day07.CamelCard.Types.FiveOfAKind, card_FiveOfAKind.Type); - + Day07.CamelCard card_FourOfAKind = new("AA8AA"); Assert.Equal(Day07.CamelCard.Types.FourOfAKind, card_FourOfAKind.Type); - + Day07.CamelCard card_FullHouse = new("23332"); Assert.Equal(Day07.CamelCard.Types.FullHouse, card_FullHouse.Type); - + Day07.CamelCard card_ThreeOfAKind = new("TTT98"); Assert.Equal(Day07.CamelCard.Types.ThreeOfAKind, card_ThreeOfAKind.Type); - + Day07.CamelCard card_TwoPair = new("23432"); Assert.Equal(Day07.CamelCard.Types.TwoPair, card_TwoPair.Type); - + Day07.CamelCard card_OnePair = new("A23A4"); Assert.Equal(Day07.CamelCard.Types.OnePair, card_OnePair.Type); - + Day07.CamelCard card_HighCard = new("23456"); Assert.Equal(Day07.CamelCard.Types.HighCard, card_HighCard.Type); } - + [Fact] public void CamelCard_CompareTo__Examples() { @@ -62,7 +62,7 @@ public class Day07_Tests Day07.CamelCard card_02 = new("2AAAA"); Assert.Equal(-1, card_01.CompareTo(card_02)); Assert.Equal(1, card_02.CompareTo(card_01)); - + Day07.CamelCard card_77888 = new("77888"); Day07.CamelCard card_77788 = new("77788"); Assert.Equal(-1, card_77888.CompareTo(card_77788)); diff --git a/AdventOfCode2023.Tests/Day08_Tests.cs b/AdventOfCode2023.Tests/Day08_Tests.cs index b887ba9..777c7bd 100644 --- a/AdventOfCode2023.Tests/Day08_Tests.cs +++ b/AdventOfCode2023.Tests/Day08_Tests.cs @@ -2,7 +2,7 @@ namespace AdventOfCode2023.Tests; public class Day08_Tests { - private readonly string[] _example1 = { + private readonly string[] _example1 = [ "RL", "", "AAA = (BBB, CCC)", @@ -12,37 +12,37 @@ public class Day08_Tests "EEE = (EEE, EEE)", "GGG = (GGG, GGG)", "ZZZ = (ZZZ, ZZZ)", - }; - + ]; + [Fact] public void ResolvePart1__Example1() { Day08 day = new(); - + string result = day.ResolvePart1(_example1); Assert.Equal("2", result); } - private readonly string[] _example2 = { + private readonly string[] _example2 = [ "LLR", "", "AAA = (BBB, BBB)", "BBB = (AAA, ZZZ)", "ZZZ = (ZZZ, ZZZ)", - }; + ]; [Fact] public void ResolvePart1__Example2() { Day08 day = new(); - + string result = day.ResolvePart1(_example2); Assert.Equal("6", result); } - private readonly string[] _example3 = { + private readonly string[] _example3 = [ "LR", "", "11A = (11B, XXX)", @@ -53,16 +53,15 @@ public class Day08_Tests "22C = (22Z, 22Z)", "22Z = (22B, 22B)", "XXX = (XXX, XXX)", - }; + ]; [Fact] public void ResolvePart2__Example3() { Day08 day = new(); - + string result = day.ResolvePart2(_example3); Assert.Equal("6", result); } - } \ No newline at end of file diff --git a/AdventOfCode2023.Tests/Day09_Tests.cs b/AdventOfCode2023.Tests/Day09_Tests.cs index e1618ad..e697e44 100644 --- a/AdventOfCode2023.Tests/Day09_Tests.cs +++ b/AdventOfCode2023.Tests/Day09_Tests.cs @@ -2,17 +2,17 @@ namespace AdventOfCode2023.Tests; public class Day09_Tests { - private readonly string[] _example = { + private readonly string[] _example = [ "0 3 6 9 12 15", "1 3 6 10 15 21", "10 13 16 21 30 45", - }; + ]; [Fact] public void ResolvePart1__Example() { Day09 day = new(); - + string result = day.ResolvePart1(_example); Assert.Equal("114", result); @@ -22,7 +22,7 @@ public class Day09_Tests public void ResolvePart2__Example() { Day09 day = new(); - + string result = day.ResolvePart2(_example); Assert.Equal("2", result); @@ -35,7 +35,7 @@ public class Day09_Tests long result = extrapolator.Extrapolate(); Assert.Equal(18, result); } - + [Fact] public void Extrapolator_Extrapolate__Example2() { @@ -43,7 +43,7 @@ public class Day09_Tests long result = extrapolator.Extrapolate(); Assert.Equal(28, result); } - + [Fact] public void Extrapolator_Extrapolate__Example3() { @@ -59,7 +59,7 @@ public class Day09_Tests long result = extrapolator.ExtrapolatePast(); Assert.Equal(-3, result); } - + [Fact] public void Extrapolator_ExtrapolatePast__Example2() { @@ -67,7 +67,7 @@ public class Day09_Tests long result = extrapolator.ExtrapolatePast(); Assert.Equal(0, result); } - + [Fact] public void Extrapolator_ExtrapolatePast__Example3() { diff --git a/AdventOfCode2023.Tests/Day10_Tests.cs b/AdventOfCode2023.Tests/Day10_Tests.cs index 930bf22..de84a68 100644 --- a/AdventOfCode2023.Tests/Day10_Tests.cs +++ b/AdventOfCode2023.Tests/Day10_Tests.cs @@ -2,25 +2,25 @@ namespace AdventOfCode2023.Tests; public class Day10_Tests { - private readonly string[] _example1 = { + private readonly string[] _example1 = [ "7-F7-", ".FJ|7", "SJLL7", "|F--J", "LJ.LJ", - }; + ]; [Fact] public void ResolvePart1__Example1() { Day10 day = new(); - + string result = day.ResolvePart1(_example1); Assert.Equal("8", result); } - - private readonly string[] _example2 = { + + private readonly string[] _example2 = [ "...........", ".S-------7.", ".|F-----7|.", @@ -30,20 +30,20 @@ public class Day10_Tests ".|..|.|..|.", ".L--J.L--J.", "...........", - }; + ]; [Fact] public void ResolvePart2__Example2() { Day10 day = new(); - + string result = day.ResolvePart2(_example2); Assert.Equal("4", result); } - - private readonly string[] _example3 = { + + private readonly string[] _example3 = [ "FF7FSF7F7F7F7F7F---7", "L|LJ||||||||||||F--J", "FL-7LJLJ||||||LJL-77", @@ -54,13 +54,13 @@ public class Day10_Tests "7-L-JL7||F7|L7F-7F7|", "L.L7LFJ|||||FJL7||LJ", "L7JLJL-JLJLJL--JLJ.L", - }; + ]; [Fact] public void ResolvePart2__Example3() { Day10 day = new(); - + string result = day.ResolvePart2(_example3); Assert.Equal("10", result); diff --git a/AdventOfCode2023.Tests/Day11_Tests.cs b/AdventOfCode2023.Tests/Day11_Tests.cs index ce53ea9..ef238e3 100644 --- a/AdventOfCode2023.Tests/Day11_Tests.cs +++ b/AdventOfCode2023.Tests/Day11_Tests.cs @@ -2,7 +2,7 @@ namespace AdventOfCode2023.Tests; public class Day11_Tests { - private readonly string[] _example1 = { + private readonly string[] _example1 = [ "...#......", ".......#..", "#.........", @@ -13,16 +13,15 @@ public class Day11_Tests "..........", ".......#..", "#...#.....", - }; + ]; [Fact] public void ResolvePart1__Example1() { Day11 day = new(); - + string result = day.ResolvePart1(_example1); Assert.Equal("374", result); } - } \ No newline at end of file diff --git a/AdventOfCode2023.Tests/Day12_Tests.cs b/AdventOfCode2023.Tests/Day12_Tests.cs index a25dfa0..2d0762c 100644 --- a/AdventOfCode2023.Tests/Day12_Tests.cs +++ b/AdventOfCode2023.Tests/Day12_Tests.cs @@ -2,14 +2,14 @@ namespace AdventOfCode2023.Tests; public class Day12_Tests { - private readonly string[] _example1 = { + private readonly string[] _example1 = [ "???.### 1,1,3", ".??..??...?##. 1,1,3", "?#?#?#?#?#?#?#? 1,3,1,6", "????.#...#... 4,1,1", "????.######..#####. 1,6,5", "?###???????? 3,2,1", - }; + ]; [Fact] public void ResolvePart1__Example1() diff --git a/AdventOfCode2023.Tests/Day24_Tests.cs b/AdventOfCode2023.Tests/Day24_Tests.cs index 0197eb7..f133396 100644 --- a/AdventOfCode2023.Tests/Day24_Tests.cs +++ b/AdventOfCode2023.Tests/Day24_Tests.cs @@ -10,14 +10,14 @@ namespace AdventOfCode2023.Tests; public class Day24_Tests { - private string[] _example1 = { + private string[] _example1 = [ "19, 13, 30 @ -2, 1, -2", "18, 19, 22 @ -1, -1, -2", "20, 25, 34 @ -2, -2, -4", "12, 31, 28 @ -1, -2, -1", "20, 19, 15 @ 1, -5, -3", - }; - + ]; + [Fact] public void ResolvePart1__Example1() { @@ -36,36 +36,35 @@ public class Day24_Tests Day24.Hail hailC = new("20, 25, 34 @ -2, -2, -4"); Day24.Hail hailD = new("12, 31, 28 @ -1, -2, -1"); Day24.Hail hailE = new("20, 19, 15 @ 1, -5, -3"); - + (bool intersect_A_B, _, _, _) = hailA.Intersect2D(hailB); Assert.True(intersect_A_B); - + (bool intersect_A_C, _, _, _) = hailA.Intersect2D(hailC); Assert.True(intersect_A_C); - + (bool intersect_A_D, _, _, _) = hailA.Intersect2D(hailD); Assert.True(intersect_A_D); - + (bool intersect_A_E, _, _, _) = hailA.Intersect2D(hailE); Assert.True(intersect_A_E); - + (bool intersect_B_C, _, _, _) = hailB.Intersect2D(hailC); Assert.False(intersect_B_C); - + (bool intersect_B_D, _, _, _) = hailB.Intersect2D(hailD); Assert.True(intersect_B_D); - + (bool intersect_B_E, _, _, _) = hailB.Intersect2D(hailE); Assert.True(intersect_B_E); - + (bool intersect_C_D, _, _, _) = hailC.Intersect2D(hailD); Assert.True(intersect_C_D); - + (bool intersect_C_E, _, _, _) = hailC.Intersect2D(hailE); Assert.True(intersect_C_E); - + (bool intersect_D_E, _, _, _) = hailD.Intersect2D(hailE); Assert.True(intersect_D_E); } - } \ No newline at end of file diff --git a/AdventOfCode2023/AdventOfCode2023.csproj b/AdventOfCode2023/AdventOfCode2023.csproj index 0753dfe..099f4ac 100644 --- a/AdventOfCode2023/AdventOfCode2023.csproj +++ b/AdventOfCode2023/AdventOfCode2023.csproj @@ -15,7 +15,7 @@ - + diff --git a/AdventOfCode2023/Day01.cs b/AdventOfCode2023/Day01.cs index a47b7d6..bae3d07 100644 --- a/AdventOfCode2023/Day01.cs +++ b/AdventOfCode2023/Day01.cs @@ -78,7 +78,7 @@ public class Day01 : IDay public string ResolvePart2(string[] inputs) { - List<(string Text, int Value)> digits = new() { + List<(string Text, int Value)> digits = [ ("1", 1), ("2", 2), ("3", 3), @@ -97,7 +97,7 @@ public class Day01 : IDay ("seven", 7), ("eight", 8), ("nine", 9), - }; + ]; int sum = 0; foreach (string line in inputs) diff --git a/AdventOfCode2023/Day03.cs b/AdventOfCode2023/Day03.cs index 38018eb..e78c062 100644 --- a/AdventOfCode2023/Day03.cs +++ b/AdventOfCode2023/Day03.cs @@ -102,7 +102,7 @@ public class Day03 : IDay for (column = 0; column < inputs[row].Length; column++) { if (inputs[row][column] != '*') { continue; } - + List adjacentNumbers = numbers.Where(n => CellAdjacentToSchemaNumber(row, column, n)).ToList(); if (adjacentNumbers.Count != 2) { continue; } @@ -115,11 +115,11 @@ public class Day03 : IDay public struct SchemaNumber { - public string[] Schema { get; set; } - public int Row { get; set; } - public int Column { get; set; } - public int Lenght { get; set; } - public int Value { get; set; } + public string[] Schema { get; init; } + public int Row { get; init; } + public int Column { get; init; } + public int Lenght { get; init; } + public int Value { get; init; } } public static SchemaNumber? SearchNextSchemaNumber(string[] schema, int initialRow, int initialColumn) @@ -176,7 +176,7 @@ public class Day03 : IDay } return false; } - + private static bool CellAdjacentToSchemaNumber(int row, int column, SchemaNumber number) { int minRow = number.Row - 1; @@ -188,8 +188,7 @@ public class Day03 : IDay { return true; } - + return false; } - } \ No newline at end of file diff --git a/AdventOfCode2023/Day04.cs b/AdventOfCode2023/Day04.cs index 538bd85..093b255 100644 --- a/AdventOfCode2023/Day04.cs +++ b/AdventOfCode2023/Day04.cs @@ -118,7 +118,7 @@ public class Day04 : IDay _winningNumbers = GetNumbers(strColumns[0]); _myNumbers = GetNumbers(strColumns[1]); } - + private static List GetNumbers(string strNumbers) { string[] parts = strNumbers.Split(' '); @@ -135,6 +135,5 @@ public class Day04 : IDay { return _myNumbers.Count(myNumber => _winningNumbers.Contains(myNumber)); } - } } \ No newline at end of file diff --git a/AdventOfCode2023/Day05.cs b/AdventOfCode2023/Day05.cs index bbd3f23..879a6ee 100644 --- a/AdventOfCode2023/Day05.cs +++ b/AdventOfCode2023/Day05.cs @@ -136,7 +136,7 @@ public class Day05 : IDay LinesReader reader = new(inputs); Almanac? almanac = Almanac.Parse(reader); int i = 0; - List ranges = new(); + List ranges = []; while (almanac?.Seeds.Count > i) { long seed = almanac.Seeds[i]; @@ -155,21 +155,14 @@ public class Day05 : IDay } - public class LinesReader + public class LinesReader(string[] lines) { - private readonly string[] _lines; private int _index; - public LinesReader(string[] lines) - { - _lines = lines; - _index = 0; - } - public string? GetLine() { - if (_index >= _lines.Length) { return null; } - string line = _lines[_index]; + if (_index >= lines.Length) { return null; } + string line = lines[_index]; _index++; return line; } @@ -298,7 +291,7 @@ public class Day05 : IDay { public string Name { get; private init; } = string.Empty; - public List RangeMappings { get; } = new(); + public List RangeMappings { get; } = []; public static AlmanacMapping? ParseNext(LinesReader reader) { @@ -346,29 +339,32 @@ public class Day05 : IDay public List Apply(AlmanacRangeMapping range) { - List unMappedRanges = new() { + List unMappedRanges = [ range, - }; - List newUnMappedRanges = new(); - List mappedRanges = new(); + ]; + List newUnMappedRanges = []; + List mappedRanges = []; int i = 0; while (RangeMappings.Count > i) { AlmanacRangeMapping rangeMapping = RangeMappings[i]; - for (int j = 0; j < unMappedRanges.Count; j++) + foreach (AlmanacRangeMapping unMappedRange in unMappedRanges) { - AlmanacRangeMapping.ClipResult result = rangeMapping.Clip(unMappedRanges[j]); + AlmanacRangeMapping.ClipResult result = rangeMapping.Clip(unMappedRange); if (result.Clipped != null) { mappedRanges.Add(result.Clipped.Value); } if (result.PreClip != null) { newUnMappedRanges.Add(result.PreClip.Value); } if (result.PostClip != null) { newUnMappedRanges.Add(result.PostClip.Value); } } unMappedRanges = newUnMappedRanges; - newUnMappedRanges = new List(); + newUnMappedRanges = []; i++; } - return mappedRanges.Union(unMappedRanges).ToList(); + List resultMappedRanges = []; + resultMappedRanges.AddRange(mappedRanges); + resultMappedRanges.AddRange(unMappedRanges); + return resultMappedRanges; } } @@ -381,7 +377,7 @@ public class Day05 : IDay public List Seeds { get; } - private List Mappings { get; } = new(); + private List Mappings { get; } = []; public static Almanac? Parse(LinesReader reader) { diff --git a/AdventOfCode2023/Day07.cs b/AdventOfCode2023/Day07.cs index 36c76b2..130dab1 100644 --- a/AdventOfCode2023/Day07.cs +++ b/AdventOfCode2023/Day07.cs @@ -114,9 +114,9 @@ public class Day07 : IDay public class CamelCard { - private readonly static char[] Labels = { 'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2' }; + private readonly static char[] Labels = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']; - private readonly static char[] LabelsWithJoker = { 'A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J' }; + private readonly static char[] LabelsWithJoker = ['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J']; private const char JokerLabel = 'J'; public enum Types @@ -174,7 +174,7 @@ public class Day07 : IDay dictLabelCounts.Add(JokerLabel, 5); } } - + if (dictLabelCounts.Count == 1) { return Types.FiveOfAKind; } if (dictLabelCounts.Count == 5) { return Types.HighCard; } if (dictLabelCounts.Count == 2) diff --git a/AdventOfCode2023/Day08.cs b/AdventOfCode2023/Day08.cs index 65da218..e80b3c0 100644 --- a/AdventOfCode2023/Day08.cs +++ b/AdventOfCode2023/Day08.cs @@ -105,18 +105,11 @@ public class Day08 : IDay return steps.ToString(); } - private class MapNode + private class MapNode(string strMapNode) { - public string Key { get; set; } - public string LeftKey { get; set; } - public string RightKey { get; set; } - - public MapNode(string strMapNode) - { - Key = strMapNode.Substring(0, 3); - LeftKey = strMapNode.Substring(7, 3); - RightKey = strMapNode.Substring(12, 3); - } + public string Key { get; } = strMapNode.Substring(0, 3); + public string LeftKey { get; } = strMapNode.Substring(7, 3); + public string RightKey { get; } = strMapNode.Substring(12, 3); } private class Map @@ -155,32 +148,23 @@ public class Day08 : IDay } } - private class MapWalker + private class MapWalker(Map map, string startKey) { - private readonly Map _map; - - private MapNode _currentNode; + private MapNode _currentNode = map.GetByKey(startKey) ?? map.Nodes.First(); private long _steps; - public MapWalker(Map map, string startKey) - { - _map = map; - _currentNode = map.GetByKey(startKey) ?? map.Nodes.First(); - _steps = 0; - } - public void StepWhile(Func condition) { while (condition(this)) { - char leftRightInstruction = _map.GetInstruction(_steps); + char leftRightInstruction = map.GetInstruction(_steps); if (leftRightInstruction == 'L') { - _currentNode = _map.GetByKey(_currentNode.LeftKey) ?? _map.Nodes.First(); + _currentNode = map.GetByKey(_currentNode.LeftKey) ?? map.Nodes.First(); } if (leftRightInstruction == 'R') { - _currentNode = _map.GetByKey(_currentNode.RightKey) ?? _map.Nodes.First(); + _currentNode = map.GetByKey(_currentNode.RightKey) ?? map.Nodes.First(); } _steps++; } @@ -196,7 +180,7 @@ public class Day08 : IDay get { return _steps; } } } - + // https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations private static long GreatestCommonDivisor(long a, long b) { diff --git a/AdventOfCode2023/Day09.cs b/AdventOfCode2023/Day09.cs index 223e790..ee381d5 100644 --- a/AdventOfCode2023/Day09.cs +++ b/AdventOfCode2023/Day09.cs @@ -164,15 +164,15 @@ public class Day09 : IDay return newLastNumber; } - + public long ExtrapolatePast() { - _derivatives.Last().Insert(0,0); + _derivatives.Last().Insert(0, 0); for (int i = _derivatives.Count - 2; i >= 0; i--) { long firstDerivative1 = _derivatives[i].First(); long firstDerivative0 = _derivatives[i + 1].First(); - _derivatives[i].Insert(0,firstDerivative1 - firstDerivative0); + _derivatives[i].Insert(0, firstDerivative1 - firstDerivative0); } long firstNumber = _numbers.First(); long firstDerivative = _derivatives[0].First(); diff --git a/AdventOfCode2023/Day10.cs b/AdventOfCode2023/Day10.cs index b9bf78e..5460ca0 100644 --- a/AdventOfCode2023/Day10.cs +++ b/AdventOfCode2023/Day10.cs @@ -228,16 +228,10 @@ public class Day10 : IDay return countInside.ToString(); } - private struct Point + private struct Point(int x, int y) { - public int X; - public int Y; - - public Point(int x, int y) - { - X = x; - Y = y; - } + public int X = x; + public int Y = y; } private class PipeMaze @@ -323,7 +317,7 @@ public class Day10 : IDay x: point.X + 1, y: point.Y); } - if(point.Y == prevPoint.Y && prevPoint.X > point.X) + if (point.Y == prevPoint.Y && prevPoint.X > point.X) { return new Point( x: point.X, @@ -339,7 +333,7 @@ public class Day10 : IDay x: point.X - 1, y: point.Y); } - if(point.Y == prevPoint.Y && prevPoint.X < point.X) + if (point.Y == prevPoint.Y && prevPoint.X < point.X) { return new Point( x: point.X, @@ -355,7 +349,7 @@ public class Day10 : IDay x: point.X - 1, y: point.Y); } - if(point.Y == prevPoint.Y && prevPoint.X < point.X) + if (point.Y == prevPoint.Y && prevPoint.X < point.X) { return new Point( x: point.X, @@ -371,7 +365,7 @@ public class Day10 : IDay x: point.X + 1, y: point.Y); } - if(point.Y == prevPoint.Y && prevPoint.X > point.X) + if (point.Y == prevPoint.Y && prevPoint.X > point.X) { return new Point( x: point.X, @@ -397,7 +391,7 @@ public class Day10 : IDay { Point? nextPoint = FollowPipe(point, prevPoint); if (nextPoint == null) { break; } - + distance++; int? currentDistance = GetDistance(point); if (currentDistance > distance || currentDistance == null) @@ -409,7 +403,7 @@ public class Day10 : IDay point = nextPoint.Value; } while (true); } - + public int GetMaxDistance() { int max = int.MinValue; @@ -438,10 +432,10 @@ public class Day10 : IDay private char CalculateCell(Point point) { - Point? pointRight =FollowPipe(new Point(point.X + 1, point.Y), point); - Point? pointLeft =FollowPipe(new Point(point.X - 1, point.Y), point); - Point? pointDown =FollowPipe(new Point(point.X, point.Y + 1), point); - Point? pointUp =FollowPipe(new Point(point.X, point.Y - 1), point); + Point? pointRight = FollowPipe(new Point(point.X + 1, point.Y), point); + Point? pointLeft = FollowPipe(new Point(point.X - 1, point.Y), point); + Point? pointDown = FollowPipe(new Point(point.X, point.Y + 1), point); + Point? pointUp = FollowPipe(new Point(point.X, point.Y - 1), point); if (pointRight != null && pointLeft == null && pointDown != null && pointUp == null) { return 'F'; @@ -507,12 +501,12 @@ public class Day10 : IDay } else if (cell == 'J') { - if(cellStart == 'F') + if (cellStart == 'F') { inside = inside != true; cellStart = null; } - else if(cellStart == 'L') + else if (cellStart == 'L') { cellStart = null; } @@ -523,23 +517,23 @@ public class Day10 : IDay { cellStart = 'F'; } - else if(cellStart == 'J') + else if (cellStart == 'J') { inside = inside != true; cellStart = null; } - else if(cellStart == '7') + else if (cellStart == '7') { cellStart = null; } } else if (cell == '7') { - if(cellStart == 'F') + if (cellStart == 'F') { cellStart = null; } - else if(cellStart == 'L') + else if (cellStart == 'L') { inside = inside != true; cellStart = null; @@ -551,11 +545,11 @@ public class Day10 : IDay { cellStart = 'L'; } - else if(cellStart == 'J') + else if (cellStart == 'J') { cellStart = null; } - else if(cellStart == '7') + else if (cellStart == '7') { inside = inside != true; cellStart = null; diff --git a/AdventOfCode2023/Day11.cs b/AdventOfCode2023/Day11.cs index df0b2f3..e058040 100644 --- a/AdventOfCode2023/Day11.cs +++ b/AdventOfCode2023/Day11.cs @@ -130,8 +130,8 @@ public class Day11 : IDay private struct Point { - public long X { get; set; } - public long Y { get; set; } + public long X { get; init; } + public long Y { get; init; } } private class GalaxyMap diff --git a/AdventOfCode2023/Day12.cs b/AdventOfCode2023/Day12.cs index 0c8014a..dc0dd0f 100644 --- a/AdventOfCode2023/Day12.cs +++ b/AdventOfCode2023/Day12.cs @@ -1,4 +1,3 @@ -using System.Text; namespace AdventOfCode2023; @@ -212,7 +211,7 @@ public class Day12 : IDay { throw new Exception("Invalid input"); } - + if (springsRecord.Length < damagedSprings[0]) { // Invalid match; Not enough data on record for the current damagedSprings group @@ -230,7 +229,7 @@ public class Day12 : IDay // Skip current damagedSprings group return CountPossibleArrangements(springsRecord.Substring(damagedSprings[0]), damagedSprings.Skip(1).ToArray()); } - + if (springsRecord.Length < (damagedSprings[0] + 1) || springsRecord[damagedSprings[0]] == '#') { // Invalid match; There must be an operational (or unknown) spring between damaged spring groups diff --git a/AdventOfCode2023/Day24.cs b/AdventOfCode2023/Day24.cs index fe0841f..c4bb66f 100644 --- a/AdventOfCode2023/Day24.cs +++ b/AdventOfCode2023/Day24.cs @@ -103,24 +103,17 @@ public class Day24 : IDay throw new NotImplementedException(); } - public struct Vector3D + public struct Vector3D(long x, long y, long z) { - public readonly long X; - public readonly long Y; - public readonly long Z; - - public Vector3D(long x, long y, long z) - { - X = x; - Y = y; - Z = z; - } + public readonly long X = x; + public readonly long Y = y; + public readonly long Z = z; } public readonly struct Hail { public readonly Vector3D Position; - public readonly Vector3D Velocity; + private readonly Vector3D _velocity; public Hail(string input) { @@ -131,7 +124,7 @@ public class Day24 : IDay y: Convert.ToInt64(strPosition[1]), z: Convert.ToInt64(strPosition[2])); string[] strVelocity = parts[1].Split(", "); - Velocity = new Vector3D( + _velocity = new Vector3D( x: Convert.ToInt64(strVelocity[0]), y: Convert.ToInt64(strVelocity[1]), z: Convert.ToInt64(strVelocity[2])); @@ -139,21 +132,21 @@ public class Day24 : IDay public (bool intersects, double s, double t, Vector3D point) Intersect2D(Hail other) { - long s_Div = (-other.Velocity.X * Velocity.Y + Velocity.X * other.Velocity.Y); + long s_Div = (-other._velocity.X * _velocity.Y + _velocity.X * other._velocity.Y); if (s_Div == 0) { return (false, 0, 0, new Vector3D()); } - double s = (-Velocity.Y * (Position.X - other.Position.X) + Velocity.X * (Position.Y - other.Position.Y)) / (double)s_Div; + double s = (-_velocity.Y * (Position.X - other.Position.X) + _velocity.X * (Position.Y - other.Position.Y)) / (double)s_Div; - long t_Div = (-other.Velocity.X * Velocity.Y + Velocity.X * other.Velocity.Y); + long t_Div = (-other._velocity.X * _velocity.Y + _velocity.X * other._velocity.Y); if (t_Div == 0) { return (false, 0, 0, new Vector3D()); } - double t = (other.Velocity.X * (Position.Y - other.Position.Y) - other.Velocity.Y * (Position.X - other.Position.X)) / (double)t_Div; + double t = (other._velocity.X * (Position.Y - other.Position.Y) - other._velocity.Y * (Position.X - other.Position.X)) / (double)t_Div; - Vector3D intersection = new((long)(Position.X + t * Velocity.X), (long)(Position.Y + t * Velocity.Y), 0); + Vector3D intersection = new((long)(Position.X + t * _velocity.X), (long)(Position.Y + t * _velocity.Y), 0); return (true, s, t, intersection); } } diff --git a/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj b/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj index 2f890c0..3497b52 100644 --- a/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj +++ b/AdventOfCode2024.Tests/AdventOfCode2024.Tests.csproj @@ -3,7 +3,7 @@ net8.0 enable - AdventOfCode2023.Tests + AdventOfCode2024.Tests enable false @@ -24,7 +24,7 @@ - + diff --git a/AdventOfCode2024.Tests/Day01_Tests.cs b/AdventOfCode2024.Tests/Day01_Tests.cs index f7a521f..fbfa652 100644 --- a/AdventOfCode2024.Tests/Day01_Tests.cs +++ b/AdventOfCode2024.Tests/Day01_Tests.cs @@ -5,33 +5,33 @@ public class Day01_Tests [Fact] public void ResolvePart1__Example() { - var day = new Day01(); + Day01 day = new Day01(); - string result = day.ResolvePart1(new[] { + string result = day.ResolvePart1([ "3 4", "4 3", "2 5", "1 3", "3 9", "3 3", - }); + ]); Assert.Equal("11", result); } - + [Fact] public void ResolvePart2__Example() { - var day = new Day01(); + Day01 day = new Day01(); - string result = day.ResolvePart2(new[] { + string result = day.ResolvePart2([ "3 4", "4 3", "2 5", "1 3", "3 9", "3 3", - }); + ]); Assert.Equal("31", result); } diff --git a/AdventOfCode2024/Day01.cs b/AdventOfCode2024/Day01.cs index cae425c..cf8169e 100644 --- a/AdventOfCode2024/Day01.cs +++ b/AdventOfCode2024/Day01.cs @@ -111,12 +111,12 @@ public class Day01 : IDay leftNumbers.Add(int.Parse(numbers[0])); rightNumbers.Add(int.Parse(numbers[1])); } - + long totalSim = 0; - for (int i = 0; i < leftNumbers.Count; i++) + foreach (int number in leftNumbers) { - int count = rightNumbers.Count(x => x == leftNumbers[i]); - int sim = leftNumbers[i] * count; + int count = rightNumbers.Count(x => x == number); + int sim = number * count; totalSim += sim; } return totalSim.ToString();