AdventOfCode/Year2022/Day2.cs

102 lines
2.4 KiB
C#
Raw Permalink Normal View History

2023-06-07 22:13:21 +00:00
namespace AdventOfCode.Year2022;
public class Day2
{
2023-06-07 23:12:01 +00:00
private static readonly List<string[]> Matches = LoadMatches();
2023-06-07 22:13:21 +00:00
public Day2()
{
2023-06-24 21:26:39 +00:00
Console.WriteLine($@"
Day2 Solution
Part1 Result: {Part1()}
Part2 Result: {Part2()}
=============================");
2023-06-07 22:13:21 +00:00
}
private static int Part1()
{
2023-06-24 21:26:39 +00:00
var score = 0;
foreach (var match in Matches)
2023-06-07 22:13:21 +00:00
{
if (match[0] == "A")
{
if (match[1] == "X")
score += 1 + 3;
if (match[1] == "Y")
score += 2 + 6;
if (match[1] == "Z")
score += 3;
}
2023-06-24 21:26:39 +00:00
2023-06-07 22:13:21 +00:00
if (match[0] == "B")
{
if (match[1] == "X")
score += 1;
if (match[1] == "Y")
score += 2 + 3;
if (match[1] == "Z")
score += 3 + 6;
}
2023-06-24 21:26:39 +00:00
2023-06-07 22:13:21 +00:00
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()
{
2023-06-24 21:26:39 +00:00
var score = 0;
foreach (var match in Matches)
2023-06-07 22:13:21 +00:00
{
if (match[0] == "A")
{
if (match[1] == "X")
score += 3;
if (match[1] == "Y")
score += 1 + 3;
if (match[1] == "Z")
score += 2 + 6;
}
2023-06-24 21:26:39 +00:00
2023-06-07 22:13:21 +00:00
if (match[0] == "B")
{
if (match[1] == "X")
score += 1;
if (match[1] == "Y")
score += 2 + 3;
if (match[1] == "Z")
score += 3 + 6;
}
2023-06-24 21:26:39 +00:00
2023-06-07 22:13:21 +00:00
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;
}
2023-06-07 23:12:01 +00:00
private static List<string[]> LoadMatches()
2023-06-07 22:13:21 +00:00
{
2023-06-24 21:26:39 +00:00
var matches = new List<string[]>();
foreach (var line in File.ReadAllLines("inputs/day2.txt"))
2023-06-07 23:12:01 +00:00
matches.Add(line.Split(" "));
return matches;
2023-06-07 22:13:21 +00:00
}
}