Created
December 9, 2017 18:31
-
-
Save ynonp/527f7bcfbfea96977e360852d022138a 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
defmodule Day9 do | |
def remove_bangs(line) do | |
Regex.replace(~r{!.}, line, "") | |
end | |
def count_garbage(line) do | |
IO.puts("Garbage Chars: ") | |
Regex.scan(~r{<.*?>}, line) | |
|> Enum.flat_map(&(&1)) | |
|> Enum.reduce(0, fn(el, acc) -> acc + String.length(el) - 2 end) | |
|> IO.puts | |
line | |
end | |
def remove_garbage(line) do | |
Regex.replace(~r{<.*?>}, line, "") | |
end | |
def count_score("", level, score) do | |
level + score | |
end | |
def count_score(line, level, score) do | |
count_score( | |
String.slice(line,1,String.length(line)), | |
case String.at(line, 0) do | |
"{" -> level + 1 | |
"}" -> level - 1 | |
_ -> level | |
end, | |
case String.at(line, 0) do | |
"}" -> level + score | |
_ -> score | |
end | |
) | |
end | |
def count_score(line) do | |
count_score(line, 0, 0) | |
end | |
end | |
IO.stream(:stdio, :line) | |
|> Enum.map(&Day9.remove_bangs/1) | |
|> Enum.map(&Day9.count_garbage/1) | |
|> Enum.map(&Day9.remove_garbage/1) | |
|> Enum.map(&Day9.count_score/1) | |
|> IO.inspect(charlists: :as_lists) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment