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)

<!-- livebook:{"file_entries":[{"name":"day3_1.txt","type":"attachment"}]} --> # Day 3 ```elixir Mix.install([ {:kino, "~> 0.13.0"} ]) ``` ## Part 1 ```elixir content = Kino.FS.file_path("day3_1.txt") |> File.read!() |> String.trim() ``` ```elixir defmodule MulParser do def scan_mul(input) do Regex.scan(~r/mul\(\d{1,3}[,]\d{1,3}\)/, input) |> List.flatten() # For each mul expression -> Get the two integers |> Enum.map(fn line -> Regex.scan(~r/\d{1,3}/, line) |> List.flatten() |> Enum.map(fn n -> String.to_integer(n) end) # Multiply the integers |> Enum.reduce(fn x, acc -> x * acc end) end) # Sum for result |> Enum.sum() end end MulParser.scan_mul(content) ``` ## Part 2 ```elixir # For this one we will use the same scan_mul in sections where it is enabled defmodule Conditional do def split_at_first_occurrence(string, delimiter) do # Split the string at the delimiter, with only 2 parts so that it splits at first occurence parts = String.split(string, delimiter, parts: 2) # Return the parts as a tuple case parts do [head, tail] -> {head, tail} # If the delimiter is not found, return the original string and an empty string _ -> {string, ""} end end def compute(input, result, computing) do case computing do :do -> # Split head and tail {head, tail} = Conditional.split_at_first_occurrence(input, "don't()") # Compute result, if there is nothing left, return result if tail == "" do MulParser.scan_mul(head) + result else # Else compute the tail as well compute(tail, MulParser.scan_mul(head) + result, :dont) end :dont -> # On don't case we just ignore the head and goto tail {_head, tail} = Conditional.split_at_first_occurrence(input, "do()") if tail == "" do result else compute(tail, result, :do) end end end end Conditional.compute(content, 0, :do) ```
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