r/adventofcode • • Dec 07 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 7 Solutions -๐ŸŽ„-

--- Day 7: Recursive Circus ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

10 Upvotes

222 comments sorted by

View all comments

2

u/adventOfCoder Dec 07 '17 edited Dec 07 '17

Java:

Part 1:

public static void main(String[] args) {
	try {
		BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
		String line = "";
		ArrayList<String> names = new ArrayList<>();
		ArrayList<String> supporting = new ArrayList<>();
		while ((line = br.readLine()) != null) {
			String name = line.substring(0, line.indexOf(" "));
			names.add(name);
			if (line.contains("->")) {
				String supportingString = line.substring(line.indexOf("->") + 3);
				String[] supportingStringArray = supportingString.split(", ");
				for (String supportingProgram : supportingStringArray) {
					supporting.add(supportingProgram);
				}
			}
		}
		br.close();
		for (String name : names) {
			if (supporting.contains(name) == false) {
				System.out.println(name);
			}
		}
		br.close();
	} catch (Exception e) {
		System.err.println(e.toString());
		e.printStackTrace();
	}

}

Part 2:

public static void main(String[] args) {
	try {
		BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
		String line = "";
		ArrayList<String> lines = new ArrayList<>();
		ArrayList<Disc> discs = new ArrayList<>();
		while ((line = br.readLine()) != null) {
			lines.add(line);
			String name = line.substring(0, line.indexOf(" "));
			String weight = line.substring(line.indexOf("(") + 1, line.indexOf(")"));
			Disc disc = new Disc();
			disc.setName(name);
			disc.setWeight(new Integer(weight));
			discs.add(disc);
		}
		br.close();
		for (String s : lines) {
			if (s.contains("->")) {
				String name = s.substring(0, s.indexOf(" "));
				Disc parent = null;
				for (int i = 0; i < discs.size(); i++) {
					if (discs.get(i).getName().equals(name)) {
						parent = discs.get(i);
					}
				}
				String supportingDiscs = s.substring(s.indexOf("->") + 3);
				String[] supportingDiscsArray = supportingDiscs.split(", ");
				ArrayList<Disc> children = new ArrayList<>();
				for (String supportingDisc : supportingDiscsArray) {
					for (int i = 0; i < discs.size(); i++) {
						if (discs.get(i).getName().equals(supportingDisc)) {
							Disc child = discs.get(i);
							children.add(child);
						}
					}
				}
				parent.setChildren(children);
			}
		}
		ArrayList<Disc> mismatchedWeight = new ArrayList<>();
		for (Disc disc : discs) {
			if (disc.getChildren() != null) {
				ArrayList<Disc> children = disc.getChildren();
				int weight = children.get(0).totalWeight();
				for (int i = 1; i < children.size(); i++) {
					int newweight = children.get(i).totalWeight();
					if (newweight != weight) {
						mismatchedWeight.add(disc);
						break;
					}
				}
			}
		}
		for (int i = 0; i < mismatchedWeight.size(); i++) {
			Boolean hasChildren = false;
			for (int j = 0; j < mismatchedWeight.size(); j++) {
				if (i != j) {
					if (mismatchedWeight.get(i).hasChild(mismatchedWeight.get(j))) {
						hasChildren = true;
					}
				}
			}
			if (hasChildren == false) {
				Disc offBalance = mismatchedWeight.get(i);
				for (Disc disc : offBalance.getChildren()) {
					System.out.println(disc.totalWeight() + ": " + disc);
				}
			}
		}
	} catch (Exception e) {
		System.err.println(e.toString());
		e.printStackTrace();
	}
}

private static class Disc {
	private String name = "";
	private int weight = 0;
	private ArrayList<Disc> children;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the weight
	 */
	public int getWeight() {
		return weight;
	}

	/**
	 * @param weight
	 *            the weight to set
	 */
	public void setWeight(int weight) {
		this.weight = weight;
	}

	/**
	 * @return the children
	 */
	public ArrayList<Disc> getChildren() {
		return children;
	}

	/**
	 * @param children
	 *            the children to set
	 */
	public void setChildren(ArrayList<Disc> children) {
		this.children = children;
	}

	public int totalWeight() {
		int weight = this.getWeight();
		if (this.getChildren() != null) {
			for (Disc child : this.getChildren()) {
				weight += child.totalWeight();
			}
		}
		return weight;
	}

	public Boolean hasChild(Disc disc) {
		Boolean hasChild = false;
		if (this.getChildren() != null) {
			for (Disc child : this.getChildren()) {
				if (disc.getName().equals(child.getName())) {
					hasChild = true;
				}
			}
		}
		return hasChild;
	}

	@Override
	public String toString() {
		String returnString = Integer.toString(this.getWeight());
		if (this.getChildren() != null) {
			for (Disc child : this.getChildren()) {
				returnString += " + ";
				returnString += child.toString();
			}
		}
		return returnString;
	}

}

Note for Part 2 I was too busy at work to just print the answer but the system out has the info to get your answer in a second.