Skip to content

Instantly share code, notes, and snippets.

@tomnis
Created December 24, 2018 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomnis/1b799d1def78bccf86f22e4727703592 to your computer and use it in GitHub Desktop.
Save tomnis/1b799d1def78bccf86f22e4727703592 to your computer and use it in GitHub Desktop.
package com.mccandless
import org.scalatest.{FlatSpec, FunSpec, Matchers}
import scala.collection.mutable
import Day16Spec.registers
class Day16Spec extends FlatSpec with Matchers {
def countBehavesLike(before: Seq[Int], code: Seq[Int], after: Seq[Int]): Int = {
0
}
"day 16" should "solve" in {
println("hello")
}
}
object Day16Spec {
val registers: mutable.Seq[Int] = mutable.Seq(0, 0, 0, 0)
}
sealed trait Op {
val inputA: Int
val inputB: Int
val outputC: Int
def eval: Unit = registers(outputC) = this.result
def result: Int
def op(a: Int, b: Int): Int
}
sealed trait RegisterOp extends Op {
override def result: Int = op(registers(this.inputA), registers(this.inputB))
}
sealed trait ImmediateOp extends Op {
override def result: Int = op(registers(this.inputA), this.inputB)
}
sealed trait AddOp extends Op {
override def op(a: Int, b: Int): Int = a + b
}
sealed trait MultOp extends Op {
override def op(a: Int, b: Int): Int = a * b
}
sealed trait AndOp extends Op {
override def op(a: Int, b: Int): Int = a & b
}
sealed trait OrOp extends Op {
override def op(a: Int, b: Int): Int = a | b
}
sealed trait AssOp extends Op {
// b is ignored
override def op(a: Int, b: Int): Int = a
}
sealed trait GtOp extends Op {
override def op(a: Int, b: Int): Int = if (a > b) 1 else 0
}
sealed trait EqOp extends Op {
override def op(a: Int, b: Int): Int = if (a == b) 1 else 0
}
// add register and immediate
case class addr(inputA: Int, inputB: Int, outputC: Int) extends RegisterOp with AddOp
case class addi(inputA: Int, inputB: Int, outputC: Int) extends ImmediateOp with AddOp
// mult register and immediate
case class mulr(inputA: Int, inputB: Int, outputC: Int) extends RegisterOp with MultOp
case class muli(inputA: Int, inputB: Int, outputC: Int) extends ImmediateOp with MultOp
// bitwise and
case class banr(inputA: Int, inputB: Int, outputC: Int) extends RegisterOp with AndOp
case class bani(inputA: Int, inputB: Int, outputC: Int) extends ImmediateOp with AndOp
// bitwise or
case class borr(inputA: Int, inputB: Int, outputC: Int) extends RegisterOp with OrOp
case class bori(inputA: Int, inputB: Int, outputC: Int) extends ImmediateOp with OrOp
// assignment (input B is ignored
case class setr(inputA: Int, inputB: Int, outputC: Int) extends AssOp {
override def result: Int = op(registers(this.inputA), -1)
}
case class seti(inputA: Int, inputB: Int, outputC: Int) extends AssOp {
override def result: Int = op(this.inputA, -1)
}
// greater than testing
case class gtir(inputA: Int, inputB: Int, outputC: Int) extends GtOp {
override def result: Int = op(inputA, registers(inputB))
}
case class gtri(inputA: Int, inputB: Int, outputC: Int) extends GtOp {
override def result: Int = op(registers(inputA), inputB)
}
case class gtrr(inputA: Int, inputB: Int, outputC: Int) extends GtOp {
override def result: Int = op(registers(inputA), registers(inputB))
}
// eq testing
case class eqir(inputA: Int, inputB: Int, outputC: Int) extends EqOp {
override def result: Int = op(inputA, registers(inputB))
}
case class eqri(inputA: Int, inputB: Int, outputC: Int) extends EqOp {
override def result: Int = op(registers(inputA), inputB)
}
case class eqrr(inputA: Int, inputB: Int, outputC: Int) extends EqOp {
override def result: Int = op(registers(inputA), registers(inputB))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment