From 13b353a2da6bc714b1d2bc0b189006d626b756e0 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Thu, 8 Jun 2023 00:12:01 +0100 Subject: [PATCH] [2022] Refactoring --- AdventOfCode/Year2022/Day1.cs | 13 ++++++------- AdventOfCode/Year2022/Day2.cs | 13 +++++++------ AdventOfCode/Year2022/Day3.cs | 8 ++++---- AdventOfCode/Year2022/Day4.cs | 8 ++++---- AdventOfCode/Year2022/Day6.cs | 6 +++--- AdventOfCode/Year2022/Day7.cs | 14 ++++++++------ 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/AdventOfCode/Year2022/Day1.cs b/AdventOfCode/Year2022/Day1.cs index 61b97d4..40ae709 100644 --- a/AdventOfCode/Year2022/Day1.cs +++ b/AdventOfCode/Year2022/Day1.cs @@ -2,12 +2,11 @@ namespace AdventOfCode.Year2022; public class Day1 { - private static List _caloriesPerElf = new(); + private static readonly List CaloriesPerElf = GetCaloriesPerElf(); public Day1() { Console.WriteLine("\nDay1 Solution"); - LoadCaloriesPerElf(); Console.WriteLine($"Part1 Result: {Part1()}"); Console.WriteLine($"Part2 Result: {Part2()}"); Console.WriteLine("\n=============================\n"); @@ -15,7 +14,7 @@ public class Day1 private static int Part1() { - return _caloriesPerElf.Max(); + return CaloriesPerElf.Max(); } private static int Part2() @@ -23,14 +22,14 @@ public class Day1 List top3 = new List(); for (int i = 0; i < 3; i++) { - top3.Add(_caloriesPerElf.Max()); - _caloriesPerElf.Remove(_caloriesPerElf.Max()); + top3.Add(CaloriesPerElf.Max()); + CaloriesPerElf.Remove(CaloriesPerElf.Max()); } return top3.Sum(); } - private static void LoadCaloriesPerElf() + private static List GetCaloriesPerElf() { string[] calories = File.ReadAllLines("inputs/day1.txt"); int[] caloriesPerDay = new int[calories.Length]; @@ -44,6 +43,6 @@ public class Day1 } int elfCount = caloriesPerDay.ToList().IndexOf(0); - _caloriesPerElf = caloriesPerDay.ToList().GetRange(0, elfCount); + return caloriesPerDay.ToList().GetRange(0, elfCount); } } \ No newline at end of file diff --git a/AdventOfCode/Year2022/Day2.cs b/AdventOfCode/Year2022/Day2.cs index 66ef8e7..830ce4e 100644 --- a/AdventOfCode/Year2022/Day2.cs +++ b/AdventOfCode/Year2022/Day2.cs @@ -2,12 +2,11 @@ namespace AdventOfCode.Year2022; public class Day2 { - private static readonly List _matches = new(); + private static readonly List Matches = LoadMatches(); public Day2() { Console.WriteLine("Day2 Solution"); - LoadMatches(); Console.WriteLine($"Part1 Result: {Part1()}"); Console.WriteLine($"Part2 Result: {Part2()}"); Console.WriteLine("\n=============================\n"); @@ -16,7 +15,7 @@ public class Day2 private static int Part1() { int score = 0; - foreach (string[] match in _matches) + foreach (string[] match in Matches) { if (match[0] == "A") { @@ -53,7 +52,7 @@ public class Day2 private static int Part2() { int score = 0; - foreach (string[] match in _matches) + foreach (string[] match in Matches) { if (match[0] == "A") { @@ -87,9 +86,11 @@ public class Day2 return score; } - private static void LoadMatches() + private static List LoadMatches() { + List matches = new List(); foreach (string line in File.ReadAllLines("inputs/day2.txt")) - _matches.Add(line.Split(" ")); + matches.Add(line.Split(" ")); + return matches; } } \ No newline at end of file diff --git a/AdventOfCode/Year2022/Day3.cs b/AdventOfCode/Year2022/Day3.cs index 47b5b41..b0681ef 100644 --- a/AdventOfCode/Year2022/Day3.cs +++ b/AdventOfCode/Year2022/Day3.cs @@ -2,7 +2,7 @@ namespace AdventOfCode.Year2022; public class Day3 { - private static readonly string[] backpacks = File.ReadAllLines("inputs/day3.txt"); + private static readonly string[] Backpacks = File.ReadAllLines("inputs/day3.txt"); public Day3() { @@ -18,7 +18,7 @@ public class Day3 List duplicates = new List(); - foreach (string backpack in backpacks) + foreach (string backpack in Backpacks) { int compartmentSize = backpack.Length / 2; List itemsInCompartment1 = new List(); @@ -49,7 +49,7 @@ public class Day3 int sum = 0; List> groups = new List>(); - for (int i = 0; i < backpacks.Length; i+=3) + for (int i = 0; i < Backpacks.Length; i+=3) { List group = new List(); @@ -57,7 +57,7 @@ public class Day3 { try { - group.Add(backpacks[i + x]); + group.Add(Backpacks[i + x]); } catch { diff --git a/AdventOfCode/Year2022/Day4.cs b/AdventOfCode/Year2022/Day4.cs index 8a956e7..56858c2 100644 --- a/AdventOfCode/Year2022/Day4.cs +++ b/AdventOfCode/Year2022/Day4.cs @@ -22,21 +22,21 @@ public class Day4 Console.WriteLine("\n=============================\n"); } - public static bool IsContained(string line) + private static bool IsContained(string line) { int[][] limits = GetLimits(line); return (limits[0][0] >= limits[1][0] && limits[0][1] <= limits[1][1]) || (limits[1][0] >= limits[0][0] && limits[1][1] <= limits[0][1]); } - - public static bool IsIntersected(string line) + + private static bool IsIntersected(string line) { int[][] limits = GetLimits(line); return (limits[0][1] >= limits[1][0] && limits[0][0] <= limits[1][1]) || (limits[1][1] >= limits[0][0] && limits[1][0] <= limits[0][1]); } - public static int[][] GetLimits(string line) + private static int[][] GetLimits(string line) { string[] pair = line.Split(","); string[] pair1 = pair[0].Split("-"); diff --git a/AdventOfCode/Year2022/Day6.cs b/AdventOfCode/Year2022/Day6.cs index 99b02dd..997b663 100644 --- a/AdventOfCode/Year2022/Day6.cs +++ b/AdventOfCode/Year2022/Day6.cs @@ -2,7 +2,7 @@ namespace AdventOfCode.Year2022; public class Day6 { - private static readonly string _input = File.ReadAllText("inputs/day6.txt"); + private static readonly string Input = File.ReadAllText("inputs/day6.txt"); public Day6() { @@ -14,8 +14,8 @@ public class Day6 private static int? GetValidMarkerIndex(int size) { - for (int i = 0; i < _input.Length-size; i++) - if (ValidateMarker(_input.Substring(i, size))) + for (int i = 0; i < Input.Length-size; i++) + if (ValidateMarker(Input.Substring(i, size))) return i + size; return null; } diff --git a/AdventOfCode/Year2022/Day7.cs b/AdventOfCode/Year2022/Day7.cs index f03fcd3..cfbd5c5 100644 --- a/AdventOfCode/Year2022/Day7.cs +++ b/AdventOfCode/Year2022/Day7.cs @@ -2,12 +2,11 @@ namespace AdventOfCode.Year2022; public class Day7 { - private static Dictionary> tree = new(); + private static readonly Dictionary> Tree = GetTree(); public Day7() { Console.WriteLine("Day7 Solution"); - LoadTree(); Console.WriteLine($"Part1 Result: {Part1()}"); Console.WriteLine($"Part2 Result: {Part2()}"); Console.WriteLine("\n=============================\n"); @@ -16,7 +15,7 @@ public class Day7 private static int Part1() { int sum = 0; - foreach (string path in tree.Keys) + foreach (string path in Tree.Keys) { int size = CalculateDirSize(path); if (size <= 100000) @@ -35,7 +34,7 @@ public class Day7 } List bigEnoughDir = new List(); - foreach (string path in tree.Keys) + foreach (string path in Tree.Keys) { int size = CalculateDirSize(path); if (size > neededSpace) @@ -49,7 +48,7 @@ public class Day7 private static int CalculateDirSize(string path) { int size = 0; - List dirContent = tree[path]; + List dirContent = Tree[path]; foreach (string content in dirContent) { string[] properties = content.Split(" "); @@ -62,8 +61,9 @@ public class Day7 return size; } - private static void LoadTree() + private static Dictionary> GetTree() { + Dictionary> tree = new Dictionary>(); string currentPath = ""; IEnumerable input = File.ReadLines("inputs/day7.txt"); @@ -96,5 +96,7 @@ public class Day7 tree[currentPath].Add(line); } } + + return tree; } } \ No newline at end of file