Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Commit

Permalink
I finissshhheeeeedddd..
Browse files Browse the repository at this point in the history
  • Loading branch information
nomppy committed Jan 17, 2020
1 parent c0df56d commit b4801d6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified out/production/AOC/Day6.class
Binary file not shown.
36 changes: 35 additions & 1 deletion src/Day6.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,45 @@ public static void main(String[] args) throws IOException {
if (hasAncestor(o, a)) orbits ++;
}
}
System.out.println(orbits);
System.out.println("Part 1: " + orbits);

String target = "SAN";
System.out.println("Part 2: " + calDis(tree.get("YOU"), tree.get("SAN")));

double endTime = System.currentTimeMillis();
System.out.println("Time: " + (endTime - startTime) + "ms");
}

static int calDis(Node a, Node b){
// find first shared ancestor

List<Node> aParents = new ArrayList<Node>();
List<Node> bParents = new ArrayList<Node>();

Node curr = a.getParent();
for (int i = a.getHeight(); i > 0; i --){
aParents.add(curr);
curr = curr.getParent();
}

curr = b.getParent();
for (int i = b.getHeight(); i > 0; i --){
bParents.add(curr);
curr = curr.getParent();
}

List<Node> sharedParents = new ArrayList<Node>();
for (Node n : aParents){
if (bParents.contains(n)){
sharedParents.add(n);
}
}

Node common = sharedParents.get(0);
System.out.println("Common: " + common);
return (a.getHeight() - common.getHeight() + b.getHeight() - common.getHeight() - 2);
}

static boolean hasAncestor(Node offspring, Node ancestor){
Node next = offspring.getParent();
for (int i = 0; i < offspring.getHeight() - ancestor.getHeight(); i++) {
Expand Down
18 changes: 12 additions & 6 deletions test.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
B)D
COM)A
A)B
A)C
COM)B
B)C
C)D
D)E
E)G
G)F
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN

0 comments on commit b4801d6

Please sign in to comment.