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)

# Day 01 ```elixir Mix.install([ {:kino, "~> 0.11"}, {:nimble_parsec, "~> 1.4"} ]) example_input = Kino.Input.textarea("example input:") |> Kino.render() real_input = Kino.Input.textarea("real input:") ``` ## Common <!-- livebook:{"reevaluate_automatically":true} --> ```elixir numbers = ~c"0123456789" filter = &(&1 in numbers) parse = fn input -> input |> Kino.Input.read() |> String.split("\n", trim: true) end process = fn input -> input |> Enum.map(&(to_charlist(&1) |> Enum.filter(filter))) |> Enum.map(&[hd(&1), List.last(&1)]) |> Enum.map(&List.to_integer/1) |> Enum.sum() end ``` ## Part 1 <!-- livebook:{"reevaluate_automatically":true} --> ```elixir real_input |> then(parse) |> then(process) ``` ## Part 2 <!-- livebook:{"reevaluate_automatically":true} --> ```elixir defmodule Replacer do import NimbleParsec replacements = for {<<head::binary-size(1), rest::binary>>, numeral} <- Enum.with_index(~w[one two three four five six seven eight nine], 1) do numeral = to_string(numeral) choice([ string(numeral), string(head) |> lookahead(string(rest)) |> replace(numeral) ]) end ignored = ignore(utf8_string([], 1)) defparsec(:replace, repeat(choice(replacements ++ [ignored]))) end real_input |> then(parse) |> Enum.map( &case Replacer.replace(&1) do {:ok, found, "", _, _, _} -> Enum.join(found) end ) |> then(process) ```
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 ×