diff --git a/AdventOfCode2024.Tests/Day01_Tests.cs b/AdventOfCode2024.Tests/Day01_Tests.cs index f53ab63..f7a521f 100644 --- a/AdventOfCode2024.Tests/Day01_Tests.cs +++ b/AdventOfCode2024.Tests/Day01_Tests.cs @@ -19,4 +19,20 @@ public class Day01_Tests Assert.Equal("11", result); } + [Fact] + public void ResolvePart2__Example() + { + var day = new Day01(); + + string result = day.ResolvePart2(new[] { + "3 4", + "4 3", + "2 5", + "1 3", + "3 9", + "3 3", + }); + + Assert.Equal("31", result); + } } \ No newline at end of file diff --git a/AdventOfCode2024/Day01.cs b/AdventOfCode2024/Day01.cs index 451b5ea..cae425c 100644 --- a/AdventOfCode2024/Day01.cs +++ b/AdventOfCode2024/Day01.cs @@ -43,6 +43,39 @@ To find the total distance between the left list and the right list, add up the Your actual left and right lists contain many location IDs. What is the total distance between your lists? +--- Part Two --- + +Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. + +Or are they? + +The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. + +This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. + +Here are the same example lists again: + +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 + +For these example lists, here is the process of finding the similarity score: + + The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. + The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. + The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). + The fourth number, 1, also does not appear in the right list. + The fifth number, 3, appears in the right list three times; the similarity score increases by 9. + The last number, 3, appears in the right list three times; the similarity score again increases by 9. + +So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). + +Once again consider your left and right lists. What is their similarity score? + + */ public class Day01 : IDay @@ -70,6 +103,22 @@ public class Day01 : IDay public string ResolvePart2(string[] inputs) { - throw new NotImplementedException(); + List leftNumbers = new(); + List rightNumbers = new(); + foreach (string input in inputs) + { + string[] numbers = input.Split(" "); + leftNumbers.Add(int.Parse(numbers[0])); + rightNumbers.Add(int.Parse(numbers[1])); + } + + long totalSim = 0; + for (int i = 0; i < leftNumbers.Count; i++) + { + int count = rightNumbers.Count(x => x == leftNumbers[i]); + int sim = leftNumbers[i] * count; + totalSim += sim; + } + return totalSim.ToString(); } } \ No newline at end of file