Commit 2f26ac96 authored by Nikolay Petrachkov's avatar Nikolay Petrachkov

Advent of code 2018 day 5 Performance improvement after Joses stream

parent 9e7a7640
......@@ -61,16 +61,15 @@ defmodule Polymer do
|> Stream.flat_map(&String.to_charlist/1)
end
def compute([h | t]), do: compute(t, h, [])
def compute([h | []], Nil, seen), do: compute([], Nil, seen ++ [h])
def compute([h | t], Nil, seen), do: compute(t, h, seen)
def compute([h | t]), do: compute(t, [h])
def compute([h | t], []), do: compute(t, [h])
def compute([h | t], prev, seen) do
case abs(h - prev) == 32 do
true -> compute(seen ++ t, Nil, [])
false -> compute([h | t], Nil, seen ++ [prev])
def compute([h | t], [hs | ts]) do
case abs(h - hs) == 32 do
true -> compute(t, ts)
false -> compute(t, [h | [hs | ts]])
end
end
def compute([], _prev, seen), do: seen
def compute([], seen), do: Enum.reverse(seen)
end
......@@ -5,15 +5,15 @@ defmodule PolymerTest do
assert Polymer.start1("test/test.txt") == 10
end
# test "computes final polymer" do
# assert Polymer.start1("data.txt") == 10250
# end
test "computes shortest polymer" do
assert Polymer.start2("test/test2.txt") == 4
test "computes final polymer" do
assert Polymer.start1("data.txt") == 10250
end
# test "computes final shortest polymer" do
# assert Polymer.start2("data.txt") == 6188
# test "computes shortest polymer" do
# assert Polymer.start2("test/test2.txt") == 4
# end
test "computes final shortest polymer" do
assert Polymer.start2("data.txt") == 6188
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment