[2022] Day5 added
This commit is contained in:
parent
6f2f699ea5
commit
bdd031d54f
|
@ -29,4 +29,51 @@ public static class Extensions
|
|||
foreach (List<T> row in list) result.AddRange(row.Where((t, i) => i == column));
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reverses a given list
|
||||
/// </summary>
|
||||
/// <param name="list">The list to reverse</param>
|
||||
/// <returns>The reversed list</returns>
|
||||
public static List<T> Reversed<T>(this List<T> list)
|
||||
{
|
||||
List<T> reversed = new List<T>();
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
reversed.Add(list[list.Count-1-i]);
|
||||
return reversed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clones a list of elements
|
||||
/// </summary>
|
||||
/// <param name="original">The original list</param>
|
||||
/// <returns>A copy of the original list</returns>
|
||||
public static List<T> Clone<T>(this List<T> original)
|
||||
{
|
||||
List<T> ret = new List<T>(original.Count);
|
||||
foreach (T element in original)
|
||||
ret.Add(element);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static Dictionary<TKey, TValue> Clone<TKey, TValue>
|
||||
(this Dictionary<TKey, TValue> original)
|
||||
where TValue : ICloneable
|
||||
where TKey : notnull
|
||||
{
|
||||
Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>(original.Count, original.Comparer);
|
||||
foreach (KeyValuePair<TKey, TValue> entry in original)
|
||||
ret.Add(entry.Key, (TValue) entry.Value.Clone());
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static Dictionary<TKey, List<TValue>> Clone<TKey, TValue>
|
||||
(this Dictionary<TKey, List<TValue>> original)
|
||||
where TKey : notnull
|
||||
{
|
||||
Dictionary<TKey, List<TValue>> ret = new Dictionary<TKey, List<TValue>>(original.Count, original.Comparer);
|
||||
foreach (KeyValuePair<TKey, List<TValue>> entry in original)
|
||||
ret.Add(entry.Key, entry.Value.Clone());
|
||||
return ret;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
using AdventOfCode.Utils;
|
||||
|
||||
namespace AdventOfCode.Year2022;
|
||||
|
||||
public class Day5
|
||||
{
|
||||
private static readonly Dictionary<int, List<char>> 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<int, List<char>> crates = Crates.Clone();
|
||||
foreach ((int, int, int) move in Moves)
|
||||
{
|
||||
List<char> 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<char> 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
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue