From bdd031d54ff7cb9fee7ced386cdae3cf4f334af4 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Thu, 8 Jun 2023 18:29:44 +0100 Subject: [PATCH] [2022] Day5 added --- Utils/Extensions.cs | 47 +++++++++++++++++++++++ Year2022/Day5.cs | 85 ++++++++++++++++++++++++++++++++++++++++++ Year2022/Day8.cs | 3 -- Year2022/Loader2022.cs | 1 + 4 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 Year2022/Day5.cs diff --git a/Utils/Extensions.cs b/Utils/Extensions.cs index 222cc07..3f8dfbb 100644 --- a/Utils/Extensions.cs +++ b/Utils/Extensions.cs @@ -29,4 +29,51 @@ public static class Extensions foreach (List row in list) result.AddRange(row.Where((t, i) => i == column)); return result; } + + /// + /// Reverses a given list + /// + /// The list to reverse + /// The reversed list + public static List Reversed(this List list) + { + List reversed = new List(); + for (int i = 0; i < list.Count; i++) + reversed.Add(list[list.Count-1-i]); + return reversed; + } + + /// + /// Clones a list of elements + /// + /// The original list + /// A copy of the original list + public static List Clone(this List original) + { + List ret = new List(original.Count); + foreach (T element in original) + ret.Add(element); + return ret; + } + + public static Dictionary Clone + (this Dictionary original) + where TValue : ICloneable + where TKey : notnull + { + Dictionary ret = new Dictionary(original.Count, original.Comparer); + foreach (KeyValuePair entry in original) + ret.Add(entry.Key, (TValue) entry.Value.Clone()); + return ret; + } + + public static Dictionary> Clone + (this Dictionary> original) + where TKey : notnull + { + Dictionary> ret = new Dictionary>(original.Count, original.Comparer); + foreach (KeyValuePair> entry in original) + ret.Add(entry.Key, entry.Value.Clone()); + return ret; + } } \ No newline at end of file diff --git a/Year2022/Day5.cs b/Year2022/Day5.cs new file mode 100644 index 0000000..080550f --- /dev/null +++ b/Year2022/Day5.cs @@ -0,0 +1,85 @@ +using AdventOfCode.Utils; + +namespace AdventOfCode.Year2022; + +public class Day5 +{ + private static readonly Dictionary> Crates = new() + { + [1] = new(), + [2] = new(), + [3] = new(), + [4] = new(), + [5] = new(), + [6] = new(), + [7] = new(), + [8] = new(), + [9] = new() + }; + + private static readonly List<(int, int, int)> Moves = new(); + + public Day5() + { + Console.WriteLine("Day5 Solution"); + + string[] input = File.ReadAllText("inputs/day5.txt").Split("\n\n"); + LoadCrates(input[0]); + LoadMoves(input[1]); + + Console.WriteLine($"Part1 Result: {Parts(Part.Part1)}"); + Console.WriteLine($"Part2 Result: {Parts(Part.Part2)}"); + Console.WriteLine("\n=============================\n"); + } + + private static string Parts(Part part) + { + Dictionary> crates = Crates.Clone(); + foreach ((int, int, int) move in Moves) + { + List cratesMoved = crates[move.Item2].Sublist(0, move.Item1); + for (int i = 0; i < move.Item1; i++) + crates[move.Item2].Remove(crates[move.Item2][0]); + foreach (char crate in part == Part.Part1 ? cratesMoved : cratesMoved.Reversed()) + crates[move.Item3].Insert(0, crate); + } + + string result = ""; + foreach (List crateList in crates.Values) + result += crateList[0]; + return result; + } + + private static void LoadCrates(string crates) + { + string[] crate_lines = crates.Split("\n"); + foreach (string line in crate_lines) + { + int firstCrate = 0; + if (line[firstCrate] == '[') + Crates[1].Add(line[1]); + while (line.Substring(firstCrate+1).Contains('[')) + { + firstCrate = line.IndexOf('[', firstCrate + 1); + Crates[firstCrate / 4 + 1].Add(line[firstCrate + 1]); + } + } + } + + private static void LoadMoves(string moves) + { + string[] move_lines = moves.Split("\n"); + foreach (string line in move_lines) + { + string move = line.Substring(5); + string[] moved = move.Split(" from "); + string[] crates = moved[1].Split(" to "); + Moves.Add((Convert.ToInt32(moved[0]), Convert.ToInt32(crates[0]), Convert.ToInt32(crates[1]))); + } + } + + private enum Part + { + Part1, Part2 + } +} \ No newline at end of file diff --git a/Year2022/Day8.cs b/Year2022/Day8.cs index 7cd96c6..34d467b 100644 --- a/Year2022/Day8.cs +++ b/Year2022/Day8.cs @@ -19,15 +19,12 @@ public class Day8 int count = 0; for (int i = 0; i < TreeMap.Count; i++) for (int j = 0; j < TreeMap[i].Count; j++) - { if (i == 0 || i == TreeMap.Count-1 || j == 0 || j == TreeMap.Count-1 || TreeMap[i].Sublist(0, j).Max() < TreeMap[i][j] || TreeMap[i].Sublist(j + 1, TreeMap[i].Count).DefaultIfEmpty().Max() < TreeMap[i][j] || TreeMap.GetColumn(j).Sublist(0, i).Max() < TreeMap[i][j] || TreeMap.GetColumn(j).Sublist(i + 1, TreeMap[i].Count).DefaultIfEmpty().Max() < TreeMap[i][j]) count++; - } - return count; } diff --git a/Year2022/Loader2022.cs b/Year2022/Loader2022.cs index 110230c..3102c1f 100644 --- a/Year2022/Loader2022.cs +++ b/Year2022/Loader2022.cs @@ -8,6 +8,7 @@ public static class Loader2022 new Day2(); new Day3(); new Day4(); + new Day5(); new Day6(); new Day7(); // new Day8(); -- Commented because it's a slow solution