Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 1: Not Quite Lisp

elixir/day_1_not_quite_lisp.livemd

Day 1: Not Quite Lisp

Section

{:ok, data} = File.read("/Users/eli/Desktop/input.txt")

data
|> String.graphemes()
|> Enum.reduce(0, fn char, acc ->
  case char do
    "(" -> acc + 1
    ")" -> acc - 1
  end
end)
280
defmodule Foo do
  def find_basement([_h | _t], floor, position) when floor < 0 do
    position - 1
  end

  def find_basement([h | t], floor, position) do
    case h do
      "(" -> find_basement(t, floor + 1, position + 1)
      ")" -> find_basement(t, floor - 1, position + 1)
    end
  end
end

data
|> String.graphemes()
|> Foo.find_basement(0, 1)
1797