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)

# Advent 2024 - Day 11 ```elixir Mix.install([ {:kino, "~> 0.14.2"} ]) ``` ## Setup ```elixir input = Kino.Input.textarea("Please paste your input file") ``` ```elixir input = input |> Kino.Input.read() |> String.split(" ") |> Enum.map(&(String.to_integer/1)) ``` ## Part 1 ```elixir 1..25 |> Enum.reduce(input, fn _, acc -> for stone <- acc do if stone == 0 do [1] else stone_as_text = Integer.to_string(stone) stone_length = String.length(stone_as_text) if rem(stone_length, 2) == 0 do {a, b} = String.split_at(stone_as_text, div(stone_length, 2)) [ String.to_integer(a), String.to_integer(b) ] else stone * 2024 end end end |> List.flatten() end) |> Enum.count() ``` ## Part 2 ```elixir input = Enum.frequencies(input) update_count = fn map, stone, count -> map |> Map.put(stone, Map.get(map, stone, 0)) |> Map.update!(stone, fn current_count -> current_count + count end) end 1..75 |> Enum.reduce(input, fn _, acc -> Enum.reduce(acc, %{}, fn {stone, count}, acc -> if stone == 0 do update_count.(acc, 1, count) else stone_as_text = Integer.to_string(stone) stone_length = String.length(stone_as_text) if rem(stone_length, 2) == 0 do {a, b} = String.split_at(stone_as_text, div(stone_length, 2)) acc = update_count.(acc, String.to_integer(a), count) update_count.(acc, String.to_integer(b), count) else update_count.(acc, stone * 2024, count) end end end) end) |> Map.values() |> Enum.sum() ```
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 ×