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} --> # Untitled notebook ## Section ```elixir defmodule Advent1 do def solve do File.stream!("#{File.cwd!()}/input/14_example.txt") |> Stream.map(&String.trim/1) |> Stream.map(&String.split/1) |> extract_koords_cave() # |> IO.inspect(label: "cave coords") end # the cave is filled with paths of stones def extract_koords_cave(paths) do paths |> Stream.map(&Advent1.extract_coords_path/1) # |> IO.inspect(label: "path coords") |> Enum.reduce(&MapSet.union/2) end # one line of input describes one path def extract_coords_path(path) do path |> Stream.filter(fn x -> x != "->" end) |> Stream.map(fn x -> String.split(x, ",") end) |> Stream.map(fn [x, y] -> [String.to_integer(x), String.to_integer(y)] end) |> Stream.map(fn [x, y] -> {x, y} end) |> Stream.chunk_every(2, 1, :discard) # |> (Enum.to_list() |> IO.inspect(label: "edges")) |> Stream.map(&extract_coords_edge/1) # |> (Enum.to_list() |> IO.inspect(label: "edges coords")) |> Enum.reduce(&MapSet.union/2) end # one path is composed of one or more edges def extract_coords_edge(edge) do [node_start, node_end] = edge extract_coords_nodes(node_start, node_end) end # one edge is described by exactly two nodes: node_start, node_end def extract_coords_nodes({x1, same}, {x2, same}) do Enum.to_list(x2..x1) |> Stream.map(fn x -> {x, same} end) |> MapSet.new() end def extract_coords_nodes({same, y1}, {same, y2}) do Enum.to_list(y2..y1) |> Stream.map(fn y -> {same, y} end) |> MapSet.new() end end Advent1.solve() ``` <!-- livebook:{"output":true} --> ``` MapSet.new([ {494, 9}, {495, 9}, {496, 6}, {496, 9}, {497, 6}, {497, 9}, {498, 4}, {498, 5}, {498, 6}, {498, 9}, {499, 9}, {500, 9}, {501, 9}, {502, 4}, {502, 5}, {502, 6}, {502, 7}, {502, 8}, {502, 9}, {503, 4} ]) ```
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 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