|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:convert'; |
| 3 | +import 'dart:async'; |
| 4 | + |
| 5 | +import 'package:args/args.dart'; |
| 6 | + |
| 7 | +class Cell { |
| 8 | + int row, column; |
| 9 | + |
| 10 | + // I ❤️ Dart for this |
| 11 | + Cell( |
| 12 | + this.row, |
| 13 | + this.column, |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +class City { |
| 18 | + int rows, columns, maximumWalkingDistance, nBuildingPlans; |
| 19 | + List<List<int>> constructedBuildings, map; |
| 20 | + |
| 21 | + City( |
| 22 | + this.rows, |
| 23 | + this.columns, |
| 24 | + this.maximumWalkingDistance, |
| 25 | + this.nBuildingPlans, |
| 26 | + this.constructedBuildings, |
| 27 | + ) { |
| 28 | + this.map = new List(); |
| 29 | + for (int row = 0; row < this.rows; row++) { |
| 30 | + this.map.add(new List()); |
| 31 | + |
| 32 | + for (int column = 0; column < this.columns; column++) { |
| 33 | + this.map[row].add(-1); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class Project { |
| 40 | + String type; |
| 41 | + int index, height, width, capacityOrService, surface; |
| 42 | + double efficiency; |
| 43 | + List<Cell> occupidCells = new List(); |
| 44 | + |
| 45 | + // Dart is amamzing! |
| 46 | + Project( |
| 47 | + this.index, |
| 48 | + this.type, |
| 49 | + this.height, |
| 50 | + this.width, |
| 51 | + this.capacityOrService, |
| 52 | + ) { |
| 53 | + this.surface = this.height * this.width; |
| 54 | + |
| 55 | + if (this.type == 'R') { |
| 56 | + this.efficiency = this.capacityOrService / this.surface; |
| 57 | + } else { |
| 58 | + this.efficiency = 1 / this.surface; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + void parseAndAddRowOfCells(int index, String rawRow) { |
| 63 | + for (int i = 0; i < rawRow.length; i++) { |
| 64 | + if (rawRow[i] == '#') { |
| 65 | + Cell cell = new Cell(index, i); |
| 66 | + occupidCells.add(cell); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + bool canPlace(City city, Cell cell) { |
| 72 | + // is outside of the city map |
| 73 | + if (cell.column < 0 || |
| 74 | + cell.row < 0 || |
| 75 | + cell.column + this.width > city.columns || |
| 76 | + cell.row + this.height > city.rows) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + // check all occupid cells |
| 81 | + for (int i = 0; i < this.occupidCells.length; i++) { |
| 82 | + if (city.map[cell.row + this.occupidCells[i].row] |
| 83 | + [cell.column + this.occupidCells[i].column] != |
| 84 | + -1) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + void place(City city, Cell cell) { |
| 93 | + city.constructedBuildings.add([ |
| 94 | + this.index, |
| 95 | + cell.row, |
| 96 | + cell.column, |
| 97 | + ]); |
| 98 | + |
| 99 | + for (int i = 0; i < this.occupidCells.length; i++) { |
| 100 | + city.map[this.occupidCells[i].row + cell.row] |
| 101 | + [this.occupidCells[i].column + cell.column] = this.index; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +Future main(List<String> args) async { |
| 107 | + exitCode = 0; // presume success |
| 108 | + |
| 109 | + // parse arguments |
| 110 | + final ArgParser parser = new ArgParser(); |
| 111 | + parser.addOption('input'); |
| 112 | + parser.addOption('output'); |
| 113 | + |
| 114 | + ArgResults argResults = parser.parse(args); |
| 115 | + |
| 116 | + // parse input lines |
| 117 | + Stream inputLines = new File(argResults['input']) |
| 118 | + .openRead() |
| 119 | + .transform(UTF8.decoder) |
| 120 | + .transform(const LineSplitter()); |
| 121 | + |
| 122 | + var parsedInput = await parseInput(inputLines); |
| 123 | + |
| 124 | + City city = parsedInput[0]; |
| 125 | + List<Project> projects = parsedInput[1]; |
| 126 | + |
| 127 | + placeDistricts( |
| 128 | + city, |
| 129 | + projects, |
| 130 | + ); |
| 131 | + |
| 132 | + IOSink outputSink = new File(argResults['output']).openWrite(); |
| 133 | + |
| 134 | + await printOutput(outputSink, city); |
| 135 | + |
| 136 | + // close streams |
| 137 | + outputSink.close(); |
| 138 | +} |
| 139 | + |
| 140 | +Future parseInput(Stream inputLines) async { |
| 141 | + int lineIndex = 0; |
| 142 | + int nextParsingProject = 0; |
| 143 | + int lineInParsingProject = 0; |
| 144 | + |
| 145 | + City city; |
| 146 | + List<Project> projects; |
| 147 | + |
| 148 | + try { |
| 149 | + await for (String line in inputLines) { |
| 150 | + List<String> lineItems = line.split(' '); |
| 151 | + |
| 152 | + if (lineIndex == 0) { |
| 153 | + city = new City( |
| 154 | + int.parse(lineItems[0]), |
| 155 | + int.parse(lineItems[1]), |
| 156 | + int.parse(lineItems[2]), |
| 157 | + int.parse(lineItems[3]), |
| 158 | + new List(), |
| 159 | + ); |
| 160 | + |
| 161 | + projects = new List(city.nBuildingPlans); |
| 162 | + } else { |
| 163 | + if (lineInParsingProject == 0) { |
| 164 | + String type = lineItems[0]; |
| 165 | + int height = int.parse(lineItems[1]); |
| 166 | + int width = int.parse(lineItems[2]); |
| 167 | + int capacityOrService = int.parse(lineItems[3]); |
| 168 | + |
| 169 | + projects[nextParsingProject] = new Project( |
| 170 | + nextParsingProject, |
| 171 | + type, |
| 172 | + height, |
| 173 | + width, |
| 174 | + capacityOrService, |
| 175 | + ); |
| 176 | + |
| 177 | + nextParsingProject++; |
| 178 | + lineInParsingProject++; |
| 179 | + } else { |
| 180 | + Project currentProject = projects[nextParsingProject - 1]; |
| 181 | + currentProject.parseAndAddRowOfCells( |
| 182 | + lineInParsingProject - 1, |
| 183 | + line, |
| 184 | + ); |
| 185 | + |
| 186 | + // reset the parsing line |
| 187 | + if (lineInParsingProject == currentProject.height) { |
| 188 | + lineInParsingProject = 0; |
| 189 | + } else { |
| 190 | + lineInParsingProject++; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + lineIndex++; |
| 196 | + } |
| 197 | + } catch (err) { |
| 198 | + stdout.writeln(err); |
| 199 | + exitCode = 2; |
| 200 | + } |
| 201 | + |
| 202 | + return [city, projects]; |
| 203 | +} |
| 204 | + |
| 205 | +Future printOutput(IOSink outputSink, City city) async { |
| 206 | + await outputSink.writeln(city.constructedBuildings.length); |
| 207 | + |
| 208 | + for (int i = 0; i < city.constructedBuildings.length; i++) { |
| 209 | + await outputSink.write(city.constructedBuildings[i][0]); |
| 210 | + await outputSink.write(' '); |
| 211 | + await outputSink.write(city.constructedBuildings[i][1]); |
| 212 | + await outputSink.write(' '); |
| 213 | + await outputSink.write(city.constructedBuildings[i][2]); |
| 214 | + await outputSink.write('\n'); |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +List<Project> getResidentialProjects(List<Project> projects) { |
| 219 | + List<Project> residentialProjects = new List.from(projects); |
| 220 | + residentialProjects.retainWhere((Project project) => project.type == 'R'); |
| 221 | + |
| 222 | + return residentialProjects; |
| 223 | +} |
| 224 | + |
| 225 | +void sortProjects(List<Project> project) { |
| 226 | + project.sort((a, b) => b.efficiency.compareTo(a.efficiency)); |
| 227 | +} |
| 228 | + |
| 229 | +void placeDistrict( |
| 230 | + City city, |
| 231 | + Cell initialCell, |
| 232 | + List<Project> projects, |
| 233 | +) { |
| 234 | + Cell currentCell = new Cell(0, 0); |
| 235 | + |
| 236 | + // place residential projects |
| 237 | + bool shoudColumnInset = false; |
| 238 | + |
| 239 | + for (int row = 0; row < 8; row++) { |
| 240 | + int startColumn = 0; |
| 241 | + |
| 242 | + if (shoudColumnInset) { |
| 243 | + startColumn = 1; |
| 244 | + } |
| 245 | + |
| 246 | + for (int column = startColumn; column < 8; column += 2) { |
| 247 | + currentCell.row = initialCell.row + row; |
| 248 | + currentCell.column = initialCell.column + column; |
| 249 | + |
| 250 | + if (projects[30].canPlace(city, currentCell)) { |
| 251 | + projects[30].place(city, currentCell); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + shoudColumnInset = !shoudColumnInset; |
| 256 | + } |
| 257 | + |
| 258 | + // place utility projects |
| 259 | + List<int> utilityProjects = [189, 128, 185, 104]; |
| 260 | + |
| 261 | + int nextUtilityProject(int currentProjectIndex) { |
| 262 | + int nextProjectIndex = currentProjectIndex + 1; |
| 263 | + |
| 264 | + if (nextProjectIndex >= utilityProjects.length) { |
| 265 | + nextProjectIndex = 0; |
| 266 | + } |
| 267 | + |
| 268 | + return nextProjectIndex; |
| 269 | + } |
| 270 | + |
| 271 | + int currentProjectIndex = 0; |
| 272 | + int rowStartingProjectIndex = 1; |
| 273 | + int insetRowStartingProjectIndex = 0; |
| 274 | + |
| 275 | + shoudColumnInset = true; |
| 276 | + |
| 277 | + for (int row = 0; row < 8; row++) { |
| 278 | + int startColumn = 0; |
| 279 | + |
| 280 | + if (shoudColumnInset) { |
| 281 | + startColumn = 1; |
| 282 | + } |
| 283 | + |
| 284 | + for (int column = startColumn; column < 8; column += 2) { |
| 285 | + currentCell.row = initialCell.row + row; |
| 286 | + currentCell.column = initialCell.column + column; |
| 287 | + |
| 288 | + if (projects[utilityProjects[currentProjectIndex]] |
| 289 | + .canPlace(city, currentCell)) { |
| 290 | + projects[utilityProjects[currentProjectIndex]].place(city, currentCell); |
| 291 | + } |
| 292 | + |
| 293 | + currentProjectIndex = nextUtilityProject(currentProjectIndex); |
| 294 | + } |
| 295 | + |
| 296 | + shoudColumnInset = !shoudColumnInset; |
| 297 | + |
| 298 | + if (shoudColumnInset) { |
| 299 | + currentProjectIndex = nextUtilityProject(insetRowStartingProjectIndex); |
| 300 | + insetRowStartingProjectIndex++; |
| 301 | + } else { |
| 302 | + currentProjectIndex = nextUtilityProject(rowStartingProjectIndex); |
| 303 | + rowStartingProjectIndex++; |
| 304 | + } |
| 305 | + } |
| 306 | +} |
| 307 | + |
| 308 | +void placeDistricts( |
| 309 | + City city, |
| 310 | + List<Project> projects, |
| 311 | +) { |
| 312 | + for (int row = 0; row < city.rows; row += 8) { |
| 313 | + for (int column = 0; column < city.columns; column += 8) { |
| 314 | + placeDistrict( |
| 315 | + city, |
| 316 | + new Cell(row, column), |
| 317 | + projects, |
| 318 | + ); |
| 319 | + } |
| 320 | + } |
| 321 | +} |
| 322 | + |
| 323 | +void placeResidentialProjects( |
| 324 | + City city, |
| 325 | + List<Project> residentialProjects, { |
| 326 | + int currentResidentialProject, |
| 327 | +}) { |
| 328 | + for (int row = 0; row < city.rows; row++) { |
| 329 | + for (int column = 0; column < city.columns; column++) { |
| 330 | + Cell cell = new Cell(row, column); |
| 331 | + |
| 332 | + if (residentialProjects[currentResidentialProject].canPlace(city, cell)) { |
| 333 | + residentialProjects[currentResidentialProject].place(city, cell); |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | +} |
0 commit comments