Day Two
This commit is contained in:
parent
e15d83107b
commit
10eb1838c7
|
@ -0,0 +1,77 @@
|
|||
# Day02
|
||||
|
||||
```elixir
|
||||
Mix.install([
|
||||
{:kino, "~> 0.14.2"},
|
||||
])
|
||||
```
|
||||
|
||||
## Run Me
|
||||
|
||||
[](https://livebook.dev/run?url=https%3A%2F%2Fgit.kev.pub%2Fkev%2Faoc2024%2Fraw%2Fbranch%2Fmain%2Fday01.livemd)
|
||||
|
||||
## Input
|
||||
|
||||
```elixir
|
||||
input = Kino.Input.textarea("Input")
|
||||
```
|
||||
|
||||
## Part One
|
||||
|
||||
```elixir
|
||||
defmodule PartOne do
|
||||
def differences([_]), do: []
|
||||
def differences([a, b|t]), do: [a - b| differences([b|t])]
|
||||
|
||||
def parse(input) do
|
||||
input
|
||||
|> Kino.Input.read
|
||||
|> String.split("\n")
|
||||
|> Enum.map(fn l -> l |> String.split(" ")|> Enum.map(&String.to_integer/1) end)
|
||||
end
|
||||
|
||||
def pass_diffs(l) do
|
||||
fail = l
|
||||
|> PartOne.differences
|
||||
|> Enum.any?(fn y -> (abs(y) > 3 || y == 0) end)
|
||||
|
||||
!fail
|
||||
end
|
||||
|
||||
def pass_direction(l) do
|
||||
l =
|
||||
l
|
||||
|> PartOne.differences
|
||||
|> Enum.map(fn x -> x > 0 end)
|
||||
|> Enum.dedup
|
||||
|> length
|
||||
|
||||
l == 1
|
||||
end
|
||||
|
||||
def passes(x), do: pass_diffs(x) && pass_direction(x)
|
||||
end
|
||||
|
||||
input
|
||||
|> PartOne.parse
|
||||
|> Enum.count(&PartOne.passes/1)
|
||||
|
||||
```
|
||||
|
||||
## Part Two
|
||||
|
||||
```elixir
|
||||
%{false: fails, true: passes} =
|
||||
input
|
||||
|> PartOne.parse
|
||||
|> Enum.group_by(&PartOne.passes/1)
|
||||
|
||||
fail_count = Enum.map(fails, fn l ->
|
||||
0..length(l)
|
||||
|> Enum.map(fn x -> List.delete_at(l,x) end)
|
||||
|> Enum.any?(&PartOne.passes/1)
|
||||
end)
|
||||
|> Enum.count(fn x -> x end)
|
||||
|
||||
length(passes) + fail_count
|
||||
```
|
Loading…
Reference in New Issue