[2022] Refactoring
This commit is contained in:
parent
69b5115122
commit
13b353a2da
|
@ -2,12 +2,11 @@ namespace AdventOfCode.Year2022;
|
|||
|
||||
public class Day1
|
||||
{
|
||||
private static List<int> _caloriesPerElf = new();
|
||||
private static readonly List<int> 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<int> top3 = new List<int>();
|
||||
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<int> 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);
|
||||
}
|
||||
}
|
|
@ -2,12 +2,11 @@ namespace AdventOfCode.Year2022;
|
|||
|
||||
public class Day2
|
||||
{
|
||||
private static readonly List<string[]> _matches = new();
|
||||
private static readonly List<string[]> 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<string[]> LoadMatches()
|
||||
{
|
||||
List<string[]> matches = new List<string[]>();
|
||||
foreach (string line in File.ReadAllLines("inputs/day2.txt"))
|
||||
_matches.Add(line.Split(" "));
|
||||
matches.Add(line.Split(" "));
|
||||
return matches;
|
||||
}
|
||||
}
|
|
@ -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<char> duplicates = new List<char>();
|
||||
|
||||
foreach (string backpack in backpacks)
|
||||
foreach (string backpack in Backpacks)
|
||||
{
|
||||
int compartmentSize = backpack.Length / 2;
|
||||
List<char> itemsInCompartment1 = new List<char>();
|
||||
|
@ -49,7 +49,7 @@ public class Day3
|
|||
int sum = 0;
|
||||
List<List<string>> groups = new List<List<string>>();
|
||||
|
||||
for (int i = 0; i < backpacks.Length; i+=3)
|
||||
for (int i = 0; i < Backpacks.Length; i+=3)
|
||||
{
|
||||
List<string> group = new List<string>();
|
||||
|
||||
|
@ -57,7 +57,7 @@ public class Day3
|
|||
{
|
||||
try
|
||||
{
|
||||
group.Add(backpacks[i + x]);
|
||||
group.Add(Backpacks[i + x]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
|
|
@ -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("-");
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -2,12 +2,11 @@ namespace AdventOfCode.Year2022;
|
|||
|
||||
public class Day7
|
||||
{
|
||||
private static Dictionary<string, List<string>> tree = new();
|
||||
private static readonly Dictionary<string, List<string>> 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<int> bigEnoughDir = new List<int>();
|
||||
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<string> dirContent = tree[path];
|
||||
List<string> 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<string, List<string>> GetTree()
|
||||
{
|
||||
Dictionary<string, List<string>> tree = new Dictionary<string, List<string>>();
|
||||
string currentPath = "";
|
||||
|
||||
IEnumerable<string> input = File.ReadLines("inputs/day7.txt");
|
||||
|
@ -96,5 +96,7 @@ public class Day7
|
|||
tree[currentPath].Add(line);
|
||||
}
|
||||
}
|
||||
|
||||
return tree;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue