Use Livebook to open this notebook and explore new ideas.
It is easy to get started, on your machine or the cloud.
# 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