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)

<!-- vim: syntax=markdown --> # Day 1 ## Setup * [Stream recording](https://www.twitch.tv/videos/1221981322) * [Stream summary](https://www.youtube.com/watch?v=mDxJjqx5-ns) ```elixir Mix.install([ {:kino, "~> 0.5.0"}, {:nx, "~> 0.1.0"} ]) ``` ```elixir input = Kino.Input.textarea("Please paste your input file:") ``` ## Part 1 <!-- livebook:{"reevaluate_automatically":true} --> ```elixir input |> Kino.Input.read() |> String.splitter("\n", trim: true) |> Stream.map(&String.to_integer/1) |> Stream.chunk_every(2, 1, :discard) |> Enum.count(fn [left, right] -> right > left end) ``` ## Part 2 ```elixir input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&String.to_integer/1) |> Enum.chunk_every(3, 1, :discard) |> Enum.chunk_every(2, 1, :discard) |> Enum.count(fn [[left, m1, m2], [m1, m2, right]] -> right > left end) ``` ## Part 1 - Nx ```elixir tensor = input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&String.to_integer/1) |> Nx.tensor() Nx.greater(tensor[1..-1//1], tensor[0..-2//1]) |> Nx.sum() |> Nx.to_scalar() ``` ## Part 2 - Nx ```elixir input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&String.to_integer/1) |> Nx.tensor() |> Nx.window_sum({3}) |> then(fn tensor -> Nx.greater(tensor[1..-1//1], tensor[0..-2//1]) end) |> Nx.sum() ``` ## Part 2 - Nx+defn ```elixir defmodule Day1 do import Nx.Defn defn window_sum(t) do t |> Nx.window_sum({3}) |> then(&(&1[1..-1//1] > &1[0..-2//1])) |> Nx.sum() end end input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&String.to_integer/1) |> Nx.tensor() |> Day1.window_sum() ``` ## Part 1 - recursion ```elixir defmodule Recursion do def recur([left, right | tail], acc) when right > left, do: recur([right | tail], acc + 1) def recur([_left, right | tail], acc), do: recur([right | tail], acc) def recur([_], acc), do: acc end input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&String.to_integer/1) |> Recursion.recur(0) ``` ```elixir defmodule Recursion do def recur([head | tail]), do: recur(tail, head, 0) def recur([head | tail], pivot, acc) when head > pivot, do: recur(tail, head, acc + 1) def recur([head | tail], _pivot, acc), do: recur(tail, head, acc) def recur([], _, acc), do: acc end input |> Kino.Input.read() |> String.split("\n", trim: true) |> Enum.map(&String.to_integer/1) |> Recursion.recur() ```
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 ×