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 2 ```elixir Mix.install([ {:kino, "~> 0.14.2"} ]) ``` ## Setup ```elixir input = Kino.Input.textarea("Please paste your input file") ``` ```elixir reports = input |> Kino.Input.read() |> String.split("\n") |> Enum.map(fn line -> line |> String.split(" ") |> Enum.map(&String.to_integer(&1)) end) ``` ## Part 1 ```elixir defmodule RedNoseReports do def process([a | [b | t]]) do if a - b > 0 do process([a | [b | t]], :up) else process([a | [b | t]], :down) end end def process([a | [b | t]], direction) do correct_direction = case direction do :up -> a - b > 0 :down -> a - b < 0 end diff = abs(b - a) if (diff > 3 or diff == 0) or not correct_direction do false else process([b | t], direction) end end def process(report, _direction) when is_list(report) and length(report) == 1 do true end end for report <- reports do RedNoseReports.process(report) end |> Enum.count(&(&1)) ``` ## Part 2 ```elixir for report <- reports do if RedNoseReports.process(report) do true else for index_to_remove <- 0..length(reports) do modified_report = report |> Enum.with_index() |> Enum.reject(fn {_value, index} -> index == index_to_remove end) |> Enum.map(fn {value, _index} -> value end) RedNoseReports.process(modified_report) end |> Enum.any? end end |> Enum.count(&(&1)) ```
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 ×