From 237014dec82f454cc98b51ed2f9abb48231ff4af Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Sun, 22 Dec 2024 20:07:09 +0900 Subject: [PATCH] Efficiency improvements for 2024 Day 22 Part 2 --- 2024/day22.exs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/2024/day22.exs b/2024/day22.exs index 47969ce..ca0c2da 100755 --- a/2024/day22.exs +++ b/2024/day22.exs @@ -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