diff options
author | Ben Harris <ben@tilde.team> | 2020-12-16 17:17:35 -0500 |
---|---|---|
committer | Ben Harris <ben@tilde.team> | 2020-12-16 17:17:35 -0500 |
commit | 837527d487c7e232b36dd87c95a15b7852f2e057 (patch) | |
tree | 21ddf9a63548ccc8c802fe5f158f3e6743a8e9f1 /Day06.cs | |
parent | c66d1d6b335e6c2f6b544ae31793f318ba2b99a7 (diff) |
refactor to two-digit day names and add puzzlename
Diffstat (limited to 'Day06.cs')
-rw-r--r-- | Day06.cs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Day06.cs b/Day06.cs new file mode 100644 index 0000000..7ef3d0a --- /dev/null +++ b/Day06.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System.Linq; + +namespace aoc2019 +{ + internal sealed class Day06 : Day + { + private readonly Dictionary<string, string> input; + + public Day06() : base(6, "Universal Orbit Map") + { + 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; + } + + protected override string Part1() + { + return $"{input.Keys.Sum(o => GetParents(o).Count - 1)}"; + } + + protected override string Part2() + { + var you = GetParents("YOU"); + var san = GetParents("SAN"); + var common = 1; + for (; you[^common] == san[^common]; common++) ; + return $"{you.Count + san.Count - common * 2}"; + } + } +} \ No newline at end of file |