r/adventofcode • • Dec 09 '15

SOLUTION MEGATHREAD --- Day 9 Solutions ---

This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.

edit: Leaderboard capped, achievement thread unlocked!

We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 9: All in a Single Night ---

Post your solution as a comment. Structure your post like previous daily solution threads.

11 Upvotes

178 comments sorted by

View all comments

Show parent comment

1

u/mal607 Dec 10 '15

Here's a solution with a single stream. Not sure it's much clearer, but it was good to figure out how to do the mapping and reducing. package aoc;

import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;

import com.google.common.collect.Collections2;

public class AocDay9 {

            public static void main(String[] args) {

	    	IntSummaryStatistics stats = FileIO.getFileAsStream("input-9.txt") //stream
	    			.map(s -> parseEntry(s))
	    			.collect(Collector.of(
						HashMap::new, (m1,m2) -> mergeMaps(m1, m2), (t, u) -> t, finish)); //combiner is NOOP

		System.err.println("min value is " + stats.getMin());
		System.err.println("max value is " + stats.getMax());    

	}

	private static Map<String, Map<String, Integer>> parseEntry(String s) {
		HashMap<String, Map<String, Integer>> routes = new HashMap<String, Map<String, Integer>>();
		String[] parts = s.split("\\s");
		if (parts.length == 5) {
			String from = parts[0], to = parts[2], dist = parts[4];
			Map<String, Integer> entry = new HashMap<String, Integer>();
			entry.put(to, new Integer(dist));
			routes.put(from, entry);
			entry = new HashMap<String, Integer>();
			entry.put(from, new Integer(dist));
			routes.put(to, entry);
			return routes;
		} else {
		throw new IllegalArgumentException("Invalid input record");
		}
	}


	private static void mergeMaps(Map<String, Map<String, Integer>> container, Map<String, Map<String, Integer>> element) {
		for (String key : element.keySet()) {
			Map<String, Integer> entry = container.get(key);
			if (entry == null) {
				entry = new HashMap<String, Integer>();
				container.put(key, entry);
		}
			entry.putAll(element.get(key));
		}
	}
     
	private static Function<Map<String, Map<String, Integer>>, IntSummaryStatistics> finish
    	= new Function<Map<String, Map<String, Integer>>, IntSummaryStatistics>() {

		@Override
		public IntSummaryStatistics apply(Map<String, Map<String, Integer>> routes) {
			return Collections2.permutations(routes.keySet()).stream()
					.mapToInt(l -> computeDistance(routes, l))
					.summaryStatistics();
		}

	};

	private static int computeDistance(Map<String, Map<String, Integer>> routes, List<String> l) {
		int distance = 0;
		for (int i = 0; i < (l.size() - 1); i++) {
			distance += routes.get(l.get(i)).get(l.get(i+1));
		}
		return distance;
	}

}