Day 13
Mix.install([
{:kino, "~> 0.14.2"}
])
IEx.Helpers.c("/Users/johnb/dev/2015adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input
# Note: when making the next template, something like this works well:
# `cat day04.livemd | sed 's/03/04/' > day04.livemd`
#
Installation and Data
input_p1example = Kino.Input.textarea("Example Data")
input_p1puzzleInput = Kino.Input.textarea("Puzzle Input")
input_source_select =
Kino.Input.select("Source", [{:example, "example"}, {:puzzle_input, "puzzle input"}])
p1data = fn ->
(Kino.Input.read(input_source_select) == :example &&
Kino.Input.read(input_p1example)) ||
Kino.Input.read(input_p1puzzleInput)
end
Part 1
defmodule Day13 do
@pair_line ~r/(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)./
def parse_pairs(text) do
text
|> AOC.as_single_lines()
|> Enum.reduce(%{}, fn line, acc ->
[[_, who1, gainlose, digits, who2]] = Regex.scan(@pair_line, line)
cost = String.to_integer(digits)
cost = if gainlose =~ "lose" do
-cost
else
cost
end
acc
|> Map.put_new(who1, %{})
|> put_in([who1, who2], cost)
# |> IO.inspect()
end)
end
# Header
def enumerate_seatings(pairs, unseated, _diners \\ [], cost \\ 0)
# Completion
def enumerate_seatings(pairs, _unseated = [], diners, cost) do
{cost +
pairs[List.last(diners)][List.first(diners)] +
pairs[List.first(diners)][List.last(diners)],
Enum.join(diners, " -> ")}
end
# Entry point
def enumerate_seatings(pairs, unseated, _diners = [], cost) do
Enum.map(unseated, fn next_diner ->
enumerate_seatings(pairs, unseated -- [next_diner], [next_diner], cost)
end)
end
# Workhorse
def enumerate_seatings(pairs, unseated, diners, cost) do
Enum.map(unseated, fn next_diner ->
enumerate_seatings(pairs, unseated -- [next_diner], diners ++ [next_diner],
cost + pairs[List.last(diners)][next_diner] + pairs[next_diner][List.last(diners)])
end)
end
def find_all_seatings(text) do
pairs = parse_pairs(text)
enumerate_seatings(pairs, Map.keys(pairs))
|> List.flatten()
|> Enum.sort_by(fn {cost, _seating} -> cost end)
end
def solve1(text) do
find_all_seatings(text)
|> List.last()
end
def add_yourself(seatings) do
Enum.reduce(Map.keys(seatings), Map.put_new(seatings, "me", %{}), fn guest, acc ->
acc
|> put_in(["me", guest], 0)
|> put_in([guest, "me"], 0)
end)
end
def solve2(text) do
pairs = parse_pairs(text) |> add_yourself()
enumerate_seatings(pairs, Map.keys(pairs))
|> List.flatten()
|> Enum.sort_by(fn {cost, _seating} -> cost end)
|> List.last()
end
end
p1data.()
|> Day13.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 330)")
# 709
p1data.()
|> Day13.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 5)")
# 668 (which seems odd that happieness would drop when I arrive)