Day 4
This commit is contained in:
69
AdventOfCode/Problems/AOC2022/Day4/CampCleanup.cs
Normal file
69
AdventOfCode/Problems/AOC2022/Day4/CampCleanup.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
1000
AdventOfCode/Problems/AOC2022/Day4/input.txt
Normal file
1000
AdventOfCode/Problems/AOC2022/Day4/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
AdventOfCode/Problems/AOC2022/Day4/test.txt
Normal file
6
AdventOfCode/Problems/AOC2022/Day4/test.txt
Normal 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
|
||||||
Reference in New Issue
Block a user