# Day 11 ```elixir Mix.install([:kino_aoc]) ``` ## Section ```elixir {:ok, puzzle_input} = KinoAOC.download_puzzle("2023", "11", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION")) ``` ``` {:ok, ".#...........#.........................................................................#...............#...........#........................\n.......................#....................................................................................................................\n...............................#...........#.................#.............................#.............................................#..\n......#............................................#........................................................................................\n......................................................................#.....#.....................#.........................................\n.................#.......#.......................................#................#.....#.....................#.............................\n.........................................#.................#................................................................................\n................................#.............................................................#..........#..................................\n#.......................................................................#..............................................#.................#..\n..........#....................................#......................................#............................................#........\n................#....................................#........#.............................................................................\n......#........................................................................#............................................................\n......................#..............#..............................................................#..........#.....#......................\n..................................................#.......................#..................................................#..........#...\n............................#...............................................................................................................\n........#...................................................................................................................................\n..................#.......................#................................................................#................................\n.............................................................#........#.....................................................................\n..............................#.......#......................................#.............#........................#.......................\n.#............................................................................................................#...........................#.\n...................................................................................................#....................#...................\n....................#......................#.............#.....#..................................................................#.........\n.................................................................................................................#..........................\n............................................................................................................................................\n....................................................................#.....................#.................................................\n.................................#..............................................#..............#............#.............#.................\n...........................#............................................#...........................................#.......................\n......................#.........................#...........................................................................................\n.......................................#....................................#...................................#...........................\n.....#." <> ...} ``` ```elixir lines = String.split(puzzle_input, "\n", trim: true) galaxies = for {line, y} <- Enum.with_index(lines), {d, x} <- Enum.with_index(String.to_charlist(line)), d == ?#, do: {x, y} {sx, sy} = Enum.reduce(galaxies, {0, 0}, fn {x, y}, {mx, my} -> {max(x, mx), max(y, my)} end) ``` ``` {139, 139} ``` ```elixir [rows, cols] = Enum.reduce(galaxies, [MapSet.new(), MapSet.new()], fn {x, y}, [rows, cols] -> [MapSet.put(rows, y), MapSet.put(cols, x)] end) empty_rows = MapSet.difference(MapSet.new(0..sy), rows) |> MapSet.to_list() |> Enum.sort() empty_cols = MapSet.difference(MapSet.new(0..sx), cols) |> MapSet.to_list() |> Enum.sort() {empty_rows, empty_cols} ``` ``` {[23, 54, 77, 81, 87, 91, 106, 107], ~c".178SYh"} ``` ```elixir defmodule Day11 do def reduce_unique_pairs(xs, init, fun) when is_function(fun, 3) do do_map(hd(xs), tl(xs), fun, init) end def do_map(_h, [], _fun, acc), do: acc def do_map(a, xs, fun, acc) do new = Enum.reduce(xs, acc, fn b, acc1 -> fun.(a, b, acc1) end) do_map(hd(xs), tl(xs), fun, new) end def expand({x, y}, empty_rows, empty_cols, m \\ 2) do {x + delta(x, empty_cols) * (m - 1), y + delta(y, empty_rows) * (m - 1)} end defp delta(p, empty) do Enum.count(empty, &(p > &1)) end end ``` ``` {:module, Day11, <<70, 79, 82, 49, 0, 0, 12, ...>>, {:delta, 2}} ``` ## Part 1 ```elixir galaxies |> Enum.map(&Day11.expand(&1, empty_rows, empty_cols)) |> Day11.reduce_unique_pairs(0, fn {xa, ya}, {xb, yb}, acc -> acc + abs(xb - xa) + abs(yb - ya) end) ``` ``` 9686930 ``` ## Part 2 ```elixir galaxies |> Enum.map(&Day11.expand(&1, empty_rows, empty_cols, 1_000_000)) |> Day11.reduce_unique_pairs(0, fn {xa, ya}, {xb, yb}, acc -> acc + abs(xb - xa) + abs(yb - ya) end) ``` ``` 630728425490 ```