MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/3uyl7s/daily_programming_puzzles_at_advent_of_code/cxj7t1a/?context=3
r/programming • u/Aneurysm9 • Dec 01 '15
179 comments sorted by
View all comments
Show parent comment
1
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)
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: