lluque

AoC day 23

Dec 23rd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.14 KB | None | 0 0
  1. package day23
  2.  
  3. import io.Source
  4. import collection.mutable
  5.  
  6. object day23 extends App {
  7.  
  8.   val registry = mutable.Map[Char, Int]('a' -> 0, 'b' -> 0, 'c' -> 0, 'd' -> 0)
  9.   val instructions = mutable.ListBuffer[Instruction]()
  10.  
  11.   abstract class Instruction {
  12.     def exec(args: AnyVal*): Int
  13.   }
  14.  
  15.   case class Jump(a: String, b: String) extends Instruction {
  16.     val _a: AnyVal = if (a.forall(_.isLetter)) a.head else a.toInt
  17.     val _b: AnyVal = if (b.forall(_.isLetter)) b.head else b.toInt
  18.     def this(cmd: Array[String]) = {
  19.       this(cmd.head.mkString, cmd.last.mkString)
  20.     }
  21.     def this(cmd: String) = {
  22.       this(cmd split ' ' drop 1)
  23.     }
  24.     override def toString = "jnz " + _a + " " + _b
  25.     override def exec(args: AnyVal*) = _a match {
  26.       case i: Int => if (i > 0) _b match {
  27.         case j: Int => j
  28.         case j: Char => registry(j)
  29.       } else 1
  30.       case c: Char => if (registry(c) > 0) _b match {
  31.         case j: Int => j
  32.         case j: Char => registry(j)
  33.       } else 1
  34.     }
  35.   }
  36.  
  37.   case class Copy(a: String, b: Char) extends Instruction {
  38.     val _a: AnyVal = if (a.forall(_.isLetter)) a.head else a.toInt
  39.     def this(cmd: String) = {
  40.       this((cmd split ' ' slice (1, 2)).mkString, cmd.split(' ').last.head)
  41.     }
  42.     override def toString = "cpy " + _a + " " + b
  43.     override def exec(args: AnyVal*) = _a match {
  44.       case i: Int =>
  45.         if (registry.keySet.contains(b)) registry(b) = i
  46.         1
  47.       case c: Char =>
  48.         if (registry.keySet.contains(b) && registry.keySet.contains(c)) registry(b) = registry(c)
  49.         1
  50.     }
  51.   }
  52.  
  53.   case class Inc(a: Char) extends Instruction {
  54.     def this(cmd: String) {
  55.       this(cmd.last)
  56.     }
  57.     override def toString = "inc " + a
  58.     override def exec(args: AnyVal*) = {
  59.       registry(a) += 1
  60.       1
  61.     }
  62.   }
  63.  
  64.   case class Dec(a: Char) extends Instruction {
  65.     def this(cmd: String) {
  66.       this(cmd.last)
  67.     }
  68.     override def toString = "dec " + a
  69.     override def exec(args: AnyVal*) = {
  70.       registry(a) -= 1
  71.       1
  72.     }
  73.   }
  74.  
  75.   case class Multi(a: Char, b: Char, c: Char) extends Instruction {
  76.     def this(cmd: Array[String]) {
  77.       this(cmd.head.head, cmd.slice(1,2).head.head, cmd.last.head)
  78.     }
  79.     def this(cmd: String) {
  80.       this(cmd split ' ' drop 1)
  81.     }
  82.     override def toString = "mlt " + a + " " + b + " " + c
  83.     override def exec(args: AnyVal*) = {
  84.       registry(c) = registry(a) * registry(b)
  85.       1
  86.     }
  87.   }
  88.  
  89.   class Nop extends Instruction {
  90.     override def exec(args: AnyVal*) = 1
  91.   }
  92.  
  93.   case class Toggle(a: Char) extends Instruction {
  94.     def this(cmd: String) {
  95.       this(cmd.last)
  96.     }
  97.     def toggleInstruction(x: Instruction): Instruction = x match {
  98.       case i: Inc => Dec(i.a)
  99.       case i: Dec => Inc(i.a)
  100.       case i: Toggle => Inc(i.a)
  101.       case i: Jump => Copy(i.a, i.b.head)
  102.       case i: Copy => Jump(i.a, i.b.toString)
  103.     }
  104.     override def toString = "tgl " + a
  105.     override def exec(args: AnyVal*) = args(0) match {
  106.       case cp: Int =>
  107.         val i = cp + registry(a)
  108.         if (0 <= i && i < instructions.length) {
  109.           instructions(i) = toggleInstruction(instructions(i))
  110.         }
  111.         1
  112.     }
  113.   }
  114.  
  115.   def parse(xs: String): Instruction = xs match {
  116.     case s if s.startsWith("jnz") => new Jump(s)
  117.     case s if s.startsWith("cpy") => new Copy(s)
  118.     case s if s.startsWith("inc") => new Inc(s)
  119.     case s if s.startsWith("dec") => new Dec(s)
  120.     case s if s.startsWith("tgl") => new Toggle(s)
  121.     case s if s.startsWith("mlt") => new Multi(s)
  122.     case s if s.startsWith("nop") => new Nop
  123.   }
  124.  
  125.   def exec(): Any = {
  126.     var p = 0
  127.     while (p < instructions.length) {
  128.       p += instructions(p).exec(p)
  129.     }
  130.   }
  131.  
  132.   def input = Source.fromFile("day23.txt").getLines.toList
  133.   instructions ++= input.map(parse)
  134.   // part 1
  135.   registry('a') = 7
  136.   exec
  137.   println(registry('a'))
  138.  
  139.   // part 2
  140.   def input2 = Source.fromFile("day23b.txt").getLines.toList
  141.   registry.keys.foreach(k => if (k == 'a') registry(k) = 12 else registry(k) = 0)
  142.   instructions.clear
  143.   instructions ++= input2.map(parse)
  144.   exec
  145.   println(registry('a'))
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment