Day 11, that took me forever
parent
76ab828e5b
commit
b1fc5a4790
@ -0,0 +1,59 @@
|
||||
defmodule Day11 do
|
||||
def run do
|
||||
stones =
|
||||
File.read!("data.txt")
|
||||
|> String.trim()
|
||||
|> String.split(" ")
|
||||
|> Enum.map(&String.to_integer/1)
|
||||
|
||||
total = 75
|
||||
|
||||
part1 =
|
||||
Enum.reduce(stones, %{stones: 0, map: %{}}, fn stone, acc ->
|
||||
{new_stones, new_map} = processStoneCounts(stone, total, acc.map)
|
||||
%{stones: acc.stones + new_stones, map: new_map}
|
||||
end)
|
||||
|
||||
dbg(part1)
|
||||
end
|
||||
|
||||
defp processStoneCounts(stone, steps_left, map) do
|
||||
if steps_left == 0 do
|
||||
{1, map}
|
||||
else
|
||||
key = {steps_left, stone}
|
||||
|
||||
if Map.has_key?(map, key) do
|
||||
{Map.get(map, key), map}
|
||||
else
|
||||
length = Integer.digits(stone) |> length
|
||||
|
||||
new_stones2 =
|
||||
case stone do
|
||||
0 ->
|
||||
[1]
|
||||
|
||||
s when rem(length, 2) == 0 ->
|
||||
Integer.digits(s)
|
||||
|> Enum.split(trunc(length / 2))
|
||||
|> Tuple.to_list()
|
||||
|> Enum.map(&Integer.undigits/1)
|
||||
|
||||
s ->
|
||||
[s * 2024]
|
||||
end
|
||||
|
||||
if length(new_stones2) == 2 do
|
||||
{left, map2} = processStoneCounts(hd(new_stones2), steps_left - 1, map)
|
||||
{right, map3} = processStoneCounts(Enum.at(new_stones2, 1), steps_left - 1, map2)
|
||||
{left + right, Map.put(map3, key, left + right)}
|
||||
else
|
||||
{new, map2} = processStoneCounts(hd(new_stones2), steps_left - 1, map)
|
||||
{new, Map.put(map2, key, new)}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Day11.run()
|
Loading…
Reference in New Issue