From 6c2c1706e8b27f3e2338f04be179ac2eaa0500a4 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Wed, 6 Dec 2023 03:19:44 +0100 Subject: [PATCH] =?UTF-8?q?AdventOfCode=202023=20Day05=20Part2,=20Na=C3=AF?= =?UTF-8?q?ve=20implementation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AdventOfCode2023.Tests/Day05_Tests.cs | 10 ++++++++ AdventOfCode2023/Day05.cs | 36 +++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/AdventOfCode2023.Tests/Day05_Tests.cs b/AdventOfCode2023.Tests/Day05_Tests.cs index aa8a3d4..cde6463 100644 --- a/AdventOfCode2023.Tests/Day05_Tests.cs +++ b/AdventOfCode2023.Tests/Day05_Tests.cs @@ -48,6 +48,16 @@ public class Day05_Tests Assert.Equal("35", result); } + [Fact] + public void ResolvePart2__Example() + { + Day05 day = new(); + + string result = day.ResolvePart2(_example); + + Assert.Equal("46", result); + } + [Fact] public void AlamanacMapping_ParseNext__Empty__Null() { diff --git a/AdventOfCode2023/Day05.cs b/AdventOfCode2023/Day05.cs index 921133c..cf02ad0 100644 --- a/AdventOfCode2023/Day05.cs +++ b/AdventOfCode2023/Day05.cs @@ -102,6 +102,23 @@ So, the lowest location number in this example is 35. What is the lowest location number that corresponds to any of the initial seed numbers? + +--- Part Two --- + +Everyone will starve if you only plant such a small number of seeds. Re-reading the almanac, it looks like the seeds: line actually describes ranges of seed numbers. + +The values on the initial seeds: line come in pairs. Within each pair, the first value is the start of the range and the second value is the length of the range. So, in the first line of the example above: + +seeds: 79 14 55 13 + +This line describes two ranges of seed numbers to be planted in the garden. The first range starts with seed number 79 and contains 14 values: 79, 80, ..., 91, 92. The second range starts with seed number 55 and contains 13 values: 55, 56, ..., 66, 67. + +Now, rather than considering four seed numbers, you need to consider a total of 27 seed numbers. + +In the above example, the lowest location number can be obtained from seed number 82, which corresponds to soil 84, fertilizer 84, water 84, light 77, temperature 45, humidity 46, and location 46. So, the lowest location number is 46. + +Consider all of the initial seed numbers listed in the ranges on the first line of the almanac. What is the lowest location number that corresponds to any of the initial seed numbers? + */ public class Day05 : IDay @@ -116,7 +133,22 @@ public class Day05 : IDay public string ResolvePart2(string[] inputs) { - throw new NotImplementedException(); + LinesReader reader = new(inputs); + Almanac? almanac = Almanac.Parse(reader); + long minLocation = long.MaxValue; + int i = 0; + while (almanac?.Seeds.Count > i) + { + long seed = almanac?.Seeds[i] ?? 0; + long seedLen = almanac?.Seeds[i + 1] ?? 0; + for (long j = 0; j < seedLen; j++) + { + long currentSeed = almanac?.ApplyMapping(seed + j) ?? 0; + if (currentSeed < minLocation) { minLocation = currentSeed; } + } + i += 2; + } + return minLocation.ToString(); } @@ -189,7 +221,7 @@ public class Day05 : IDay }; mapping.RangeMappings.Add(rangeMapping); } while (true); - + return mapping; }