This commit is contained in:
2022-12-04 13:56:54 -05:00
parent c595abfa3f
commit e9ccc06320
3 changed files with 1075 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
using AdventOfCode.Runner;
using AdventOfCode.Runner.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode.Problems.AOC2022.Day4;
[ProblemInfo(2022, 4, "Camp Cleanup")]
internal class CampCleanup : Problem<int, int>
{
private List<(Range a, Range b)> _pairs = new(500);
public override void LoadInput()
{
var lines = ReadInputLines("input.txt");
foreach (var line in lines)
{
var (a, b) = line.Split(',')
.Select(range =>
range.Split('-')
.Select(v => int.Parse(v))
.Chunk(2)
.Select(r => new Range(r.First(), r.Last()))
.First()
).Chunk(2).Select(pair => (pair.First(), pair.Last())).First();
_pairs.Add((a, b));
}
}
public override void CalculatePart1()
{
var total = 0;
foreach (var (a, b) in _pairs)
{
if (a.Contains(b) || b.Contains(a))
total++;
}
Part1 = total;
}
public override void CalculatePart2()
{
foreach (var (a, b) in _pairs)
{
if (a.OverlapsWith(b))
Part2++;
}
}
record Range(int A, int B)
{
public bool Contains(Range other)
{
return (A <= other.A && B >= other.B);
}
public bool OverlapsWith(Range other)
{
return (B >= other.A && A <= other.A) || (A <= other.B && B >= other.B) || (A >= other.A && B <= other.B);
}
public static implicit operator Range((int a, int b) value) => new Range(value.a, value.b);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8