From 583229b4db9d4a62962dfb70e270b17757c718cf Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Thu, 26 Sep 2024 16:47:43 +0200 Subject: Day 13 --- 2015/day13.exs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 2015/day13.exs diff --git a/2015/day13.exs b/2015/day13.exs new file mode 100644 index 0000000..e23dc34 --- /dev/null +++ b/2015/day13.exs @@ -0,0 +1,64 @@ +pairings = + File.stream!("inputs/day13.txt") + |> Enum.reduce(%{}, fn line, acc -> + [_, from, dir, amount, to] = + Regex.run( + ~r/^(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)./, + line + ) + + {amount, ""} = Integer.parse(amount) + + amount = + if dir == "lose" do + -amount + else + +amount + end + + {_, newAcc} = + Map.get_and_update(acc, from, fn + nil -> {nil, %{to => amount}} + existing -> {existing, Map.put(existing, to, amount)} + end) + + newAcc + end) + +defmodule A do + def permutations([lastElement]) do + [[lastElement]] + end + + def permutations(list) do + Enum.flat_map(list, fn start -> + rest = List.delete(list, start) + + Enum.map(permutations(rest), fn perm -> + [start | perm] + end) + end) + end + + def maxHappiness(pairings, people) do + A.permutations(people) + |> Enum.map(fn tableSorting -> + offsetSorting = Stream.cycle(tableSorting) |> Stream.drop(1) + + Enum.zip(tableSorting, offsetSorting) + |> Enum.map(fn {p1, p2} -> + oneToTwo = Map.get(pairings, p1, %{}) |> Map.get(p2, 0) + twoToOne = Map.get(pairings, p2, %{}) |> Map.get(p1, 0) + oneToTwo + twoToOne + end) + |> Enum.sum() + end) + |> Enum.max() + end +end + +maxHappinessWithoutMe = A.maxHappiness(pairings, Map.keys(pairings)) +IO.puts("Maximum happiness without me: #{maxHappinessWithoutMe}") + +maxHappinessWithMe = A.maxHappiness(pairings, ["Me" | Map.keys(pairings)]) +IO.puts("Maximum happiness with me: #{maxHappinessWithMe}") -- cgit v1.2.3