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
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