From 70a86f783a2014d81bf3d3c1f45290482f97398f Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Sat, 9 Dec 2023 21:17:03 +0000 Subject: [PATCH] [2023] Add Day5 Part1 Signed-off-by: TiagoRG --- Year2023/Day5.cs | 66 ++++++++++++++++++++++++++++++++++++++++++ Year2023/Loader2023.cs | 1 + 2 files changed, 67 insertions(+) create mode 100644 Year2023/Day5.cs diff --git a/Year2023/Day5.cs b/Year2023/Day5.cs new file mode 100644 index 0000000..8f6ea57 --- /dev/null +++ b/Year2023/Day5.cs @@ -0,0 +1,66 @@ +namespace AdventOfCode.Year2023; + +public static class Day5 +{ + private class Converter + { + public string? Name { get; set; } + public long Start { get; set; } + public long End { get; set; } + public long Count { get; set; } + } + private static string[] MapTypes = { "seed-to-soil", "soil-to-fertilizer", "fertilizer-to-water", "water-to-light", "light-to-temperature", "temperature-to-humidity", "humidity-to-location", }; + private static List Converters = new List(); + + private static string? CurrentMap; + + public static void Run() + { + string[] input = File.ReadAllLines("inputs/Year2023/day5.txt")[2..].ToArray(); + + foreach (var line in input) + { + if (String.IsNullOrEmpty(line)) + { + continue; + } + if (Char.IsLetter(line[0])) + { + CurrentMap = line.Split(" ")[0]; + continue; + } + Converters.Add(new Converter + { + Name = CurrentMap, + Start = Convert.ToInt64(line.Split(" ")[1]), + End = Convert.ToInt64(line.Split(" ")[0]), + Count = Convert.ToInt64(line.Split(" ")[2]) + }); + } + + Console.WriteLine($@" +Day5 Solution +Part1 Result: {Part1()} + +============================="); + } + + private static long Part1() + { + List seeds = File.ReadAllLines("inputs/Year2023/day5.txt")[0].Split(": ")[1].Split(" ").Select(long.Parse).ToList(); + List locations = new List(seeds); + + for (int i = 0; i < seeds.Count(); i++) + { + locations[i] = seeds[i]; + foreach (var type in MapTypes) + { + Converter? conv = Converters.Where(c => c.Name == type).Where(c => (c.Start <= locations[i] && c.Start + c.Count > locations[i])).FirstOrDefault(); + if (conv == null) continue; + locations[i] = conv.End + locations[i] - conv.Start; + } + } + + return locations.Min(); + } +} diff --git a/Year2023/Loader2023.cs b/Year2023/Loader2023.cs index 402d7c1..0fd50f8 100644 --- a/Year2023/Loader2023.cs +++ b/Year2023/Loader2023.cs @@ -13,6 +13,7 @@ public static class Loader2023 Day1.Run(); Day2.Run(); Day4.Run(); + Day5.Run(); } }