Efficiency improvements for 2024 Day 22 Part 2
This commit is contained in:
parent
9b19247c8f
commit
237014dec8
1 changed files with 4 additions and 9 deletions
|
@ -22,11 +22,10 @@ defmodule Day22 do
|
|||
def part2(input) do
|
||||
input
|
||||
|> Enum.map(fn secret ->
|
||||
secret
|
||||
|> Stream.iterate(&next/1)
|
||||
|> Stream.map(&bananas/1)
|
||||
|> Stream.take(2000)
|
||||
|> Stream.chunk_every(5, 1, :discard)
|
||||
1..2000
|
||||
|> Enum.map_reduce(secret, fn _, secret -> {rem(secret, 10), next(secret)} end)
|
||||
|> elem(0)
|
||||
|> Enum.chunk_every(5, 1, :discard)
|
||||
|> Enum.reduce(%{}, fn [a, b, c, d, bananas], changes ->
|
||||
Map.put_new(changes, {b - a, c - b, d - c, bananas - d}, bananas)
|
||||
end)
|
||||
|
@ -36,10 +35,6 @@ defmodule Day22 do
|
|||
|> elem(1)
|
||||
end
|
||||
|
||||
def bananas(secret) do
|
||||
secret |> Integer.to_string() |> String.last() |> String.to_integer()
|
||||
end
|
||||
|
||||
def input do
|
||||
with [input_filename] <- System.argv(),
|
||||
{:ok, input} <- File.read(input_filename) do
|
||||
|
|
Loading…
Reference in a new issue