this repo has no description
1# Day 02
2
3```elixir
4Mix.install([:kino_aoc])
5```
6
7## Section
8
9<!-- livebook:{"attrs":"eyJhc3NpZ25fdG8iOiJwdXp6bGVfaW5wdXQiLCJkYXkiOiIyIiwic2Vzc2lvbl9zZWNyZXQiOiJBRFZFTlRfT0ZfQ09ERV9TRVNTSU9OIiwieWVhciI6IjIwMjQifQ","chunks":null,"kind":"Elixir.KinoAOC.HelperCell","livebook_object":"smart_cell"} -->
10
11```elixir
12{:ok, puzzle_input} =
13 KinoAOC.download_puzzle("2024", "2", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
14```
15
16```elixir
17inputs =
18 puzzle_input
19 |> String.split("\n", trim: true)
20 |> Enum.map(fn row ->
21 row
22 |> String.split(" ", trim: true)
23 |> Enum.map(&String.to_integer/1)
24 end)
25```
26
27```elixir
28defmodule Day02 do
29 def correct?([a, b | _] = input) do
30 cmp = if a > b, do: &>/2, else: &</2
31 valid? = fn [c, d] -> abs(c - d) in 1..3 and cmp.(c, d) end
32
33 input
34 |> Enum.chunk_every(2, 1, :discard)
35 |> Enum.all?(valid?)
36 end
37end
38```
39
40## Part 1
41
42```elixir
43Enum.count(inputs, &Day02.correct?/1)
44```
45
46## Part 2
47
48```elixir
49Enum.count(inputs, fn row ->
50 Enum.any?(0..length(row), fn idx ->
51 row |> List.delete_at(idx) |> Day02.correct?()
52 end)
53end)
54```
55
56<!-- livebook:{"offset":1095,"stamp":{"token":"XCP.EDLheuV1bFdj8dcA62THFYEnpGMMQhndPyDclGgiuluufI8nGaZSSck2Zkh1QsI2YzHSIIw7X4Z8X4n_sgLE0sPJr4ahqOV5-G9t1bfxRG4kZgo2wsY4xf7SvIl1diXFy1k","version":2}} -->