Advertisement
lluque

AoC day 9

Dec 9th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.96 KB | None | 0 0
  1. import scala.io.Source
  2.  
  3. object day9 extends App {
  4.  
  5.   def decompressedLength(xs: List[Char], v2: Boolean): Long = {
  6.  
  7.     def getMarker(xs: List[Char]): (Int, Int, Int) = {
  8.       val clause = xs.mkString.split(')').head
  9.       val Array(a, b) = clause split 'x' map (_.toInt)
  10.       (a, b, clause.length + 1)
  11.     }
  12.  
  13.     def accumulator(xs: List[Char], acc: Long): Long = xs match {
  14.       case Nil => acc
  15.       case '(' :: t =>
  16.         val marker = getMarker(t)
  17.         val (content, remainder) = t drop marker._3 splitAt marker._1
  18.         val scope: Long =
  19.           if (v2) accumulator(content, 0)
  20.           else content.length
  21.         accumulator(remainder, acc + marker._2 * scope)
  22.       case _ :: t => accumulator(t, acc + 1)
  23.     }
  24.  
  25.     accumulator(xs, 0)
  26.   }
  27.  
  28.   val input = Source.fromFile("day9.txt").getLines.mkString.replaceAll("\\s", "").toList
  29.   println(decompressedLength(input, v2 = false))
  30.   println(decompressedLength(input, v2 = true))
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement