about summary refs log tree commit diff
path: root/Day6.cs
blob: de59f9967ab09b1843feb643f33dc9a7b9c328bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections.Generic;
using System.Linq;

namespace aoc2019
{
    internal class Day6 : Day
    {
        public override int DayNumber => 6;

        private readonly Dictionary<string, string> input;
        public Day6()
        {
            input = Input.ToDictionary(i => i.Split(')')[1], i => i.Split(')')[0]);
        }

        private List<string> GetParents(string obj)
        {
            var res = new List<string>();
            for (var curr = obj; curr != "COM"; curr = input[curr])
                res.Add(curr);
            res.Add("COM");
            return res;
        }

        public override string Part1()
        {
            return $"{input.Keys.Sum(o => GetParents(o).Count - 1)}";
        }

        public override string Part2()
        {
            List<string> you = GetParents("YOU");
            List<string> san = GetParents("SAN");
            int common = 1;
            for (; you[^common] == san[^common]; common++);
            return $"{you.Count + san.Count - common * 2}";
        }
    }
}