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 03 ```elixir Mix.install([ {:kino, "~> 0.14"}, {: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 defmodule AOC do def parse(input) do input |> Kino.Input.read() |> Parser.parse() |> case do {:ok, acc, "", _, _, _} -> acc end end end defmodule Parser do import NimbleParsec disable = ignore(string("don't()")) |> tag(:disable) enable = ignore(string("do()")) |> tag(:enable) operand = integer(max: 3, min: 1) mul = ignore(string("mul(")) |> concat(operand) |> ignore(string(",")) |> concat(operand) |> ignore(string(")")) |> tag(:mul) instruction = choice([disable, enable, mul]) instructions = eventually(instruction) |> repeat() defparsec(:parse, instructions |> eventually(eos())) end ``` ## Part 1 <!-- livebook:{"reevaluate_automatically":true} --> ```elixir defmodule Part1 do def process(instructions) do instructions |> Enum.reduce(0, fn {:mul, [a, b]}, acc -> acc + a * b _, acc -> acc end) end end real_input |> AOC.parse() |> Part1.process() ``` ## Part 2 <!-- livebook:{"reevaluate_automatically":true} --> ```elixir defmodule Part2 do def process(instructions) do {_, sum} = instructions |> Enum.reduce({:enabled, 0}, fn {:mul, [a, b]}, {:enabled, sum} -> {:enabled, sum + a * b} {:disable, _}, {:enabled, sum} -> {:disabled, sum} {:enable, _}, {:disabled, sum} -> {:enabled, sum} _, acc -> acc end) sum end end real_input |> AOC.parse() |> Part2.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 ×