aoc2019/Day6.cs

40 lines
1.0 KiB
C#
Raw Normal View History

2019-12-06 07:02:01 +00:00
using System.Collections.Generic;
using System.Linq;
namespace aoc2019
{
2020-12-02 04:50:35 +00:00
internal sealed class Day6 : Day
2019-12-06 07:02:01 +00:00
{
private readonly Dictionary<string, string> input;
2019-12-06 07:02:01 +00:00
public Day6()
{
input = Input.ToDictionary(i => i.Split(')')[1], i => i.Split(')')[0]);
}
public override int DayNumber => 6;
2019-12-06 07:02:01 +00:00
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;
}
2020-12-02 06:58:54 +00:00
protected override string Part1()
2019-12-06 07:02:01 +00:00
{
return $"{input.Keys.Sum(o => GetParents(o).Count - 1)}";
}
2020-12-02 06:58:54 +00:00
protected override string Part2()
2019-12-06 07:02:01 +00:00
{
var you = GetParents("YOU");
var san = GetParents("SAN");
var common = 1;
for (; you[^common] == san[^common]; common++) ;
2019-12-06 07:02:01 +00:00
return $"{you.Count + san.Count - common * 2}";
}
}
}