lluque

AoC day 19

Dec 28th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.77 KB | None | 0 0
  1. package day19
  2.  
  3. import collection.mutable.ArrayBuffer
  4.  
  5. object Prog extends App {
  6.  
  7.   def solveSimple(n: Int) = {
  8.     val value = n - Integer.highestOneBit(n);
  9.     2 * value + 1;
  10.   }
  11.  
  12.   def solve(n: Int) = {
  13.     val left = ArrayBuffer[Int]()
  14.     val right = ArrayBuffer[Int]()
  15.  
  16.     for (i <- 1 to n) {
  17.       if (i < (n / 2).toInt + 1) left += i
  18.       else i +=: right
  19.     }
  20.  
  21.     while (left.nonEmpty && right.nonEmpty) {
  22.       if (left.length > right.length) {
  23.         left.remove(left.length - 1)
  24.       } else {
  25.         right.remove(right.length - 1)
  26.       }
  27.       left.remove(0) +=: right
  28.       left += right.remove(right.length-1)
  29.     }
  30.  
  31.     if (left.nonEmpty) left.head else right.head
  32.   }
  33.  
  34.   println(solveSimple(3012210))
  35.   println(solve(3012210))
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment