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 5: Print Queue ```elixir Mix.install([:kino]) input = Kino.Input.textarea("Please paste your input:") ``` ## Part 1 [![Run in Livebook](https://livebook.dev/badge/v1/blue.svg)](https://livebook.dev/run?url=https%3A%2F%2Fgithub.com%2Fnallwhy%2Fadvent-of-code%2Fblob%2Fmain%2F2024%2Fday_05.livemd) https://adventofcode.com/2024/day/5 ```elixir data = input |> Kino.Input.read() |> String.split("\n\n", trim: true) |> Enum.map(& &1 |> String.split("\n", trim: true)) ``` ```elixir [rule_data, update_data] = data rules = rule_data |> Enum.map(fn str -> str |> String.split("|") |> Enum.map(&String.to_integer/1) end) updates = update_data |> Enum.map(fn str -> str |> String.split(",") |> Enum.map(&String.to_integer/1) end) rule_af_bfs_map = rules |> Enum.group_by(fn [_bf, af] -> af end, fn [bf, _af] -> bf end) do_check_rule_fun = fn _updated, [] = _queue, _recur_fun -> true updated, [h | t] = queue, recur_fun -> bfs = rule_af_bfs_map |> Map.get(h, []) case bfs |> Enum.any?(&(&1 in queue)) do true -> false _ -> recur_fun.(updated ++ [h], t, recur_fun) end end check_rule_fun = fn update -> do_check_rule_fun.([], update, do_check_rule_fun) end get_mid_fun = fn update -> mid_index = div(Enum.count(update), 2) update |> Enum.at(mid_index) end _answer = updates |> Enum.filter(check_rule_fun) |> Enum.map(get_mid_fun) |> Enum.sum() ``` ## Part 2 https://adventofcode.com/2024/day/5#part2 ```elixir do_correct_update_fun = fn [], acc, _recur_fun -> acc remains, acc, recur_fun -> IO.inspect({remains, acc}, charlists: :as_lists) h = remains |> Enum.find(fn remain -> bfs = rule_af_bfs_map |> Map.get(remain, []) bfs -- remains == bfs end) recur_fun.(remains -- [h], [h | acc], recur_fun) end correct_update_fun = fn update -> do_correct_update_fun.(update, [], do_correct_update_fun) end _answer = updates |> Enum.reject(check_rule_fun) |> Enum.map(correct_update_fun) |> Enum.map(get_mid_fun) |> 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 ×