Efficiency improvements for 2024 Day 22 Part 2

This commit is contained in:
Adam Millerchip 2024-12-22 20:07:09 +09:00
parent 9b19247c8f
commit 237014dec8

View file

@ -22,11 +22,10 @@ defmodule Day22 do
def part2(input) do def part2(input) do
input input
|> Enum.map(fn secret -> |> Enum.map(fn secret ->
secret 1..2000
|> Stream.iterate(&next/1) |> Enum.map_reduce(secret, fn _, secret -> {rem(secret, 10), next(secret)} end)
|> Stream.map(&bananas/1) |> elem(0)
|> Stream.take(2000) |> Enum.chunk_every(5, 1, :discard)
|> Stream.chunk_every(5, 1, :discard)
|> Enum.reduce(%{}, fn [a, b, c, d, bananas], changes -> |> Enum.reduce(%{}, fn [a, b, c, d, bananas], changes ->
Map.put_new(changes, {b - a, c - b, d - c, bananas - d}, bananas) Map.put_new(changes, {b - a, c - b, d - c, bananas - d}, bananas)
end) end)
@ -36,10 +35,6 @@ defmodule Day22 do
|> elem(1) |> elem(1)
end end
def bananas(secret) do
secret |> Integer.to_string() |> String.last() |> String.to_integer()
end
def input do def input do
with [input_filename] <- System.argv(), with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do {:ok, input} <- File.read(input_filename) do