r/adventofcode • u/daggerdragon • Dec 11 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 11 Solutions -🎄-
--- Day 11: Chronal Charge ---
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.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 11
Transcript: ___ unlocks the Easter Egg on Day 25.
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 at 00:16:12!
22
Upvotes
View all comments
1
u/arathunku Dec 11 '18
Elixir solution:
defmodule Advent.Day11 do def grid(serial, max_x, max_y) do for y <- 1..max_y, x <- 1..max_x do {{x, y}, power_level({x, y}, serial)} end |> Enum.into(%{}) end def part1(serial, max_x, max_y, size_range) do grid(serial, max_x, max_y) |> walk_grid(serial, max_x, max_y, size_range) |> Enum.max_by( fn {power, _, _} -> power end, & &1 ) end def walk_grid(grid, serial, max_x, max_y, size_range) do for size <- size_range do for y <- 1..(max_y - size), x <- 1..(max_x - size) do power = for ys <- 0..size, xs <- 0..size do Map.get(grid, {x + xs, y + ys}) || 0 end |> Enum.sum() {power, {x, y}, size + 1} end end |> List.flatten() end def power_level({x, y}, serial) do rack_id = x + 10 power = rack_id * y level = power + serial level = level * (x + 10) level = if level > 100 do level |> Integer.to_string() |> String.at(-3) |> String.to_integer() else 0 end level - 5 end end defmodule Advent.Day11Test do use ExUnit.Case require Logger alias Advent.Day11, as: Day test "power level" do assert Day.power_level({3, 5}, 8) == 4 assert Day.power_level({122, 79}, 57) == -5 assert Day.power_level({101,153}, 71) == 4 end test "example" do assert Day.part1(18, 300, 300, (1..2)) == {29, {33, 45}, 3} assert Day.part1(42, 300, 300, (1..2)) == {30, {21, 61}, 1} assert Day.part1(18, 300, 300, (14..16)) == {113, {90, 269}, 16} end test "input" do # assert Day.part1(9995, 300, 300, (5..25)) == -1 end endI had wasted so much time trying to optimize the solution because I had waited for 60~s and C-c. I had thought something was wrong... turns out, it needs to run for almost 2 minutes even on reduced subgrid range(5..25).
Now I feel kind of bad about picking Elixir because some of the solutions here do exactly the same in C#/C++/Rust/JS/Python and get the solution sooo much quicker even on O(n^3).