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:{"persist_outputs":true} --> # Day 2 ```elixir Mix.install([ {:kino, "~> 0.11.3"} ]) ``` ## Input ```elixir input = Kino.Input.textarea("Input") ``` ## Part 1 ```elixir example = """ Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green """ configuration = %{"red" => 12, "green" => 13, "blue" => 14} expected = [1, 2, 5] ``` <!-- livebook:{"output":true} --> ``` [1, 2, 5] ``` ```elixir defmodule Part1 do defp parse_line(line) do [_, rest] = String.split(line, ": ") for set <- String.split(rest, "; ") do for cube <- String.split(set, ", "), into: %{} do [count, color] = String.split(cube, " ") {color, String.to_integer(count)} end end end def parse(input) do input |> String.split("\n", trim: true) |> Enum.map(&parse_line/1) end def possible?(game, configuration) do Enum.all?(configuration, fn {color, count} -> (game[color] || 0) <= count end) end def run(input, configuration) do input |> parse() |> Enum.with_index(1) |> Enum.filter(fn {games, _} -> Enum.all?(games, &possible?(&1, configuration)) end) |> Enum.map(fn {_, day} -> day end) end end Part1.run(example, configuration) == expected ``` <!-- livebook:{"output":true} --> ``` true ``` ```elixir Kino.Input.read(input) |> Part1.run(configuration) |> Enum.sum() ``` <!-- livebook:{"output":true} --> ``` 2541 ``` ## Part 2 ```elixir expected = [48, 12, 1560, 630, 36] ``` <!-- livebook:{"output":true} --> ``` [48, 12, 1560, 630, 36] ``` ```elixir defmodule Part2 do def mincubes(game) do Enum.reduce(game, %{}, fn set, acc -> Enum.reduce(set, acc, fn {color, count}, acc -> Map.update(acc, color, count, &max(&1, count)) end) end) end def run(input) do for game <- Part1.parse(input) do game |> mincubes() |> Map.values() |> Enum.product() end end end Part2.run(example) ``` <!-- livebook:{"output":true} --> ``` [48, 12, 1560, 630, 36] ``` ```elixir Kino.Input.read(input) |> Part2.run() |> Enum.sum() ``` <!-- livebook:{"output":true} --> ``` 66016 ```
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 ×