|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.IO; |
| 4 | +using System.Threading; |
3 | 5 |
|
4 | 6 | namespace adventofcode.Year2024.Day02; |
5 | 7 |
|
@@ -73,5 +75,155 @@ public void SolvePart1() |
73 | 75 | public void SolvePart2() |
74 | 76 | { |
75 | 77 | Console.WriteLine("Part 2"); |
| 78 | + //var inputFilePath = GetInputFilePath("test"); |
| 79 | + var inputFilePath = GetInputFilePath(); |
| 80 | + Console.WriteLine(inputFilePath); |
| 81 | + |
| 82 | + // Read input file |
| 83 | + string[] lines = File.ReadAllLines(inputFilePath); |
| 84 | + |
| 85 | + int safeReports = 0; |
| 86 | + |
| 87 | + List<string> unsafeReports = new(); |
| 88 | + |
| 89 | + foreach (string report in lines) |
| 90 | + { |
| 91 | + string[] levels = report.Split(' '); |
| 92 | + long previousLevel = Convert.ToInt64(levels[0]); |
| 93 | + long nextLevel = Convert.ToInt64(levels[1]); |
| 94 | + bool wayUp = false; |
| 95 | + bool safe = true; |
| 96 | + |
| 97 | + if(previousLevel < nextLevel) |
| 98 | + wayUp = true; |
| 99 | + |
| 100 | + // if(previousLevel == nextLevel) |
| 101 | + // break; |
| 102 | + |
| 103 | + for (int i = 1; i < levels.Length; i++) |
| 104 | + { |
| 105 | + long actualLevel = Convert.ToInt64(levels[i]); |
| 106 | + long diff = 0; |
| 107 | + |
| 108 | + if (previousLevel < actualLevel && wayUp) |
| 109 | + { |
| 110 | + diff = actualLevel - previousLevel; |
| 111 | + if (diff == 0 || diff > 3) |
| 112 | + { |
| 113 | + safe = false; |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + else if (previousLevel > actualLevel && !wayUp) |
| 118 | + { |
| 119 | + diff = previousLevel - actualLevel; |
| 120 | + if (diff == 0 || diff > 3) |
| 121 | + { |
| 122 | + safe = false; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + safe = false; |
| 129 | + break; |
| 130 | + } |
| 131 | + previousLevel = actualLevel; |
| 132 | + } |
| 133 | + |
| 134 | + if(safe) |
| 135 | + { |
| 136 | + safeReports++; |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + unsafeReports.Add(report); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + Console.WriteLine($"part 2: first found safe reports {safeReports}"); |
| 145 | + Console.WriteLine($"part 2: found unsafe reports {unsafeReports.Count}"); |
| 146 | + |
| 147 | + foreach (string item in unsafeReports) |
| 148 | + { |
| 149 | + string[] originalLevels = item.Split(' '); |
| 150 | + |
| 151 | + int skipIndex = 0; |
| 152 | + |
| 153 | + for (int i = 0; i < originalLevels.Length; i++) |
| 154 | + { |
| 155 | + skipIndex = i; |
| 156 | + |
| 157 | + string[] deleteOneLevel = new string[originalLevels.Length - 1]; |
| 158 | + int newIndex = 0; |
| 159 | + |
| 160 | + for (int d = 0; d < originalLevels.Length; d++) |
| 161 | + { |
| 162 | + if (d != skipIndex) |
| 163 | + { |
| 164 | + deleteOneLevel[newIndex] = originalLevels[d]; |
| 165 | + newIndex++; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + //old part repeated for finding more safe reports |
| 170 | + //string[] levels = report.Split(' '); |
| 171 | + long previousLevel = Convert.ToInt64(deleteOneLevel[0]); |
| 172 | + long nextLevel = Convert.ToInt64(deleteOneLevel[1]); |
| 173 | + bool wayUp = false; |
| 174 | + bool safe = true; |
| 175 | + |
| 176 | + if(previousLevel < nextLevel) |
| 177 | + wayUp = true; |
| 178 | + |
| 179 | + // if(previousLevel == nextLevel) |
| 180 | + // break; |
| 181 | + |
| 182 | + for (int f = 1; f < deleteOneLevel.Length; f++) |
| 183 | + { |
| 184 | + long actualLevel = Convert.ToInt64(deleteOneLevel[f]); |
| 185 | + long diff = 0; |
| 186 | + |
| 187 | + if (previousLevel < actualLevel && wayUp) |
| 188 | + { |
| 189 | + diff = actualLevel - previousLevel; |
| 190 | + if (diff == 0 || diff > 3) |
| 191 | + { |
| 192 | + safe = false; |
| 193 | + break; |
| 194 | + } |
| 195 | + } |
| 196 | + else if (previousLevel > actualLevel && !wayUp) |
| 197 | + { |
| 198 | + diff = previousLevel - actualLevel; |
| 199 | + if (diff == 0 || diff > 3) |
| 200 | + { |
| 201 | + safe = false; |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + safe = false; |
| 208 | + break; |
| 209 | + } |
| 210 | + previousLevel = actualLevel; |
| 211 | + } |
| 212 | + |
| 213 | + if(safe) |
| 214 | + { |
| 215 | + safeReports++; |
| 216 | + break; |
| 217 | + } |
| 218 | + |
| 219 | + // end of old part |
| 220 | + } |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + Console.WriteLine($"Part 2: Count of complete safe reports: {safeReports}"); |
76 | 228 | } |
77 | 229 | } |
0 commit comments