class Program { private static (Range elfOne, Range elfTwo)[] output; static void Main(string[] args) { Read(File.ReadAllLines(@"../../../Input.txt"), out output); Part1(); Part2(); } private static void Read(string[] input, out (Range elfOne, Range elfTwo)[] output) { output = input.Select(line => line.Split(',')) .Select(parts => (left: parts[0].Split('-'), right: parts[1].Split('-'))) .Select(parts => (elfOne: new Range(int.Parse(parts.left[0]), int.Parse(parts.left[1])), elfTwo: new Range(int.Parse(parts.right[0]), int.Parse(parts.right[1])))) .ToArray(); } public struct Range { public int start; public int end; public Range(int start, int end) { this.start = start; this.end = end; } public bool Contains(Range range) { return this.start <= range.start && this.end >= range.end; } public bool Overlaps(Range range) { return (range.start >= this.start && range.start <= this.end) || (range.end >= this.start && range.end <= this.start); } } private static void Part1() { var solution = output.Where(ranges => ranges.elfOne.Contains(ranges.elfTwo) || ranges.elfTwo.Contains(ranges.elfOne)).Count(); Console.WriteLine(solution); } private static void Part2() { var solution = output.Where(ranges => ranges.elfOne.Overlaps(ranges.elfTwo) || ranges.elfTwo.Overlaps(ranges.elfOne)).Count(); Console.WriteLine(solution); } }