-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlotsa-steps.cs
118 lines (97 loc) · 2.32 KB
/
lotsa-steps.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
using System;
using System.Collections.Generic;
namespace AdventOfCode
{
class Step
{
public char name;
public Dictionary<char, Step> requirements;
public Dictionary<char, Step> descendants;
public void AddRequirement(Step req)
{
Step entry;
this.requirements.TryGetValue(req.name, out entry);
if(entry == null) this.requirements[req.name] = req;
}
public void AddDescendant(Step desc)
{
Step entry;
this.descendants.TryGetValue(desc.name, out entry);
if(entry == null) this.descendants[desc.name] = desc;
}
public void RemoveRequirement(Step req)
{
this.requirements.Remove(req.name);
}
public void RemoveDescendant(Step desc)
{
this.descendants.Remove(desc.name);
}
public Step(char theName)
{
this.name = theName;
this.requirements = new Dictionary<char, Step>();
this.descendants = new Dictionary<char, Step>();
}
}
class Day07
{
private static Dictionary<char, Step> steps = new Dictionary<char, Step>();
private static Step GetStep(char name)
{
Step step;
Day07.steps.TryGetValue(name, out step);
if(step == null)
{
step = new Step(name);
Day07.steps[name] = step;
}
return step;
}
private static void ReadInput()
{
string line;
while((line = Console.ReadLine()) != null)
{
string[] words = line.Split(" ".ToCharArray());
if(words.Length < 8) continue;
char nameR = words[1].ToCharArray()[0];
char nameD = words[7].ToCharArray()[0];
Step stepR = Day07.GetStep(nameR);
Step stepD = Day07.GetStep(nameD);
stepR.AddDescendant(stepD);
stepD.AddRequirement(stepR);
}
}
private static Step GetNextStep()
{
Step best = null;
foreach(KeyValuePair<char, Step> entry in Day07.steps)
{
if(entry.Value.requirements.Count > 0) continue;
if(best != null)
{
if(best.name < entry.Value.name) continue;
}
best = entry.Value;
}
return best;
}
static void Main()
{
Day07.steps = new Dictionary<char, Step>();
Day07.ReadInput();
Step s;
while((s = GetNextStep()) != null)
{
Console.Write(s.name);
Day07.steps.Remove(s.name);
foreach(KeyValuePair<char, Step> entry in s.descendants)
{
entry.Value.RemoveRequirement(s);
}
}
Console.WriteLine("");
}
}
}