1
0
Fork 0

solve 2024 day 2

This commit is contained in:
sloane 2024-12-02 08:35:32 -05:00
parent 4d1b02e916
commit 6f2bc5b30b
Signed by: sloanelybutsurely
SSH key fingerprint: SHA256:8SBnwhl+RY3oEyQxy1a9wByPzxWM0x+/Ejc+sIlY5qQ
3 changed files with 50 additions and 2 deletions

View file

@ -2,9 +2,10 @@
| S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| [1] | 2 | 3 | 4 | 5 | 6 | 7 |
| [1] | [2] | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | | | |
[1]: ./lib/2024/1.ex
[2]: ./lib/2024/2.ex

47
2024/lib/2024/2.ex Normal file
View file

@ -0,0 +1,47 @@
import AOC
aoc 2024, 2 do
def p1(input) do
input
|> list_of_lists_of_numbers()
|> Enum.count(&safe?/1)
end
def p2(input) do
input
|> list_of_lists_of_numbers()
|> Enum.count(&safe_with_dampener?(&1))
end
defp list_of_lists_of_numbers(input) do
input
|> String.split("\n")
|> Enum.map(fn line ->
line
|> String.split()
|> Enum.map(&String.to_integer/1)
end)
end
defp safe?(report) do
diffs = differences(report)
Enum.all?(diffs, &(1 <= &1 and &1 <= 3)) or
Enum.all?(diffs, &(-3 <= &1 and &1 <= -1))
end
def safe_with_dampener?(report) do
reports_with_a_single_level_removed =
for i <- 0..(length(report) - 1) do
{head, [_ | tail]} = Enum.split(report, i)
head ++ tail
end
safe?(report) or
Enum.any?(reports_with_a_single_level_removed, &safe?/1)
end
defp differences([_ | rest] = list) do
Enum.zip_with(list, rest, &(&1 - &2))
end
end

View file

@ -15,7 +15,7 @@
| [2021] | **43/50** 🌟 | Elixir |
| [2022] | **14/50** 🌟 | Elixir, Haskell |
| [2023] | **19/50** 🌟 | Elixir, Haskell |
| [2024] | **2/50** 🌟 | Elixir |
| [2024] | **4/50** 🌟 | Elixir |
[2015]: ./2015
[2017]: ./2017