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 4: Camp Cleanup ```elixir Mix.install([ {:benchee, "~> 1.1"}, {:benchee_html, "~> 1.0"} ]) ``` <!-- livebook:{"output":true} --> ``` :ok ``` ## Story Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs. However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input). For example, consider the following list of section assignment pairs: ``` 2-4,6-8 2-3,4-5 5-7,7-9 2-8,3-7 6-6,4-6 2-6,4-8 ``` For the first few pairs, this list means: * Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8). * The Elves in the second pair were each assigned two sections. * The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9. This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this: ``` .234..... 2-4 .....678. 6-8 .23...... 2-3 ...45.... 4-5 ....567.. 5-7 ......789 7-9 .2345678. 2-8 ..34567.. 3-7 .....6... 6-6 ...456... 4-6 .23456... 2-6 ...45678. 4-8 ``` Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are **2** such pairs. In how many assignment pairs does one range fully contain the other? ```elixir defmodule Advent1 do def solve do File.stream!("#{File.cwd!()}/input/4.txt") |> Stream.map(&String.trim/1) |> Stream.map(fn x -> String.split(x, ",") end) |> Stream.map(&split/1) |> Stream.map(&in_range/1) |> Enum.sum() end def split([left, right]) do (String.split(left, "-") ++ String.split(right, "-")) |> Stream.map(&String.to_integer/1) |> Enum.to_list() end def in_range([a, b, x, y]) when a <= x and b >= y, do: 1 def in_range([a, b, x, y]) when x <= a and y >= b, do: 1 def in_range(_), do: 0 end Advent1.solve() ``` <!-- livebook:{"output":true} --> ``` 560 ``` ## Part Two It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all. In the above example, the first two pairs (`2-4,6-8` and `2-3,4-5`) don't overlap, while the remaining four pairs (`5-7,7-9`, `2-8,3-7`, `6-6,4-6`, and `2-6,4-8`) do overlap: * `5-7,7-9` overlaps in a single section, `7`. * `2-8,3-7` overlaps all of the sections `3` through `7`. * `6-6,4-6` overlaps in a single section, `6`. * `2-6,4-8` overlaps in sections `4`, `5`, and `6`. So, in this example, the number of overlapping assignment pairs is 4. In how many assignment pairs do the ranges overlap? ```elixir defmodule Advent2 do def solve() do File.stream!("#{File.cwd!()}/input/4.txt") |> Stream.map(&String.trim/1) |> Stream.map(fn x -> String.split(x, ",") end) |> Stream.map(&split/1) |> Stream.map(&overlap?/1) |> Stream.filter(fn x -> x end) |> Enum.to_list() |> length() end def split([left, right]) do (String.split(left, "-") ++ String.split(right, "-")) |> Stream.map(&String.to_integer/1) |> Enum.to_list() end def overlap?([a, b, x, y]) do between?(a, x, b) or between?(a, y, b) or between?(x, a, y) or between?(x, b, y) end def between?(l, m, r) do l <= m and m <= r end def bool_to_int(true), do: 1 def bool_to_int(_), do: 0 end Advent2.solve() ``` <!-- livebook:{"output":true} --> ``` 839 ``` ## Benchmark ```elixir Benchee.run(%{ "Part 1" => &Advent1.solve/0, "Part 2" => &Advent2.solve/0 }) :ok ``` <!-- livebook:{"output":true} --> ``` Operating System: Linux CPU Information: Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz Number of Available Cores: 4 Available memory: 7.69 GB Elixir 1.14.0 Erlang 25.1.2 Benchmark suite executing with the following configuration: warmup: 2 s time: 5 s memory time: 0 ns reduction time: 0 ns parallel: 1 inputs: none specified Estimated total run time: 14 s Benchmarking Part 1 ... Benchmarking Part 2 ... Name ips average deviation median 99th % Part 1 284.59 3.51 ms ±23.62% 3.16 ms 6.47 ms Part 2 275.95 3.62 ms ±25.04% 3.23 ms 6.60 ms Comparison: Part 1 284.59 Part 2 275.95 - 1.03x slower +0.110 ms ``` <!-- livebook:{"output":true} --> ``` :ok ```
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 ×