Created
December 7, 2017 17:36
-
-
Save saucecode/7782b6b14c2362107c7f97e4115b53e9 to your computer and use it in GitHub Desktop.
AoC day 7 part 2 solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
-snip- | |
public class AoC7Solution { | |
static List<Node> nodes = new ArrayList<>(); | |
public static void main(String[] args) { | |
// Read input | |
try { | |
Scanner input = new Scanner(new File("day7challengeinput")); | |
while(input.hasNextLine()) { | |
String line = input.nextLine(); | |
String name = line.split(" ")[0]; | |
int weight = Integer.parseInt(line.split(" ")[1].split("\\(")[1].split("\\)")[0]); | |
Node n = new Node(name, weight); | |
if(line.contains(" -> ")) { | |
n.childrenStrings.addAll(Arrays.asList(line.split(" -> ")[1].split(", "))); | |
} | |
nodes.add(n); | |
} | |
input.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
// Hardcode determinism for the root node | |
Node rootNode = null; | |
for(Node n : nodes) { | |
if(n.name.equals("svugo")) rootNode = n; | |
} | |
nodes.remove(rootNode); | |
System.out.println(rootNode.name + " " + rootNode.weight); | |
// assemble the tree from the trunk upwards, recursively | |
assemble(rootNode); | |
// recursively assign weights by | |
calculateWeightsAbove(rootNode); | |
} | |
public static Node findChild(String name) { | |
for(Node n : nodes) | |
if(n.name.equals(name)) | |
return n; | |
System.out.println("cannot find node named " + name); | |
return null; | |
} | |
public static int calculateWeightsAbove(Node node) { | |
List<Integer> seenWeights = new ArrayList<>(); | |
int weightAbove = 0; | |
for(Node n : node.children) { | |
int childWeight = calculateWeightsAbove(n) + n.weight; | |
seenWeights.add(childWeight); | |
weightAbove += childWeight; | |
} | |
node.weightAbove = weightAbove; | |
boolean allEqual = seenWeights.stream().distinct().limit(2).count() <= 1; | |
if(!allEqual) { | |
System.out.println("Node " + node.name + " is unbalanced!"); | |
for(Integer i : seenWeights) System.out.print(i.intValue() + " "); | |
System.out.println(); | |
System.out.println(node.name + "'s weight above is " + weightAbove + " has children:"); | |
for(Node c : node.children) { | |
System.out.println("\t" + c.name + " " + c.weight + " weight above " + c.weightAbove + " total " + (c.weightAbove + c.weight)); | |
} | |
System.out.println(); | |
} | |
return weightAbove; | |
} | |
// Put this node's children into its list | |
public static void assemble(Node node) { | |
for(String childName : node.childrenStrings) { | |
Node child = findChild(childName); | |
node.children.add(child); | |
nodes.remove(child); | |
} | |
node.childrenStrings.clear(); | |
for(Node n : node.children) { | |
assemble(n); | |
} | |
} | |
static class Node { | |
String name; | |
int weight; | |
int weightAbove = 0; | |
boolean isBalanced = true; | |
Node parent; | |
List<Node> children = new ArrayList<>(); | |
List<String> childrenStrings = new ArrayList<>(); | |
public Node(String name, int weight) { | |
this.weight = weight; | |
this.name = name; | |
} | |
} | |
/* | |
SAMPLE OUTPUT | |
-------- | |
svugo 32 | |
Node yruivis is unbalanced! | |
2671 2671 2680 | |
yruivis's weight above is 8022 has children: | |
oxipms 2533 weight above 138 total 2671 | |
ggpau 91 weight above 2580 total 2671 | |
sphbbz 1161 weight above 1519 total 2680 | |
Node gjxqx is unbalanced! | |
10782 10773 10773 10773 10773 10773 | |
gjxqx's weight above is 64647 has children: | |
yruivis 2760 weight above 8022 total 10782 | |
rizjob 6366 weight above 4407 total 10773 | |
qsfwl 63 weight above 10710 total 10773 | |
asckjlv 4889 weight above 5884 total 10773 | |
sfqwrge 948 weight above 9825 total 10773 | |
bncdhrm 3810 weight above 6963 total 10773 | |
Node svugo is unbalanced! | |
64652 64661 64652 64652 64652 | |
svugo's weight above is 323269 has children: | |
xolvnpy 22946 weight above 41706 total 64652 | |
gjxqx 14 weight above 64647 total 64661 | |
gtzxxav 8936 weight above 55716 total 64652 | |
njorjq 31504 weight above 33148 total 64652 | |
qpiklvf 11870 weight above 52782 total 64652 | |
-------- | |
*/ | |
/* | |
* yruivis's child sphbbz weights 2680, which is 9 above 2671. | |
* sphbbz must go from weight 1161 to 1152 in order to balance yruivis's disc | |
* the answer to AoC7 part 2 is therefore 1152. | |
* | |
* the output shows all trees that are unbalanced, where svugo is the root. | |
* you can trace the path to the unbalanced node! | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment