|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace AdventOfCode |
| 5 | +{ |
| 6 | + class Step |
| 7 | + { |
| 8 | + public char name; |
| 9 | + public int? timeUntilDone; |
| 10 | + public Dictionary<char, Step> requirements; |
| 11 | + public Dictionary<char, Step> descendants; |
| 12 | + |
| 13 | + public void AddRequirement(Step req) |
| 14 | + { |
| 15 | + Step entry; |
| 16 | + this.requirements.TryGetValue(req.name, out entry); |
| 17 | + if(entry == null) this.requirements[req.name] = req; |
| 18 | + } |
| 19 | + |
| 20 | + public void AddDescendant(Step desc) |
| 21 | + { |
| 22 | + Step entry; |
| 23 | + this.descendants.TryGetValue(desc.name, out entry); |
| 24 | + if(entry == null) this.descendants[desc.name] = desc; |
| 25 | + } |
| 26 | + |
| 27 | + public void RemoveRequirement(Step req) |
| 28 | + { |
| 29 | + this.requirements.Remove(req.name); |
| 30 | + } |
| 31 | + |
| 32 | + public void RemoveDescendant(Step desc) |
| 33 | + { |
| 34 | + this.descendants.Remove(desc.name); |
| 35 | + } |
| 36 | + |
| 37 | + public Step(char theName) |
| 38 | + { |
| 39 | + this.name = theName; |
| 40 | + this.requirements = new Dictionary<char, Step>(); |
| 41 | + this.descendants = new Dictionary<char, Step>(); |
| 42 | + this.timeUntilDone = null; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + class Day07 |
| 47 | + { |
| 48 | + private static Dictionary<char, Step> steps = new Dictionary<char, Step>(); |
| 49 | + |
| 50 | + private static Step GetStep(char name) |
| 51 | + { |
| 52 | + Step step; |
| 53 | + Day07.steps.TryGetValue(name, out step); |
| 54 | + if(step == null) |
| 55 | + { |
| 56 | + step = new Step(name); |
| 57 | + Day07.steps[name] = step; |
| 58 | + } |
| 59 | + |
| 60 | + return step; |
| 61 | + } |
| 62 | + |
| 63 | + private static void ReadInput() |
| 64 | + { |
| 65 | + string line; |
| 66 | + while((line = Console.ReadLine()) != null) |
| 67 | + { |
| 68 | + string[] words = line.Split(" ".ToCharArray()); |
| 69 | + if(words.Length < 8) continue; |
| 70 | + |
| 71 | + char nameR = words[1].ToCharArray()[0]; |
| 72 | + char nameD = words[7].ToCharArray()[0]; |
| 73 | + |
| 74 | + Step stepR = Day07.GetStep(nameR); |
| 75 | + Step stepD = Day07.GetStep(nameD); |
| 76 | + |
| 77 | + stepR.AddDescendant(stepD); |
| 78 | + stepD.AddRequirement(stepR); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static Step GetNextAvailableStep() |
| 83 | + { |
| 84 | + Step best = null; |
| 85 | + foreach(KeyValuePair<char, Step> entry in Day07.steps) |
| 86 | + { |
| 87 | + if(entry.Value.requirements.Count > 0) continue; |
| 88 | + if(entry.Value.timeUntilDone != null) continue; |
| 89 | + |
| 90 | + if(best != null) |
| 91 | + { |
| 92 | + if(best.name < entry.Value.name) continue; |
| 93 | + } |
| 94 | + |
| 95 | + best = entry.Value; |
| 96 | + } |
| 97 | + |
| 98 | + return best; |
| 99 | + } |
| 100 | + |
| 101 | + private static void AdvanceSteps() |
| 102 | + { |
| 103 | + foreach(KeyValuePair<char, Step> entry in Day07.steps) |
| 104 | + { |
| 105 | + if(entry.Value.timeUntilDone == null) continue; |
| 106 | + entry.Value.timeUntilDone -= 1; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static Step GetNextFinishedStep() |
| 111 | + { |
| 112 | + Step best = null; |
| 113 | + foreach(KeyValuePair<char, Step> entry in Day07.steps) |
| 114 | + { |
| 115 | + if(entry.Value.timeUntilDone != 0) continue; |
| 116 | + |
| 117 | + if(best != null) |
| 118 | + { |
| 119 | + if(best.name < entry.Value.name) continue; |
| 120 | + } |
| 121 | + |
| 122 | + best = entry.Value; |
| 123 | + } |
| 124 | + |
| 125 | + return best; |
| 126 | + } |
| 127 | + |
| 128 | + static void Main() |
| 129 | + { |
| 130 | + Day07.steps = new Dictionary<char, Step>(); |
| 131 | + Day07.ReadInput(); |
| 132 | + |
| 133 | + int extraTime = 60; |
| 134 | + int workerLimit = 5; |
| 135 | + |
| 136 | + int simultaneousSteps = 0; |
| 137 | + int totalTime = 0; |
| 138 | + while(Day07.steps.Count > 0) |
| 139 | + { |
| 140 | + Step s; |
| 141 | + while( (simultaneousSteps < workerLimit) && ((s = Day07.GetNextAvailableStep()) != null) ) |
| 142 | + { |
| 143 | + Console.Write(s.name); |
| 144 | + s.timeUntilDone = extraTime + (Convert.ToByte(s.name) - Convert.ToByte('A') + 1); |
| 145 | + simultaneousSteps += 1; |
| 146 | + } |
| 147 | + |
| 148 | + Day07.AdvanceSteps(); |
| 149 | + |
| 150 | + while((s = Day07.GetNextFinishedStep()) != null) |
| 151 | + { |
| 152 | + Day07.steps.Remove(s.name); |
| 153 | + foreach(KeyValuePair<char, Step> entry in s.descendants) |
| 154 | + { |
| 155 | + entry.Value.RemoveRequirement(s); |
| 156 | + } |
| 157 | + |
| 158 | + simultaneousSteps -= 1; |
| 159 | + } |
| 160 | + |
| 161 | + totalTime += 1; |
| 162 | + } |
| 163 | + |
| 164 | + Console.WriteLine(""); |
| 165 | + Console.WriteLine("Part 2 time: " + totalTime); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments