solve 2023 9.1
This commit is contained in:
parent
44c277ff94
commit
c14a908541
4 changed files with 51 additions and 2 deletions
|
@ -3,7 +3,7 @@
|
|||
| S | M | T | W | T | F | S |
|
||||
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
||||
| | | | | | [1] | [2] |
|
||||
| [3] | [4] | [5] | 6 | [7] | [8] | 9 |
|
||||
| [3] | [4] | [5] | 6 | [7] | [8] | [9] |
|
||||
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|
||||
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
|
||||
| 24 | 25 | | | | | |
|
||||
|
@ -17,3 +17,4 @@
|
|||
<!-- [6]: ./lib/2023/6.ex -->
|
||||
[7]: ./lib/2023/7.ex
|
||||
[8]: ./lib/2023/8.ex
|
||||
[9]: ./lib/2023/9.ex
|
||||
|
|
31
2023/lib/2023/9.ex
Normal file
31
2023/lib/2023/9.ex
Normal file
|
@ -0,0 +1,31 @@
|
|||
import AOC
|
||||
import AOCHelpers
|
||||
|
||||
aoc 2023, 9 do
|
||||
def p1(input) do
|
||||
input
|
||||
|> lines_of_integers()
|
||||
|> Enum.map(&next_in_sequence/1)
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
def p2(_input) do
|
||||
end
|
||||
|
||||
def next_in_sequence(xs) do
|
||||
xs
|
||||
|> differentiate()
|
||||
|> Enum.map(&List.last/1)
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
def differentiate(xs) do
|
||||
xs
|
||||
|> Stream.iterate(&deltas/1)
|
||||
|> Enum.take_while(&(not all_zeros?(&1)))
|
||||
end
|
||||
|
||||
def deltas(xs), do: Enum.zip_with(xs, Enum.drop(xs, 1), &(&2 - &1))
|
||||
|
||||
def all_zeros?(xs), do: Enum.all?(xs, is?(0))
|
||||
end
|
|
@ -11,6 +11,22 @@ defmodule AOCHelpers do
|
|||
String.split(str, "", trim: true)
|
||||
end
|
||||
|
||||
def integers(str) when is_binary(str) do
|
||||
str
|
||||
|> words()
|
||||
|> integers()
|
||||
end
|
||||
|
||||
def integers(ws) when is_list(ws) do
|
||||
Enum.map(ws, &String.to_integer/1)
|
||||
end
|
||||
|
||||
def lines_of_integers(input) do
|
||||
input
|
||||
|> lines()
|
||||
|> Enum.map(&integers/1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Take a list of terms and a list of 1-arity functions and apply each function
|
||||
to the coresponding term in the list of terms.
|
||||
|
@ -27,4 +43,5 @@ defmodule AOCHelpers do
|
|||
|
||||
def id(x), do: x
|
||||
def always(x), do: fn -> x end
|
||||
def is?(x), do: &(&1 == x)
|
||||
end
|
|
@ -14,7 +14,7 @@
|
|||
1. [2020] **17/50** 🌟
|
||||
1. [2021] **43/50** 🌟
|
||||
1. [2022] **14/50** 🌟
|
||||
1. [2023] **13/50** 🌟
|
||||
1. [2023] **14/50** 🌟
|
||||
|
||||
[2015]: ./2015
|
||||
[2017]: ./2017
|
||||
|
|
Loading…
Reference in a new issue