Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions day1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ defmodule Day1 do

def floor(instructions), do: step(instructions, 0)

defp step("", floor), do: floor
defp step("(" <> instructions, floor), do: step(instructions, floor + 1)
defp step(")" <> instructions, floor), do: step(instructions, floor - 1)
defp step([], floor), do: floor
defp step( [?( | instructions], floor), do: step(instructions, floor + 1)
defp step( [?) | instructions], floor), do: step(instructions, floor - 1)

def basement(instructions), do: step_basement(instructions, 0, 1)

defp step_basement("", _, _), do: 0
defp step_basement(")" <> _, 0, position), do: position
defp step_basement("(" <> instructions, floor, position), do: step_basement(instructions, floor + 1, position + 1)
defp step_basement(")" <> instructions, floor, position), do: step_basement(instructions, floor - 1, position + 1)
defp step_basement([], _, _), do: 0
defp step_basement([?) | _ ], 0, position), do: position
defp step_basement([?( | instructions], floor, position), do: step_basement(instructions, floor + 1, position + 1)
defp step_basement([?) | instructions], floor, position), do: step_basement(instructions, floor - 1, position + 1)

end

Expand All @@ -47,35 +47,37 @@ defmodule Day1Test do
use ExUnit.Case, async: true

test "floor" do
assert 0 == Day1.floor("(())")
assert 0 == Day1.floor("()()")
assert 0 == Day1.floor('(())')
assert 0 == Day1.floor('()()')

assert 3 == Day1.floor("(((")
assert 3 == Day1.floor("(()(()(")
assert 3 == Day1.floor("))(((((")
assert 3 == Day1.floor('(((')
assert 3 == Day1.floor('(()(()(')
assert 3 == Day1.floor('))(((((')

assert -1 == Day1.floor("())")
assert -1 == Day1.floor("))(")
assert -1 == Day1.floor('())')
assert -1 == Day1.floor('))(')

assert -3 == Day1.floor(")))")
assert -3 == Day1.floor(")())())")
assert -3 == Day1.floor(')))')
assert -3 == Day1.floor(')())())')
end

test "basement" do
assert 1 == Day1.basement(")")
assert 5 == Day1.basement("()())")
assert 1 == Day1.basement(')')
assert 5 == Day1.basement('()())')
end

test "input floor" do
IO.puts "floor"
File.read!("day1.txt")
|> to_char_list
|> Day1.floor
|> IO.puts
end

test "input basement" do
IO.puts "basement"
File.read!("day1.txt")
|> to_char_list
|> Day1.basement
|> IO.puts
end
Expand Down