diff --git a/AdventOfCode/Program.cs b/AdventOfCode/Program.cs index f9f1ea3..43d55d6 100644 --- a/AdventOfCode/Program.cs +++ b/AdventOfCode/Program.cs @@ -7,6 +7,7 @@ class Program public static void Main(string[] args) { new Day1(); + new Day2(); new Day3(); new Day7(); } diff --git a/AdventOfCode/Year2022/Day2.cs b/AdventOfCode/Year2022/Day2.cs new file mode 100644 index 0000000..f98cca1 --- /dev/null +++ b/AdventOfCode/Year2022/Day2.cs @@ -0,0 +1,95 @@ +namespace AdventOfCode.Year2022; + +public class Day2 +{ + private static List matches = new(); + + public Day2() + { + Console.WriteLine("Day2 Solution\n"); + LoadMatches(); + Console.WriteLine($"Part1\nResult: {Part1()}\n"); + Console.WriteLine($"Part2\nResult: {Part2()}\n"); + Console.WriteLine("=============================\n"); + } + + private static int Part1() + { + int score = 0; + foreach (string[] match in matches) + { + if (match[0] == "A") + { + if (match[1] == "X") + score += 1 + 3; + if (match[1] == "Y") + score += 2 + 6; + if (match[1] == "Z") + score += 3; + } + if (match[0] == "B") + { + if (match[1] == "X") + score += 1; + if (match[1] == "Y") + score += 2 + 3; + if (match[1] == "Z") + score += 3 + 6; + } + if (match[0] == "C") + { + if (match[1] == "X") + score += 1 + 6; + if (match[1] == "Y") + score += 2; + if (match[1] == "Z") + score += 3 + 3; + } + } + + return score; + } + + private static int Part2() + { + int score = 0; + foreach (string[] match in matches) + { + if (match[0] == "A") + { + if (match[1] == "X") + score += 3; + if (match[1] == "Y") + score += 1 + 3; + if (match[1] == "Z") + score += 2 + 6; + } + if (match[0] == "B") + { + if (match[1] == "X") + score += 1; + if (match[1] == "Y") + score += 2 + 3; + if (match[1] == "Z") + score += 3 + 6; + } + if (match[0] == "C") + { + if (match[1] == "X") + score += 2; + if (match[1] == "Y") + score += 3 + 3; + if (match[1] == "Z") + score += 1 + 6; + } + } + + return score; + } + + private static void LoadMatches() + { + foreach (string line in File.ReadAllLines("inputs/day2.txt")) + matches.Add(line.Split(" ")); + } +} \ No newline at end of file