Sarat Chandra - Programming and Tech

Thoughts about Tech, Programming and Random things

Solving Advent of Code with Elixir -Day 1

Posted at — Dec 9, 2015

Maybe some of you know about Advent of code, but it’s a collection of small programming puzzles. I am also learning Elixir recently. So I wanted to just try the puzzles in Elixir. Each puzzle is unlocked each day, so here is the code for the first day.

defmodule Advent1 do
  def part1("(" <> rest, count) do
    part1(rest, count + 1)
  end

  def part1(")"<> rest, count) do
    part1(rest, count - 1)
  end

  def part1("", count), do: count

  def part2(_, -1, pos), do: pos - 1

  def part2("(" <> rest, count, pos) do
    part2(rest, count + 1, pos + 1)
  end

  def part2(")" <> rest, count, pos) do
    part2(rest, count - 1, pos + 1)
  end
end
comments powered by Disqus