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)

# Advent of Code - Day 14 ```elixir Mix.install( [ {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}, :kino_vega_lite ], force: true ) alias VegaLite, as: Vl ``` ## Section <!-- livebook:{"attrs":{"day":"14","session_secret":"SESSION","variable":"puzzle_input","year":"2022"},"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} --> ```elixir {:ok, puzzle_input} = KinoAOC.download_puzzle("2022", "14", System.fetch_env!("LB_SESSION")) ``` ```elixir input = """ 498,4 -> 498,6 -> 496,6 503,4 -> 502,4 -> 502,9 -> 494,9 """ ``` ```elixir defmodule Day14 do defstruct rocks_and_sand: [], bottom: nil, count_sand: 0, infinite_bottom: false, sand: MapSet.new(), rocks: [] def parse(input) do rocks = input |> String.split("\n", trim: true) |> Stream.flat_map(fn s -> String.split(s, " -> ") |> Stream.map(fn e -> e |> String.split(",") |> Enum.map(&String.to_integer/1) end) |> Stream.chunk_every(2, 1, :discard) |> Stream.flat_map(fn [[x, y1], [x, y2]] -> Stream.map(y1..y2, &{x, &1}) [[x1, y], [x2, y]] -> Stream.map(x1..x2, &{&1, y}) end) end) |> MapSet.new() %__MODULE__{ rocks_and_sand: rocks, rocks: rocks, bottom: rocks |> Enum.max_by(&elem(&1, 1)) |> elem(1) } end def make_sand_fall(state, pos \\ nil) def make_sand_fall(state, nil) do Stream.repeatedly(fn -> {500, 0} end) |> Enum.reduce_while( state, fn pos, state -> case make_sand_fall(state, pos) do :halt -> {:halt, state} {_x, y} = sand -> { if y == 0 do :halt else :cont end, state |> update_in([Access.key!(:rocks_and_sand)], &MapSet.put(&1, sand)) |> update_in([Access.key!(:sand)], &MapSet.put(&1, sand)) |> update_in([Access.key!(:count_sand)], &(&1 + 1)) } end end ) # |> Map.get(:count_sand) end def make_sand_fall(%__MODULE__{bottom: bottom}, {_x, y}) when y > bottom do :halt end def make_sand_fall(state, {x, y} = pos) do case Enum.find([{x, y + 1}, {x - 1, y + 1}, {x + 1, y + 1}], &(not blocked(state, &1))) do nil -> pos new_pos -> make_sand_fall(state, new_pos) end end defp blocked(state, pos = {_x, y}), do: (state.infinite_bottom and y == state.bottom) or MapSet.member?(state.rocks_and_sand, pos) def part1(input) do Day14.parse(input) |> make_sand_fall() end def part2(input) do Day14.parse(input) |> Map.update!(:bottom, &(&1 + 2)) |> Map.put(:infinite_bottom, true) |> make_sand_fall() end end state = Day14.part2(puzzle_input) Map.get(state, :count_sand) ``` ```elixir path_chart = Vl.new(width: 800, height: 1200) |> Vl.layers([ Vl.new() |> Vl.data_from_values( state.rocks |> Enum.map(fn {x, y} -> %{"x" => x, "y" => y, "h" => 1} end) ) |> Vl.mark(:rect, fill: :gray, stroke: :gray) |> Vl.encode_field(:x, "x", type: :nominal, axis: nil) |> Vl.encode_field(:y, "y", type: :nominal, axis: nil) |> Vl.encode(:color, field: "h", type: :quantitative), Vl.new() |> Vl.data_from_values( state.sand |> Enum.map(fn {x, y} -> %{"x" => x, "y" => y, "h" => 2} end) ) |> Vl.mark(:circle, size: 4, fill: :yellow) |> Vl.encode_field(:x, "x", type: :nominal, axis: nil) |> Vl.encode_field(:y, "y", type: :nominal, axis: nil) |> Vl.encode(:color, field: "h", type: :quantitative, legend: nil) ]) |> Vl.config(view: [stroke: nil, fill: :black]) |> Kino.VegaLite.new() |> Kino.render() :ok ```
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