lluque

AoC day 25

Dec 25th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.27 KB | None | 0 0
  1. import io.Source
  2. import collection.mutable
  3.  
  4. object day25 extends App {
  5.  
  6.   val registry = mutable.Map[Char, Int]('a' -> 0, 'b' -> 0, 'c' -> 0, 'd' -> 0)
  7.   val clock = mutable.ListBuffer[Int]()
  8.  
  9.   abstract class Instruction {
  10.     def exec: Int
  11.   }
  12.  
  13.   case class Jump(a: String, b: String) extends Instruction {
  14.     val _a: AnyVal = if (a.forall(_.isLetter)) a.head else a.toInt
  15.     val _b: AnyVal = if (b.forall(_.isLetter)) b.head else b.toInt
  16.     def this(cmd: Array[String]) = {
  17.       this(cmd.head.mkString, cmd.last.mkString)
  18.     }
  19.     def this(cmd: String) = {
  20.       this(cmd split ' ' drop 1)
  21.     }
  22.     override def toString = "jnz " + _a + " " + _b
  23.     override def exec = _a match {
  24.       case i: Int => if (i > 0) _b match {
  25.         case j: Int => j
  26.         case j: Char => registry(j)
  27.       } else 1
  28.       case c: Char => if (registry(c) > 0) _b match {
  29.         case j: Int => j
  30.         case j: Char => registry(j)
  31.       } else 1
  32.     }
  33.   }
  34.  
  35.   case class Copy(a: String, b: Char) extends Instruction {
  36.     val _a: AnyVal = if (a.forall(_.isLetter)) a.head else a.toInt
  37.     def this(cmd: String) = {
  38.       this((cmd split ' ' slice (1, 2)).mkString, cmd.split(' ').last.head)
  39.     }
  40.     override def toString = "cpy " + _a + " " + b
  41.     override def exec = _a match {
  42.       case i: Int =>
  43.         if (registry.keySet.contains(b)) registry(b) = i
  44.         1
  45.       case c: Char =>
  46.         if (registry.keySet.contains(b) && registry.keySet.contains(c)) registry(b) = registry(c)
  47.         1
  48.     }
  49.   }
  50.  
  51.   case class Inc(a: Char) extends Instruction {
  52.     def this(cmd: String) {
  53.       this(cmd.last)
  54.     }
  55.     override def toString = "inc " + a
  56.     override def exec = {
  57.       registry(a) += 1
  58.       1
  59.     }
  60.   }
  61.  
  62.   case class Dec(a: Char) extends Instruction {
  63.     def this(cmd: String) {
  64.       this(cmd.last)
  65.     }
  66.     override def toString = "dec " + a
  67.     override def exec = {
  68.       registry(a) -= 1
  69.       1
  70.     }
  71.   }
  72.  
  73.   case class Out(a: Char) extends Instruction {
  74.     def this(cmd: String) = this(cmd.last)
  75.     override def toString = "out " + a
  76.     override def exec = {
  77.       val i = registry(a)
  78.       (clock.toList.reverse, i) match {
  79.         case (Nil, 0) => clock += 0; 1
  80.         case (1 :: t, 0) => clock += 0; 1
  81.         case (0 :: t, 1) => clock += 1; 1
  82.         case _ => 0
  83.       }
  84.     }
  85.   }
  86.  
  87.   def parse(xs: String): Instruction = xs match {
  88.     case s if s.startsWith("jnz") => new Jump(s)
  89.     case s if s.startsWith("cpy") => new Copy(s)
  90.     case s if s.startsWith("inc") => new Inc(s)
  91.     case s if s.startsWith("dec") => new Dec(s)
  92.     case s if s.startsWith("out") => new Out(s)
  93.   }
  94.  
  95.   def exec(a: Int, instructions: List[Instruction]): Boolean = {
  96.     registry('a') = a
  97.     registry('b') = 0
  98.     registry('c') = 0
  99.     registry('d') = 0
  100.     clock.clear
  101.     def inner(p: Int): Boolean = p match {
  102.       case i if i >= instructions.length => false
  103.       case i =>
  104.         if (clock.length > 100) true
  105.         else {
  106.           val incr = instructions(i).exec
  107.           if (incr != 0) inner(p + incr) else false
  108.         }
  109.       }
  110.     inner(0)
  111.   }
  112.  
  113.  
  114.   def input = Source.fromFile("day25.txt").getLines.toList
  115.   val instructions = input.map(parse)
  116.   var a = 1
  117.   while (!exec(a, instructions)) a += 1
  118.   println(a)
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment