Solution to part 1 of Day 7 Advent of Code in Scala Does someone want to review my solution to part 1 of Day 7 Advent of Code in Scala?
The assignment is here: http://adventofcode.com/day/7
Without repeating the whole text, in short:

Given a text file of the following form (simplified):

x AND y -&gt; a
x -&gt; 1
y -&gt; 0


calculate the value of a. Here is an example of a full input file.

My solution:

import scala.io.Source

object Day7 {

  sealed trait Expr

  case class Variable(name: String) extends Expr

  case class Const(value: Int) extends Expr

  case class Not(expr: Expr) extends Expr

  case class And(left: Expr, right: Expr) extends Expr

  case class Or(left: Expr, right: Expr) extends Expr

  case class LShift(expr: Expr, by: Int) extends Expr

  case class RShift(expr: Expr, by: Int) extends Expr

  case class Assignment(v: Variable, expr: Expr) extends Expr

  case object Command {
    def parse(s: String): Assignment = {
      val assignmentRegex = """(.+) -&gt; (.+)""".r
      def innerParse(s: String): Expr = {
        val rShiftRegex = """(\w+) RSHIFT (\d+)""".r
        val lShiftRegex = """(\w+) LSHIFT (\d+)""".r
        val orRegex = """(\w+) OR (\w+)""".r
        val andRegex = """(\w+) AND (\w+)""".r
        val notRegex = """NOT (\w+)""".r
        val constRegex = """(\d+)""".r
        val varRegex = """(\w+)""".r

        s match {
          case rShiftRegex(l, n) =&gt;
            RShift(innerParse(l), n.toInt)
          case lShiftRegex(l, n) =&gt;
            LShift(innerParse(l), n.toInt)
          case orRegex(l, r) =&gt;
            Or(innerParse(l), innerParse(r))
          case andRegex(l, r) =&gt;
            And(innerParse(l), innerParse(r))
          case notRegex(e) =&gt;
            Not(innerParse(e))
          case constRegex(n) =&gt;
            Const(n.toInt)
          case varRegex(n) =&gt;
            Variable(n)
        }
      }
      s match {
        case assignmentRegex(l, r) =&gt; Assignment(Variable(r), innerParse(l))
        case _ =&gt; throw new Exception(s"Unrecognized command: $s")
      }
    }
  }

  class Environment(mappings: Map[String, Expr]) {

    val cache = new scala.collection.mutable.HashMap[Expr, Int]()

    private def eval(e: Expr): Int = {
      cache.get(e) match {
        case Some(i) =&gt; i
        case None =&gt; {
          val result = e match {
            case Variable(name) =&gt; {
              println(s"evaluating $name")
              eval(mappings(name))
            }
            case Const(value) =&gt; value
            case Not(expr) =&gt; ~eval(expr)
            case And(l, r) =&gt; eval(l) &amp; eval(r)
            case Or(l, r) =&gt; eval(l) | eval(r)
            case LShift(expr, n) =&gt; eval(expr) &lt;&lt; n
            case RShift(expr, n) =&gt; eval(expr) &gt;&gt; n
          }
          cache += (e -&gt; result)
          result
        }
      }
    }

    def call(name: String): Int = {
      eval(mappings(name))
    }
  }

  def main(args: Array[String]) = {
    val lines = Source.fromFile("input-day7.txt").getLines()
    val x = lines.map(Command.parse).map((a) =&gt; a match {
      case Assignment(Variable(v), e) =&gt; (v -&gt; e)
    })
    val m = x.toMap
    println(new Environment(m).call("a"))

  }

}
It seems to me that your general approach is solid. One small change I noticed you could make would be to move the Regex declarations outside of the functions parse and innerParse. The reason for doing this is that it is expensive to compile the Regexs with every call to these functions. By moving them out we only construct them once. This is supported by the Scala Regex documentation which can be read here.

case object Command {
  val assignmentRegex = """(.+) -&gt; (.+)""".r
  val rShiftRegex     = """(\w+) RSHIFT (\d+)""".r
  val lShiftRegex     = """(\w+) LSHIFT (\d+)""".r
  val constRegex      = """(\d+)""".r
  val varRegex        = """(\w+)""".r
  val notRegex        = """NOT (\w+)""".r
  val andRegex        = """(\w+) AND (\w+)""".r
  val orRegex         = """(\w+) OR (\w+)""".r

  def parse(s: String): Assignment = {
    def innerParse(s: String): Expr = {
      // ...
    }
    // ...
  }
  // ...
}