Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.9 kB
3
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;

namespace Day42022
{
    class Range
    {
        public int Big { get; set; }
        public int Small { get; set; }
    }

    class Program
    {
        public static List<string> Input => System.IO.File.ReadAllLines(@"input.txt").ToList();
        static void Main(string[] args)
        {
            int Cover = 0;
            int Overlap = 0;

            foreach (var line in Input)
            {
                var divided = line.Split(',');

                var FirstRange = new Range()
                {
                    Big = int.Parse(divided[0].Split('-')[0]),
                    Small = int.Parse(divided[0].Split('-')[1])
                };
                var SecondRange = new Range()
                {
                    Big = int.Parse(divided[1].Split('-')[0]),
                    Small = int.Parse(divided[1].Split('-')[1])
                };

                if (Covered(FirstRange, SecondRange))
                {
                    Cover++;
                }
                if (Overlaps(FirstRange, SecondRange))
                {
                    Overlap++;
                }
            }
            Console.WriteLine(Cover);
            Console.WriteLine(Overlap);
            Console.ReadLine();
        }
        public static bool Covered(Range left, Range right)
        {
            if ((left.Big >= right.Big && left.Small <= right.Small) || (left.Big <= right.Big && left.Small >= right.Small))
            {
                return true;
            }
            return false;
        }
        public static bool Overlaps(Range left, Range right)
        {
            if (left.Big <= right.Small && right.Big <= left.Small)
            {
                return true;
            }
            return false;
        }
    }
}