2023-06-07 22:28:51 +00:00
|
|
|
namespace AdventOfCode.Year2022;
|
|
|
|
|
|
|
|
public class Day4
|
|
|
|
{
|
|
|
|
public Day4()
|
|
|
|
{
|
2023-06-07 22:59:16 +00:00
|
|
|
Console.WriteLine("Day4 Solution");
|
2023-06-07 22:28:51 +00:00
|
|
|
|
|
|
|
string[] lines = File.ReadAllLines("inputs/day4.txt");
|
|
|
|
int containedCount = 0;
|
|
|
|
int intersectedCount = 0;
|
|
|
|
foreach (string line in lines)
|
|
|
|
{
|
|
|
|
if (IsContained(line))
|
|
|
|
containedCount++;
|
|
|
|
if (IsIntersected(line))
|
|
|
|
intersectedCount++;
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:59:16 +00:00
|
|
|
Console.WriteLine($"Part1 Result: {containedCount}");
|
|
|
|
Console.WriteLine($"Part2 Result: {intersectedCount}");
|
|
|
|
Console.WriteLine("\n=============================\n");
|
2023-06-07 22:28:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 23:12:01 +00:00
|
|
|
private static bool IsContained(string line)
|
2023-06-07 22:28:51 +00:00
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
2023-06-07 23:12:01 +00:00
|
|
|
|
|
|
|
private static bool IsIntersected(string line)
|
2023-06-07 22:28:51 +00:00
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2023-06-07 23:12:01 +00:00
|
|
|
private static int[][] GetLimits(string line)
|
2023-06-07 22:28:51 +00:00
|
|
|
{
|
|
|
|
string[] pair = line.Split(",");
|
|
|
|
string[] pair1 = pair[0].Split("-");
|
|
|
|
string[] pair2 = pair[1].Split("-");
|
|
|
|
return new []
|
|
|
|
{
|
|
|
|
new [] {Convert.ToInt32(pair1[0]), Convert.ToInt32(pair1[1])},
|
|
|
|
new [] {Convert.ToInt32(pair2[0]), Convert.ToInt32(pair2[1])}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|