Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

<!-- livebook:{"persist_outputs":true} --> # Advent of Code 2023 - Day 11 ## Sample ```elixir sample = """ ...#...... .......#.. #......... .......... ......#... .#........ .........# .......... .......#.. #...#..... """ ``` <!-- livebook:{"output":true} --> ``` "...#......\n.......#..\n#.........\n..........\n......#...\n.#........\n.........#\n..........\n.......#..\n#...#.....\n" ``` ## Input ```elixir input = """ ......................#..................#................................#............#..........................#......................... ..........#....................................#.................#.......................................................................... ................................#......................#..........................#.....................#................#.................. ...........................#....................................................................#........................................#.. .....#..........#...................................................................................................................#....... ........................................................................................#................................................... ........................................#............#...................................................................................... .....................#....................................................#...................#.......................#..................... ..............................................................................................................................#...........#. ............................#.......#......................#................................................................................ ...........#......................................#..............#............#............................................................. .........................................................................................#.................................................. ....#...............................................................................#.....................................#...........#..... ..................#...........#...............#..............................................#.......#........#............................. ........................................#...........................#....................................................................... ............................................................................................................................................ .........................#..........................#.........................#.........................#................................... ................#...................#..................................................................................#...................# ...#...........................................................#........#.............#..............................................#...... ..................................................................................................#......................................... .............#..............................#............................................................................................... .....................#.........................................................................................#............................ ............................#........................#............#.........................#..................................#............ .......#..............................................................................................................#..................... ........................................................................#........#..................#....................................... ...#......................................#.......#......................................................#........#...............#......... ...........................................................#.............................................................................#.. ................................#..................................#........................................................................ .............#.................................#............................................................................................ #.......................#...............#.................................#...........#.........#...................................#....... ...........................................................................................#..................#............................. ........#....................#........................................................................................#......#.............. ............................................................#......................................#......................................#. ..#.......................................#................................................................................................. ....................#................................................................#....................................#........#........ .............#......................#................#......................................#............................................... .......#............................................................#................................................#...................... ..............................#.................#.................................................#......................................... .......................#...................................................................................#................................ ..................#..........................................#..................................................#..............#.....#...... ...................................................#...............................#.....#.............#.................................... ...#..........#.............#........#...................................................................................................... ............................................................................#..................#.............#...........................#.. ......................................................#..............#...................................................................... #......#....................................................................................................................#............... ..............................................#............................................................................................. ...................#................#...................................#.......................................................#..........# ...........#............#.................................#.................................................#...........#................... .....................................................................................#.....#........#............#.....................#.... ....................................................#..........#............................................................................ ............................................................................................................................................ .#.......#..................#..........#.............................#............#............#..........#................#..............#. ...............#.....#...................................................................#.................................................. .................................................#..........................#.........................................#........#.....#...... ......#..........................................................#...............................................#.......................... ............................................................................................................................................ .............................#................#.......................................#.....................#............................... ..................#..................#.......................#..........#.....................#.............................#............... ............................................................................................................................................ ..................................................................................#.................#...................#..........#........ ..................................#.....#..............#..........#.....................#................................................... .....#.................#.................................................................................................................... #......................................................................................................................................#.... ..................#..................#........#...........#..................#.................#............................................ .....................................................................#................................#.........#.............#............. ..........#..........................................................................#.....#................................................ ............................#.......................#...........#........................................................................... .#.................................#.............................................................#.................#........................ ............................................................................#................................#..........................#... ......#..................................................................................................................#.................. ...................#..............................#.................................................#.........................#............. ..............#................#.........................................................#..........................................#....... ............................................#....................#...............#.......................................................... .......................#................................................#.................................#................................. ..................................#.....#...........#...................................................................#................... .#.....................................................................................#........#........................................... .......#......................#................................#.......................................#.................................... ...............................................#............................................................................................ ..........................................................#................#.....................................#.......................... ......................#..................................................................................................................... ............................................#.........#.......................................#..................................#.......... .#.........#........................................................#...................................#.....#............................. .......................................#.............................................#..................................................#... ...............................#............................................................................................................ .............................................................................#..................#................#........#................. ......................#..................................................................................................................... ...#..............................................#.........#.....................#......................#...........................#...... ..................................#......................................................................................................... ...........................................................................#...............#...................................#............ ..........#..............#....................#............................................................................................. ...............................................................................................................#........................#... ....#.........#...............................................................#.....#....................................................... .......................................................#..........#.......................................................#................. ..................................#...............#..............................................................................#.......... ..............................................................#..........................#................#.........#....................... ........#............................................................#...................................................................... #...............................................................................................#........................................... ......................#....................................................................................................................# .....#......................#..............................................#...........#..........................#.....#................... ................#.................................................#..............#.......................................................... ...........#.........................................#...................................................................................... ..#..................................#.............................................................................................#.....#.. ..........................................#................................................#.....#........#................................. ..............#...............................................#.................................................#........................... ....................#.............#....................#..............................................#..................................... .............................#..........................................#................................................................... ...........................................................#................................................................................ .......................#.........................#.....................................#...........#........................................ .................................................................#............................................#.......#..................... ........#.....#............................................................#........................................................#....... .................................#...........................................................#.............................................. .#.................#......................#.................#.........#..................................#.......#........#................# ...........#................................................................................................................................ ........................#..............................#.............................#...................................................... ...................................#.....................................#.........................#.................................#...... ............................................................................................................................................ ....#..................................#.................................................#...............................#.................. .............................#....................#......................................................................................#.. .............................................................................#.........................#.................................... .............#.....#......................#.......................#.................#........................................#.............. ......................................................................................................................................#..... ...#.................................................#...........................................#.......................................... ......................#.....................................#............................................................................... ................#..........#..................................................................................#..........#.................. ..........#.........................#............#.................#..........#............................................................# ............................................................................................................................................ .......................................................#............................................................#....................... ...#.........................................#.....................................#...............#..............................#......... ............#.........................#..................................................................................................... .......................#...........................#.....................................................#.................................. ..............................................................#............................#...............................#..........#..... .........#......................#..........#.........................................#...................................................... ...............#................................................................................#.....#..................................... ....#.......................#..................#....................#......................................#...................#............ ..........................................................#................................................................................. .....................................#.........................#.........................#.................................................. #..........................................................................#................................................................ ...........................................#..................................................................#........#...........#........ ......................#...........................................................#...................#..................................... ....#...........#................................................................................#.......................................... """ ``` <!-- livebook:{"output":true} --> ``` "......................#..................#................................#............#..........................#.........................\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#......" <> ... ``` ## Part 1 ```elixir defmodule Part1 do def parse(text) do text |> String.split("\n", trim: true) |> Enum.with_index() |> Enum.reduce(%{}, fn {line, y}, acc -> line |> String.graphemes() |> Enum.with_index() |> Enum.reduce(acc, fn {cell, x}, acc -> Map.put(acc, {x, y}, cell) end) |> Map.merge(acc) end) end def distance({ax, ay}, {bx, by}, rows, cols) do dist = abs(bx - ax) + abs(by - ay) empty_cols = ax..bx |> Enum.filter(&(!MapSet.member?(cols, &1))) |> length empty_rows = ay..by |> Enum.filter(&(!MapSet.member?(rows, &1))) |> length dist + empty_cols + empty_rows end def find_galaxies(map) do map |> Map.filter(fn {_, v} -> v == "#" end) |> Enum.map(&elem(&1, 0)) end defp populated_n(galaxies, n) do galaxies |> Enum.map(&elem(&1, n)) |> MapSet.new() end def populated_columns(galaxies) do populated_n(galaxies, 0) end def populated_rows(galaxies) do populated_n(galaxies, 1) end def all_pair_distances(galaxies, rows, cols) do galaxies |> Enum.map(fn a -> galaxies |> Enum.filter(&(&1 > a)) |> Enum.map(fn b -> distance(a, b, rows, cols) end) |> Enum.sum() end) |> Enum.sum() end end map = Part1.parse(input) galaxies = Part1.find_galaxies(map) rows = Part1.populated_rows(galaxies) cols = Part1.populated_columns(galaxies) Part1.all_pair_distances(galaxies, rows, cols) ``` <!-- livebook:{"output":true} --> ``` 10033566 ``` ## Part 2 ```elixir defmodule Part2 do def parse(text) do text |> String.split("\n", trim: true) |> Enum.with_index() |> Enum.reduce(%{}, fn {line, y}, acc -> line |> String.graphemes() |> Enum.with_index() |> Enum.reduce(acc, fn {cell, x}, acc -> Map.put(acc, {x, y}, cell) end) |> Map.merge(acc) end) end def distance({ax, ay}, {bx, by}, rows, cols) do dist = abs(bx - ax) + abs(by - ay) empty_cols = ax..bx |> Enum.filter(&(!MapSet.member?(cols, &1))) |> length empty_rows = ay..by |> Enum.filter(&(!MapSet.member?(rows, &1))) |> length dist + empty_cols * 999_999 + empty_rows * 999_999 end def find_galaxies(map) do map |> Map.filter(fn {_, v} -> v == "#" end) |> Enum.map(&elem(&1, 0)) end defp populated_n(galaxies, n) do galaxies |> Enum.map(&elem(&1, n)) |> MapSet.new() end def populated_columns(galaxies) do populated_n(galaxies, 0) end def populated_rows(galaxies) do populated_n(galaxies, 1) end def all_pair_distances(galaxies, rows, cols) do galaxies |> Enum.map(fn a -> galaxies |> Enum.filter(&(&1 > a)) |> Enum.map(fn b -> distance(a, b, rows, cols) end) |> Enum.sum() end) |> Enum.sum() end end map = Part2.parse(input) galaxies = Part2.find_galaxies(map) rows = Part2.populated_rows(galaxies) cols = Part2.populated_columns(galaxies) Part2.all_pair_distances(galaxies, rows, cols) ``` <!-- livebook:{"output":true} --> ``` 560822911938 ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run on your machine

with Livebook Desktop

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More ×