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

2

u/ignaciovaz Dec 09 '15 edited Dec 09 '15

Elixir here and I also used the dark heuristic force.

I shamefully introduce: the poor man's genetic random solution. It generates random permutations in the location list until it finds a solution that "wins" for a certain amount of iterations.

defmodule Distances do
	def find_best(locations, distances) do find(locations, distances, [], :infinity, 0, &(&1 < &2)) end
	def find_worst(locations, distances) do find(locations, distances, [], 0, 0, &(&2 < &1)) end

	def find(_, _, best_route, best_distance, 100_000, _) do
		{best_route, best_distance}
	end

	def find(locations, distances, best_route, best_distance, best_times_count, compare_fn) do
		route = Enum.shuffle locations
		{_, distance} = Enum.reduce(route, {"", 0}, fn city, {previous_city, distance} ->
			distance = distance + Dict.get(distances, { previous_city, city }, 0)
			{city, distance}
		end)

		if compare_fn.(distance, best_distance) do
			find(locations, distances, route, distance, 0, compare_fn)
		else
			find(locations, distances, best_route, best_distance, best_times_count + 1, compare_fn)
		end
	end
end

input_stream = File.stream! "input.txt"
{locations, distances} = Enum.reduce(input_stream, {MapSet.new, %{}}, fn line, {locations, distances} ->
	[from, to, distance] = Regex.run(~r|(.*?) to (.*?) = (\d+)|, line, capture: :all_but_first)
	distance = String.to_integer(distance)

	distances = Dict.put(distances, {from, to}, distance)
	distances = Dict.put(distances, {to, from}, distance)

	locations = MapSet.put(locations, from)
	locations = MapSet.put(locations, to)

	{locations, distances}
end)

locations = Enum.into locations, []

{_, distance} = Distances.find_best(locations, distances)
IO.puts "Part 1: Shortest route distance: " <> to_string(distance)

{_, distance} = Distances.find_worst(locations, distances)
IO.puts "Part 2: Longest route distance: " <> to_string(distance)

2

u/ignaciovaz Dec 09 '15

By the way, I found a nice algorithm to generate permutations in Elixir.

def permutations([]) do [[]] end
def permutations(list) do
	for h <- list, t <- permutations(list -- [h]), do: [h | t]
end

1

u/hutsboR Dec 09 '15

You used black magic. I don't blame you, it took me an hour to figure out how to write the permutations algorithm. As a consequence, I lost two hours of sleep last night.

1

u/ignaciovaz Dec 09 '15

we are on the same boat. Barely getting any sleep now.