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)

<!-- vim: syntax=markdown --> # Day 10 ## Setup ```elixir Mix.install([ {:kino, "~> 0.4.1"} ]) ``` ## Input ```elixir input = Kino.Input.textarea("Please paste your input:") ``` ## NavigationSubsystem ```elixir defmodule NavigationSubsystem do @matching_characters %{ ?[ => ?], ?{ => ?}, ?( => ?), ?< => ?> } @opening_characters Map.keys(@matching_characters) @closing_characters Map.values(@matching_characters) @score_part_1 %{?) => 3, ?] => 57, ?} => 1197, ?> => 25137} @score_part_2 %{?) => 1, ?] => 2, ?} => 3, ?> => 4} defstruct stack: [], invalid_char: nil def new(line) do %__MODULE__{} |> add_line(line) end def complete?(%{stack: []}), do: true def complete?(_), do: false def valid?(%{invalid_char: nil}), do: true def valid?(_), do: false def score_part_1(ns) do @score_part_1[ns.invalid_char] end def score_part_2(ns) do ns.stack |> Enum.reduce(0, fn char, acc -> acc * 5 + @score_part_2[char] end) end defp add_line(ns, line) do line |> String.to_charlist() |> Enum.reduce(ns, &add_char(&2, &1)) end defp add_char(%{invalid_char: invalid_char} = ns, _char) when not is_nil(invalid_char), do: ns defp add_char(%{stack: [char | rest_stack]} = ns, char) when char in @closing_characters do %{ns | stack: rest_stack} end defp add_char(ns, char) when char in @closing_characters do %{ns | invalid_char: char} end defp add_char(ns, char) when char in @opening_characters do %{ns | stack: [Map.get(@matching_characters, char) | ns.stack]} end end ``` ## Part 1 ```elixir input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&NavigationSubsystem.new/1) |> Enum.reject(&NavigationSubsystem.valid?/1) |> Enum.map(&NavigationSubsystem.score_part_1/1) |> Enum.sum() ``` ## Part 2 ```elixir input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&NavigationSubsystem.new/1) |> Enum.filter(&NavigationSubsystem.valid?/1) |> Enum.reject(&NavigationSubsystem.complete?/1) |> Enum.map(&NavigationSubsystem.score_part_2/1) |> then(fn scores -> scores |> Enum.sort() |> Enum.at(div(length(scores), 2)) end) ```
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 ×