1
0
Fork 0
Browse Source

TASK: 2019 - Day 6

master
Michael Gerdemann 3 years ago
parent
commit
e8e3841b6a
  1. 104
      2019/6/app.dart
  2. 1604
      2019/6/input.txt

104
2019/6/app.dart

@ -0,0 +1,104 @@
import 'dart:io';
class Orbit {
String name;
Orbit directConnection = null;
Orbit(this.name);
toString() {
return this.name;
}
}
List<Orbit> orbits = new List();
void main() {
new File('input.txt').readAsLines().then((List<String> connections) {
for (int i = 0; i < connections.length; i++) {
List<String> connectionParts = connections[i].trim().split(')');
String leftOrbitName = connectionParts[0];
String rightOrbitName = connectionParts[1];
Orbit leftOrbit = findOrCreateOrbit(leftOrbitName);
Orbit rightOrbit = findOrCreateOrbit(rightOrbitName);
rightOrbit.directConnection = leftOrbit;
}
int connectionCount = 0;
Orbit you;
Orbit santa;
for (int i = 0; i < orbits.length; i++) {
if (orbits[i].directConnection != null) {
connectionCount += count(orbits[i].directConnection);
}
if (orbits[i].name == 'YOU') {
you = orbits[i];
}
if (orbits[i].name == 'SAN') {
santa = orbits[i];
}
}
List<Orbit> youConnections = getConnections(you);
List<Orbit> santaConnections = getConnections(santa);
int youCounter = 0;
int santaCounter = 0;
youloop: for (int i = 0; i < youConnections.length; i++) {
youCounter++;
santaCounter = 0;
santaloop: for (int j = 0; j < santaConnections.length; j++) {
santaCounter++;
if (youConnections[i].name == santaConnections[j].name) {
break youloop;
break santaloop;
}
}
}
// without you
youCounter--;
// without santa
santaCounter--;
int sum = youCounter + santaCounter;
// without connection point for both
sum--;
sum--;
// Print #1 result
print('First Part: ' + connectionCount.toString());
// Print #2 result
print('Second Part: ' + sum.toString());
});
}
Orbit findOrCreateOrbit(String name) {
for (int i = 0; i < orbits.length; i++) {
if (orbits[i].name == name) {
return orbits[i];
}
}
Orbit newOrbit = new Orbit(name);
orbits.add(newOrbit);
return newOrbit;
}
int count(Orbit orbit) {
int counter = 1;
if (orbit.directConnection != null) {
counter += count(orbit.directConnection);
}
return counter;
}
List<Orbit> getConnections(Orbit orbit) {
List<Orbit> orbits = new List();
orbits.add(orbit);
if (orbit.directConnection != null) {
List<Orbit> connections = getConnections(orbit.directConnection);
return new List.from(orbits)..addAll(connections);
}
return orbits;
}

1604
2019/6/input.txt

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save