2024/22 optimizations
This commit is contained in:
parent
eb433c390d
commit
fe538e867a
1 changed files with 53 additions and 22 deletions
|
@ -8,7 +8,8 @@
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
input = """
|
input = """
|
||||||
"""
|
""" |> String.split("\n", trim: true)
|
||||||
|
|> Enum.map(&String.to_integer/1)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Section
|
## Section
|
||||||
|
@ -17,13 +18,41 @@ input = """
|
||||||
defmodule MonkeyMarket do
|
defmodule MonkeyMarket do
|
||||||
@doc """
|
@doc """
|
||||||
|
|
||||||
|
## Example
|
||||||
|
iex> MonkeyMarket.enc_base19([-1,0,9,-9])
|
||||||
|
-9+10+20*(19+20*(10+20*(9)))
|
||||||
|
"""
|
||||||
|
def enc_base19(ints) do
|
||||||
|
ints
|
||||||
|
|> Enum.reduce(
|
||||||
|
0,
|
||||||
|
fn i, acc -> acc * 20 + i + 10 end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
## Example
|
||||||
|
iex> MonkeyMarket.dec_base19(-9+10+20*(19+20*(10+20*(9))))
|
||||||
|
[-1,0,9,-9]
|
||||||
|
"""
|
||||||
|
def dec_base19(i) do
|
||||||
|
dec_base19(i, [])
|
||||||
|
end
|
||||||
|
defp dec_base19(0, acc), do: acc
|
||||||
|
defp dec_base19(i, acc) do
|
||||||
|
dec_base19(div(i,20),[Integer.mod(i, 20)-10 | acc])
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
iex> MonkeyMarket.part2([1,2,3,2024])
|
iex> MonkeyMarket.part2([1,2,3,2024])
|
||||||
23
|
23
|
||||||
"""
|
"""
|
||||||
def part2(input) do
|
def part2(input) do
|
||||||
for m <- (
|
tasks = for j <- input do
|
||||||
for j <- input do
|
Task.async(fn ->
|
||||||
diffs = MonkeyMarket.next(j, 2000)
|
diffs = MonkeyMarket.next(j, 2000)
|
||||||
|> Stream.map(&rem(&1, 10))
|
|> Stream.map(&rem(&1, 10))
|
||||||
|> Enum.reverse()
|
|> Enum.reverse()
|
||||||
|
@ -33,12 +62,14 @@ defmodule MonkeyMarket do
|
||||||
for [_,_,_,{n,_}] = chunk <- diffs
|
for [_,_,_,{n,_}] = chunk <- diffs
|
||||||
|> Stream.chunk_every(4, 1, :discard),
|
|> Stream.chunk_every(4, 1, :discard),
|
||||||
chunk = chunk |> Enum.map(&(elem(&1, 1))),
|
chunk = chunk |> Enum.map(&(elem(&1, 1))),
|
||||||
|
|
||||||
reduce: %{} do
|
reduce: %{} do
|
||||||
acc2 ->
|
acc2 ->
|
||||||
acc2 |> Map.update(chunk, n, &(&1))
|
acc2 |> Map.put_new(chunk |> enc_base19, n)
|
||||||
end
|
end
|
||||||
end), reduce: %{} do
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
for m <- Task.await_many(tasks, :infinity), reduce: %{} do
|
||||||
acc -> Map.merge(
|
acc -> Map.merge(
|
||||||
m,
|
m,
|
||||||
acc,
|
acc,
|
||||||
|
@ -74,13 +105,13 @@ defmodule MonkeyMarket do
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
MonkeyMarket.part2(input)
|
||||||
|
```
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
Benchee.run(%{
|
Benchee.run(%{
|
||||||
part1: fn -> MonkeyMarket.part1(input) end
|
part1: fn -> MonkeyMarket.part1(input) end
|
||||||
})
|
})
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```elixir
|
|
||||||
MonkeyMarket.part2(input)
|
|
||||||
```
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue