79 lines
2.2 KiB
C#
79 lines
2.2 KiB
C#
|
namespace AdventOfCode2022.Handlers
|
||
|
{
|
||
|
public class Day04
|
||
|
{
|
||
|
string[] pairs = new string[] {};
|
||
|
public Day04()
|
||
|
{
|
||
|
pairs = File.ReadAllText(@"./days/04/input").Trim().Split("\n");
|
||
|
}
|
||
|
|
||
|
public void PartA()
|
||
|
{
|
||
|
var count = 0;
|
||
|
foreach (var pair in pairs)
|
||
|
{
|
||
|
var split = pair.Split(",");
|
||
|
var first = split[0];
|
||
|
var second = split[1];
|
||
|
|
||
|
var firstMin = int.Parse(first.Split("-")[0]);
|
||
|
var firstMax = int.Parse(first.Split("-")[1]);
|
||
|
|
||
|
var secondMin = int.Parse(second.Split("-")[0]);
|
||
|
var secondMax = int.Parse(second.Split("-")[1]);
|
||
|
|
||
|
if (firstMin <= secondMin && firstMax >= secondMax)
|
||
|
{
|
||
|
count++;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (secondMin <= firstMin && secondMax >= firstMax)
|
||
|
{
|
||
|
count++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Console.WriteLine($"Part A: {count}");
|
||
|
}
|
||
|
|
||
|
public void PartB()
|
||
|
{
|
||
|
var count = 0;
|
||
|
foreach (var pair in pairs)
|
||
|
{
|
||
|
var split = pair.Split(",");
|
||
|
var first = split[0];
|
||
|
var second = split[1];
|
||
|
|
||
|
var firstMin = int.Parse(first.Split("-")[0]);
|
||
|
var firstMax = int.Parse(first.Split("-")[1]);
|
||
|
|
||
|
var secondMin = int.Parse(second.Split("-")[0]);
|
||
|
var secondMax = int.Parse(second.Split("-")[1]);
|
||
|
|
||
|
for (var i = firstMin; i <= firstMax; i++)
|
||
|
{
|
||
|
var shouldLoop = true;
|
||
|
for (var j = secondMin; j <= secondMax; j++)
|
||
|
{
|
||
|
if (i == j)
|
||
|
{
|
||
|
count++;
|
||
|
shouldLoop = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!shouldLoop)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Console.WriteLine($"Part B: {count}");
|
||
|
}
|
||
|
}
|
||
|
}
|