122 lines
3.2 KiB
C#
122 lines
3.2 KiB
C#
//const string fileName = @"example-input.txt";
|
|
using System.Collections.Generic;
|
|
using System.Text.Unicode;
|
|
|
|
const string puzzleFileName = @"puzzle-input.txt";
|
|
const string exampleFileName = @"example-input.txt";
|
|
|
|
var exampleData = new LoadedData(exampleFileName);
|
|
var puzzleData = new LoadedData(puzzleFileName);
|
|
|
|
Console.WriteLine(String.Format("Part01, ExampleFullyContains: {0}, PuzzleFullyContains {1}", exampleData.GetFullyContainsCount(), puzzleData.GetFullyContainsCount()));
|
|
Console.WriteLine(String.Format("Part02, ExampleOverlaps: {0}, PuzzleOverlaps {1}", exampleData.GetOverlapsCount(), puzzleData.GetOverlapsCount()));
|
|
|
|
class LoadedData
|
|
{
|
|
public class Assignment
|
|
{
|
|
public struct Range
|
|
{
|
|
public Range() { }
|
|
public Range(int l, int r)
|
|
{
|
|
Low = l; High = r;
|
|
}
|
|
|
|
public int Low { get; set; }
|
|
public int High { get; set; }
|
|
}
|
|
|
|
public Range Left;
|
|
public Range Right;
|
|
|
|
public bool FullyContains
|
|
{
|
|
get
|
|
{
|
|
if ( Left.Low >= Right.Low && Left.High <= Right.High
|
|
|| Right.Low >= Left.Low && Right.High <= Left.High)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool Overlaps
|
|
{
|
|
get
|
|
{
|
|
if (Left.High < Right.Low || Right.High < Left.Low)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<Assignment> Assignments = new List<Assignment>();
|
|
|
|
public int GetFullyContainsCount()
|
|
{
|
|
int count = 0;
|
|
|
|
foreach (var a in Assignments)
|
|
{
|
|
if (a.FullyContains) count++;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
public int GetOverlapsCount()
|
|
{
|
|
int count = 0;
|
|
|
|
foreach (var a in Assignments)
|
|
{
|
|
if (a.Overlaps) count++;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
public LoadedData(string fileName)
|
|
{
|
|
using (StreamReader reader = File.OpenText(fileName))
|
|
{
|
|
while (!reader.EndOfStream)
|
|
{
|
|
string? line = reader.ReadLine();
|
|
|
|
if (line == null)
|
|
{
|
|
throw new InvalidDataException();
|
|
}
|
|
|
|
var ranges = line.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
|
|
if (ranges.Length != 2)
|
|
{
|
|
throw new InvalidDataException();
|
|
}
|
|
|
|
var rangeLeft = ranges[0].Split('-');
|
|
var rangeRight = ranges[1].Split('-');
|
|
|
|
Assignment a = new Assignment();
|
|
a.Left.Low = Int32.Parse(rangeLeft[0]);
|
|
a.Left.High = Int32.Parse(rangeLeft[1]);
|
|
a.Right.Low = Int32.Parse(rangeRight[0]);
|
|
a.Right.High = Int32.Parse(rangeRight[1]);
|
|
|
|
Assignments.Add(a);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|