From fba5683ec5cacbff1363d0d8d14e766b75b9acf7 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Wed, 7 Jun 2023 22:56:41 +0100 Subject: [PATCH] Initial new project commit --- AdventOfCode.sln | 16 +++++ AdventOfCode/AdventOfCode.csproj | 10 ++++ AdventOfCode/Program.cs | 13 ++++ AdventOfCode/Year2022/Day1.cs | 49 +++++++++++++++ AdventOfCode/Year2022/Day3.cs | 89 +++++++++++++++++++++++++++ AdventOfCode/Year2022/Day7.cs | 100 +++++++++++++++++++++++++++++++ 6 files changed, 277 insertions(+) create mode 100644 AdventOfCode.sln create mode 100644 AdventOfCode/AdventOfCode.csproj create mode 100644 AdventOfCode/Program.cs create mode 100644 AdventOfCode/Year2022/Day1.cs create mode 100644 AdventOfCode/Year2022/Day3.cs create mode 100644 AdventOfCode/Year2022/Day7.cs diff --git a/AdventOfCode.sln b/AdventOfCode.sln new file mode 100644 index 0000000..3062a87 --- /dev/null +++ b/AdventOfCode.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode", "AdventOfCode\AdventOfCode.csproj", "{D9E24F29-20E8-4271-89A3-4940A5A044D7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D9E24F29-20E8-4271-89A3-4940A5A044D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9E24F29-20E8-4271-89A3-4940A5A044D7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D9E24F29-20E8-4271-89A3-4940A5A044D7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9E24F29-20E8-4271-89A3-4940A5A044D7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/AdventOfCode/AdventOfCode.csproj b/AdventOfCode/AdventOfCode.csproj new file mode 100644 index 0000000..2b14c81 --- /dev/null +++ b/AdventOfCode/AdventOfCode.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/AdventOfCode/Program.cs b/AdventOfCode/Program.cs new file mode 100644 index 0000000..f9f1ea3 --- /dev/null +++ b/AdventOfCode/Program.cs @@ -0,0 +1,13 @@ +using AdventOfCode.Year2022; + +namespace AdventOfCode; + +class Program +{ + public static void Main(string[] args) + { + new Day1(); + new Day3(); + new Day7(); + } +} \ No newline at end of file diff --git a/AdventOfCode/Year2022/Day1.cs b/AdventOfCode/Year2022/Day1.cs new file mode 100644 index 0000000..799a904 --- /dev/null +++ b/AdventOfCode/Year2022/Day1.cs @@ -0,0 +1,49 @@ +namespace AdventOfCode.Year2022; + +public class Day1 +{ + private static List _caloriesPerElf = new(); + + public Day1() + { + Console.WriteLine("\nDay1 Solution\n"); + LoadCaloriesPerElf(); + Console.WriteLine($"Part1\nResult: {Part1()}\n"); + Console.WriteLine($"Part2\nResult: {Part2()}\n"); + Console.WriteLine("=============================\n"); + } + + private static int Part1() + { + return _caloriesPerElf.Max(); + } + + private static int Part2() + { + List top3 = new List(); + for (int i = 0; i < 3; i++) + { + top3.Add(_caloriesPerElf.Max()); + _caloriesPerElf.Remove(_caloriesPerElf.Max()); + } + + return top3.Sum(); + } + + private static void LoadCaloriesPerElf() + { + string[] calories = File.ReadAllLines("inputs/day1.txt"); + int[] caloriesPerDay = new int[calories.Length]; + var index = 0; + foreach (string calorie in calories) + { + if (calorie == "") + index++; + else + caloriesPerDay[index] += Convert.ToInt32(calorie); + } + + int elfCount = caloriesPerDay.ToList().IndexOf(0); + _caloriesPerElf = caloriesPerDay.ToList().GetRange(0, elfCount); + } +} \ No newline at end of file diff --git a/AdventOfCode/Year2022/Day3.cs b/AdventOfCode/Year2022/Day3.cs new file mode 100644 index 0000000..7b44d73 --- /dev/null +++ b/AdventOfCode/Year2022/Day3.cs @@ -0,0 +1,89 @@ +namespace AdventOfCode.Year2022; + +public class Day3 +{ + private static readonly string[] backpacks = File.ReadAllLines("inputs/day3.txt"); + + public Day3() + { + Console.WriteLine("Day3 Solution\n"); + Console.WriteLine($"Part1\nResult: {Part1()}\n"); + Console.WriteLine($"Part2\nResult: {Part2()}\n"); + Console.WriteLine("=============================\n"); + } + + private static int Part1() + { + int sum = 0; + + List duplicates = new List(); + + foreach (string backpack in backpacks) + { + int compartmentSize = backpack.Length / 2; + List itemsInCompartment1 = new List(); + List itemsInCompartment2 = new List(); + + for (int i = 0; i < compartmentSize; i++) + itemsInCompartment1.Add(backpack[i]); + for (int i = compartmentSize; i < compartmentSize * 2; i++) + itemsInCompartment2.Add(backpack[i]); + + char duplicatedItem = itemsInCompartment1.Intersect(itemsInCompartment2).FirstOrDefault(); + duplicates.Add(duplicatedItem); + } + + foreach (char duplicate in duplicates) + { + if (Char.IsUpper(duplicate)) + sum += Convert.ToInt16(duplicate) - 38; + else + sum += Convert.ToInt16(duplicate) - 96; + } + + return sum; + } + + private static int Part2() + { + int sum = 0; + List> groups = new List>(); + + for (int i = 0; i < backpacks.Length; i+=3) + { + List group = new List(); + + for (int x = 0; x < 3; x++) + { + try + { + group.Add(backpacks[i + x]); + } + catch + { + break; + } + } + + if (group.All(x => x != "")) + groups.Add(group); + } + + List duplicates = new List(); + foreach (List group in groups) + { + string[] groupArray = group.ToArray(); + duplicates.Add(groupArray[0].Intersect(groupArray[1].Intersect(groupArray[2])).FirstOrDefault()); + } + + foreach (char duplicate in duplicates) + { + if (Char.IsUpper(duplicate)) + sum += Convert.ToInt16(duplicate) - 38; + else + sum += Convert.ToInt16(duplicate) - 96; + } + + return sum; + } +} \ No newline at end of file diff --git a/AdventOfCode/Year2022/Day7.cs b/AdventOfCode/Year2022/Day7.cs new file mode 100644 index 0000000..d157186 --- /dev/null +++ b/AdventOfCode/Year2022/Day7.cs @@ -0,0 +1,100 @@ +namespace AdventOfCode.Year2022; + +public class Day7 +{ + private static Dictionary> tree = new(); + + public Day7() + { + Console.WriteLine("Day7 Solution\n"); + LoadTree(); + Console.WriteLine($"Part1\nResult: {Part1()}\n"); + Console.WriteLine($"Part2\nResult: {Part2()}\n"); + Console.WriteLine("=============================\n"); + } + + private static int Part1() + { + int sum = 0; + foreach (string path in tree.Keys) + { + int size = CalculateDirSize(path); + if (size <= 100000) + sum += size; + } + + return sum; + } + + private static int Part2() + { + int neededSpace = CalculateDirSize("/") - 40000000; + if (neededSpace <= 0) + { + return 0; + } + + List bigEnoughDir = new List(); + foreach (string path in tree.Keys) + { + int size = CalculateDirSize(path); + if (size > neededSpace) + bigEnoughDir.Add(size); + } + + bigEnoughDir.Sort(); + return bigEnoughDir[0]; + } + + private static int CalculateDirSize(string path) + { + int size = 0; + List dirContent = tree[path]; + foreach (string content in dirContent) + { + string[] properties = content.Split(" "); + if (properties[0] == "dir") + size += CalculateDirSize(path + properties[1] + "/"); + else + size += Convert.ToInt32(properties[0]); + } + + return size; + } + + private static void LoadTree() + { + string currentPath = ""; + + IEnumerable input = File.ReadLines("inputs/day7.txt"); + foreach (string line in input) + { + if (line.StartsWith("$")) + { + string[] cmdLine = line.Substring(2).Split(" "); + if (cmdLine[0] == "cd") + { + string dir = cmdLine[1]; + if (dir == "/") + currentPath = "/"; + else if (dir == "..") + { + if (currentPath == "/") continue; + int slashIndex = currentPath + .Substring(0, currentPath.Length - 1) + .LastIndexOf('/'); + currentPath = currentPath.Substring(0, slashIndex + 1); + } + else + currentPath += dir + "/"; + } + } + else + { + if (!tree.ContainsKey(currentPath)) + tree.Add(currentPath, new List()); + tree[currentPath].Add(line); + } + } + } +} \ No newline at end of file