r/programming • • Dec 01 '15

Daily programming puzzles at Advent of Code

http://adventofcode.com/
324 Upvotes

179 comments sorted by

View all comments

Show parent comment

1

u/GMTao Dec 01 '15

Nice! I was thinking there was an easier way to do this, but I just went with 'ol fashioned tail recursion:

    @tailrec
    def recurse(idx: Int, count: Int, s: String): Int = {
      if (count < 0) idx
      else {
        s.head match {
          case '(' => recurse(idx + 1, count + 1, s.tail)
          case ')' => recurse(idx + 1, count - 1, s.tail)
        }
      }
    }

    recurse(0, 0, input)