Completed Day 05 (finally! and ugly)
This commit is contained in:
parent
047f841a6c
commit
efc3fa3b1b
@ -7,9 +7,9 @@ InputReader inputReader = new()
|
||||
//IsDebug = true
|
||||
};
|
||||
|
||||
//inputReader.SetInputByChallange(10);
|
||||
inputReader.SetInputByChallange(5);
|
||||
|
||||
IChallange challange = new Day10(inputReader);
|
||||
IChallange challange = new Day05(inputReader);
|
||||
|
||||
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
||||
|
||||
|
||||
@ -24,7 +24,8 @@ namespace AdventOfCode.Core.Shared.Grid
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Start.Y},{Start.X}] {Value}";
|
||||
return $"[{Start.X},{End.X}] {Length}";
|
||||
//return $"[{Start.Y},{Start.X}] {Value}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,8 +3,6 @@ using AdventOfCode.Core.Shared.Grid;
|
||||
|
||||
namespace AdventOfCode.Solutions._2023
|
||||
{
|
||||
|
||||
|
||||
public class Day05(InputReader reader) : IChallange
|
||||
{
|
||||
private readonly InputReader _inputReader = reader;
|
||||
@ -61,8 +59,6 @@ namespace AdventOfCode.Solutions._2023
|
||||
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
return string.Empty;
|
||||
|
||||
List<string> data = new(await _inputReader.ReadAsArrayString());
|
||||
|
||||
int stringSeperator = data.IndexOf(SeedSoil) + 1;
|
||||
@ -82,17 +78,23 @@ namespace AdventOfCode.Solutions._2023
|
||||
|
||||
long[] seedData = data[0].Split(' ').Skip(1).Select(long.Parse).ToArray();
|
||||
List<Line> ranges = [];
|
||||
long lowestLocation = long.MaxValue;
|
||||
for(int index = 0; index < seedData.Length / 2; index++)
|
||||
{
|
||||
ranges.Add(new Line(seedData[index], 0, seedData[index + 1]));
|
||||
Line[] seedRange = [new Line(seedData[index * 2], 0, seedData[(index * 2) + 1])];
|
||||
Line[] soilMapped = GetTargetLocation(soil, seedRange);
|
||||
Line[] fertilizerMapped = GetTargetLocation(fertilizer, soilMapped);
|
||||
Line[] waterMapped = GetTargetLocation(water, fertilizerMapped);
|
||||
Line[] lightMapped = GetTargetLocation(light, waterMapped);
|
||||
Line[] temperatureMapped = GetTargetLocation(temperature, lightMapped);
|
||||
Line[] humidityMapped = GetTargetLocation(humidity, temperatureMapped);
|
||||
Line[] locationMapped = GetTargetLocation(location, humidityMapped);
|
||||
|
||||
ranges.Add(locationMapped.OrderBy(l => l.Start.X).First());
|
||||
}
|
||||
|
||||
return lowestLocation.ToString();
|
||||
return ranges.OrderBy(l => l.Start.X).First().Start.X.ToString();
|
||||
}
|
||||
|
||||
private record Range(long Start, long Disrance);
|
||||
|
||||
private static List<InOutMapper> MapData(List<string> data, int startIndex, int endIndex)
|
||||
{
|
||||
return data
|
||||
@ -128,21 +130,45 @@ namespace AdventOfCode.Solutions._2023
|
||||
{
|
||||
// I do not expect to hit this but just to be sure.
|
||||
resultRanges.Add(currentSeedRange);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mappers[0].Start.X > currentSeedRange.Start.X)
|
||||
{
|
||||
|
||||
long lastId = currentSeedRange.Start.X;
|
||||
|
||||
foreach(InOutMapper mapper in mappers.OrderBy(m => m.Start.X))
|
||||
{
|
||||
//List<Line> mapperResults = [];
|
||||
if (lastId < mapper.SourceStart)
|
||||
{
|
||||
// there is some uneffected space infront of the mapper
|
||||
resultRanges.Add(new Line(lastId, 0, mapper.SourceStart - lastId));
|
||||
lastId = mapper.SourceStart;
|
||||
}
|
||||
|
||||
long end = mapper.End.X > currentSeedRange.End.X ? currentSeedRange.End.X : mapper.End.X;
|
||||
Line map = new(lastId, 0, end - lastId + 1);
|
||||
map.Start.X += mapper.Delta;
|
||||
map.End.X += mapper.Delta;
|
||||
resultRanges.Add(map);
|
||||
lastId = end + 1;
|
||||
}
|
||||
|
||||
if (lastId < currentSeedRange.End.X)
|
||||
{
|
||||
Line map = new(new Point(lastId, 0, ""), currentSeedRange.End);
|
||||
resultRanges.Add(map);
|
||||
}
|
||||
}
|
||||
|
||||
// merge the parts
|
||||
// validate
|
||||
if (seedRange.Sum(r => r.Length) != resultRanges.Sum(r => r.Length))
|
||||
{
|
||||
Console.WriteLine("Lost seeds!");
|
||||
}
|
||||
|
||||
return [.. resultRanges];
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class SeedLocationMapper
|
||||
{
|
||||
public long Seed { get; set; }
|
||||
|
||||
@ -1,10 +1,33 @@
|
||||
FF7FSF7F7F7F7F7F---7
|
||||
L|LJ||||||||||||F--J
|
||||
FL-7LJLJ||||||LJL-77
|
||||
F--JF--7||LJLJ7F7FJ-
|
||||
L---JF-JLJ.||-FJLJJ7
|
||||
|F|F-JF---7F7-L7L|7|
|
||||
|FFJF7L7F-JF7|JL---7
|
||||
7-L-JL7||F7|L7F-7F7|
|
||||
L.L7LFJ|||||FJL7||LJ
|
||||
L7JLJL-JLJLJL--JLJ.L
|
||||
seeds: 79 14 55 13
|
||||
|
||||
seed-to-soil map:
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
soil-to-fertilizer map:
|
||||
0 15 37
|
||||
37 52 2
|
||||
39 0 15
|
||||
|
||||
fertilizer-to-water map:
|
||||
49 53 8
|
||||
0 11 42
|
||||
42 0 7
|
||||
57 7 4
|
||||
|
||||
water-to-light map:
|
||||
88 18 7
|
||||
18 25 70
|
||||
|
||||
light-to-temperature map:
|
||||
45 77 23
|
||||
81 45 19
|
||||
68 64 13
|
||||
|
||||
temperature-to-humidity map:
|
||||
0 69 1
|
||||
1 0 69
|
||||
|
||||
humidity-to-location map:
|
||||
60 56 37
|
||||
56 93 4
|
||||
Loading…
Reference in New Issue
Block a user