Last active
December 9, 2016 06:41
-
-
Save Cheezmeister/416664c2405d474ee177110c9883d2af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decompress("") do | |
"" | |
end | |
def handle_repeat(input) do | |
re = ~r/(\d+)x(\d+)\)(.*)/ | |
[_, len, repeats, rest] = Regex.run re, input | |
{section, remainder} = rest |> String.split_at(String.to_integer(len)) | |
(section |> String.duplicate(String.to_integer repeats))<> decompress(remainder) | |
end | |
def decompress(<< c ::binary-size(1), rest ::binary >>) do | |
case c do | |
"(" -> handle_repeat(rest) | |
_ -> << c, decompress(rest) >> | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment