Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io.Source.stdin
- import scala.collection.mutable.Stack
- object cycle {
- def main(args: Array[String]): Unit = {
- val strings = stdin.getLines().toList
- val correct = strings
- .map { x => balance_stack(x.toList, new Stack[Char]) }
- .map { if(_) '1' else '0' }
- .toString()
- print(Integer.parseInt(correct, 2))
- }
- def balance_stack(chars: List[Char], stack: Stack[Char]): Boolean = {
- if(chars.isEmpty) stack.isEmpty
- else {
- chars.head match {
- case '(' | '[' | '{' => { stack.push(chars.head); balance_stack(chars.tail, stack) }
- case _ => {
- val top = stack.pop()
- chars.head match {
- case ')' => { if(top == '(') balance_stack(chars.tail, stack) else false }
- case '}' => { if(top == '{') balance_stack(chars.tail, stack) else false }
- case ']' => { if(top == '[') balance_stack(chars.tail, stack) else false }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement