about summary refs log tree commit diff
path: root/Day06.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Day06.cs')
-rw-r--r--Day06.cs38
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