-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day06.cs
169 lines (142 loc) · 5.17 KB
/
Day06.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
namespace aoc2019
{
internal class Day06
{
public static void Run()
{
var input = File.ReadAllLines(Path.Combine("input", "day06.txt")).ToList();
var res1 = OrbitChecksum(input);
Console.WriteLine($"Day06 - part1 - result: {res1}");
var res2 = OrbitTransfers(input, "YOU", "SAN");
Console.WriteLine($"Day02 - part2 - result: {res2}");
}
public static int OrbitChecksum(IEnumerable<string> input)
{
var orbits = input.Select(s => s.Split(')')).Select(s => (c: s[0], o: s[1]));
var orbitTree = BuildOrbitTree(orbits);
var orbitCount = 0;
foreach(var node in orbitTree.Values)
orbitCount += Depth(node);
return orbitCount;
}
public static int OrbitTransfers(IEnumerable<string> input, string from, string to)
{
var orbits = input.Select(s => s.Split(')')).Select(s => (c: s[0], o: s[1]));
var orbitTree = BuildOrbitTree(orbits);
var startNode = orbitTree[from];
var endNode = orbitTree[to];
var visitedNodes = new HashSet<OrbitNode>();
var frontier = new List<OrbitNode>() { startNode };
var distance = 0;
// Perform a breadth-first search until we reach "to"
while(! frontier.Contains(endNode) )
{
frontier = frontier.SelectMany(n => n.Connections).Where(n => visitedNodes.Add(n)).ToList();
distance++;
}
return distance - 2; // -2 because we ignore the orbit segments of YOU and SAN
}
// the Depth of a node is the number of parents it takes to reach the Center of Mass
private static int Depth(OrbitNode node)
{
var depth = 0;
for(var current = node; !current.IsCenterOfMass; current = current.Parent, depth++);
return depth;
}
private static Dictionary<string, OrbitNode> BuildOrbitTree(IEnumerable<(string c, string o)> orbits)
{
var nodes = new Dictionary<string, OrbitNode>();
// small helper function
OrbitNode FindOrCreateNode(string id)
{
if ( ! nodes.TryGetValue(id, out OrbitNode node) )
return nodes[id] = new OrbitNode(id);
return node;
}
foreach(var orbit in orbits)
{
var nodeC = FindOrCreateNode(orbit.c);
var nodeO = FindOrCreateNode(orbit.o);
nodeO.SetParent(nodeC);
}
return nodes;
}
private sealed class OrbitNode : IEquatable<OrbitNode>
{
private readonly HashSet<OrbitNode> _children = new HashSet<OrbitNode>();
public string Id { get; private set; }
public bool IsCenterOfMass => Id == "COM";
public OrbitNode Parent { get; private set; }
public IEnumerable<OrbitNode> Children => _children;
public IEnumerable<OrbitNode> Connections => IsCenterOfMass ? _children : _children.Append(Parent);
public OrbitNode(string id)
{
Id = id;
}
// Set the Parent of this node to the given parent node
// also adds this node to the Children list of the parent
public void SetParent(OrbitNode parent)
{
if ( IsCenterOfMass )
throw new Exception("Center of Mass can not have a parent");
if ( Parent != null && Parent.Id != parent.Id )
throw new Exception($"Parent redefinition: {parent.Id} but was already {Parent.Id}");
Parent = parent;
parent._children.Add(this);
}
public override bool Equals(object obj) => Equals(obj as OrbitNode);
public override int GetHashCode() => Id.GetHashCode();
public bool Equals(OrbitNode other) => other != null && other.Id == Id;
public override string ToString() => Id;
}
}
[TestFixture]
internal class Day06Tests
{
private static string[] TestMap1 = new[] {
"COM)B",
"B)C",
"C)D",
"D)E",
"E)F",
"B)G",
"G)H",
"D)I",
"E)J",
"J)K",
"K)L",
};
private static string[] TestMap2 = new[] {
"COM)B",
"B)C",
"C)D",
"D)E",
"E)F",
"B)G",
"G)H",
"D)I",
"E)J",
"J)K",
"K)L",
"K)YOU",
"I)SAN"
};
[Test]
public void Test1()
{
var actual = Day06.OrbitChecksum(TestMap1);
Assert.AreEqual(42, actual);
}
[Test]
public void Test2()
{
var actual = Day06.OrbitTransfers(TestMap2, "YOU", "SAN");
Assert.AreEqual(4, actual);
}
}
}