Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.61 KB | None | 0 0
  1. class Computer1(val initMemory:Array[Long]) {
  2.  
  3.   var pc = 0
  4.   var relative = 0
  5.  
  6.   var memory:Array[Long] = initMemory
  7.   for (i <- 0 to 10000) memory = memory :+ 0.toLong
  8.   def process(input:Long):Long = {
  9.       while (pc < memory.length) {
  10.       var op = memory(pc) % 100
  11.       var mod1 = memory(pc)/100 % 10
  12.       var mod2 = memory(pc)/1000 % 10
  13.       var mod3 = (memory(pc)/10000) % 10
  14.       println(op + " - " + memory(pc) + " - " + relative)
  15.       op match {
  16.         case 1 => {
  17.           val (i,j,k) = getValues()
  18.           memory(k) = i+j
  19.           pc += 4
  20.         }
  21.         case 2 => {
  22.           val (i,j,k) = getValues()
  23.           memory(k) = i*j
  24.           pc += 4
  25.         }
  26.         case 3 => {
  27.           if (isPos(mod1)) memory(memory(pc+1).toInt + relative) = input
  28.           else if (immediate(mod1)) memory(pc+1) = input
  29.           else memory(memory(pc+1).toInt) = input
  30.           pc += 2
  31.         }
  32.         case 4 => {
  33.           val (i,j,k) = getValues(1)
  34.           println(i)
  35.           pc += 2
  36.         }
  37.         case 5 => {
  38.           val (i,j,k) = getValues(2)
  39.           if (i != 0) pc = j.toInt
  40.           else pc += 3
  41.         }
  42.         case 6 => {
  43.           val (i,j,k) = getValues(2)
  44.           if (i == 0) pc = j.toInt
  45.           else pc += 3
  46.         }
  47.         case 7 => {
  48.           val (i,j,k) = getValues()
  49.           memory(k) = if (i < j) 1 else 0
  50.           pc += 4
  51.         }
  52.         case 8 => {
  53.           val (i,j,k) = getValues()
  54.           memory(k) = if (i == j) 1 else 0
  55.           pc += 4
  56.         }
  57.         case 9 => {
  58.           val (i,j,k) = getValues(1)
  59.           relative += i.toInt
  60.           pc += 2
  61.         }
  62.         case 99 => {
  63.           return 0
  64.         }
  65.         case _ => {println("Unknown Opcode: " + op); pc+=4}
  66.       }
  67.     }
  68.     println("Got all the way")
  69.     return 0
  70.   }
  71.  
  72.   def getValues(l:Int = 3):(Long,Long,Int) = {
  73.     var modes = memory(pc).toString.substring(0,math.max(memory(pc).toString.length() - 2,0)).map(_.toInt -48).toArray
  74.     while(modes.length != 3) modes = 0 +: modes
  75.     modes.foreach(print)
  76.     val i = if (modes(0) == 2) memory(memory(pc+1).toInt + relative) else if (modes(0) == 1) memory(pc+1) else memory(memory(pc+1).toInt)
  77.     var j:Long = 0
  78.     if (l > 1) j = if (modes(1) == 2) memory(memory(pc+2).toInt + relative) else if (modes(1) == 1) memory(pc+2) else memory(memory(pc+2).toInt)
  79.     var k = 0
  80.     if (l > 2) k = if (modes(2) == 2) (memory(pc+3).toInt + relative) else if (modes(1) == 1) pc+3 else memory(pc+3).toInt
  81.     (i,j,k)
  82.   }
  83.  
  84.   def immediate(n:Long):Boolean = n == 1
  85.   def isPos(n:Long):Boolean = n == 2
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement