lluque

AoC day 8

Dec 8th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.44 KB | None | 0 0
  1. import scala.io.Source
  2.  
  3. object day8 extends App {
  4.  
  5.   def drawRect(cmd: String)(implicit display: Array[Array[Int]]) = {
  6.     val Array(w, h) = (cmd split " " takeRight 1).mkString.split("x") map (_.toInt)
  7.     for {
  8.       x <- 0 until w
  9.       y <- 0 until h
  10.     } yield display(y)(x) = 1
  11.   }
  12.  
  13.   def rotate(cmd: String)(implicit display: Array[Array[Int]]) = {
  14.     val Array(n) = cmd.split(" ") takeRight 1 map (_.toInt)
  15.     val Array(z) = cmd.split(" ").slice(2, 3) map (a => a drop 2) map (_.toInt)
  16.     val vertical = cmd.contains("column")
  17.     val dim = if (vertical) 6 else 50
  18.     for (i <- 1 to n) {
  19.       val container = Array.ofDim[Int](dim)
  20.       for (j <- 0 until dim) {
  21.         val next = if (j < dim - 1) j + 1 else 0
  22.         container(next) = if (vertical) display(j)(z) else display(z)(j)
  23.       }
  24.       for (j <- 0 until dim) {
  25.         if (vertical) display(j)(z) = container(j)
  26.         else display(z)(j) = container(j)
  27.       }
  28.     }
  29.   }
  30.  
  31.   def runCommand(cmd: String) = cmd match {
  32.     case x if x.startsWith("rect") => drawRect(x)
  33.     case x if x.startsWith("rotate") => rotate(x)
  34.   }
  35.  
  36.   implicit val display = Array.ofDim[Int](6, 50)
  37.   val input = Source.fromFile("day8.txt").getLines.toList
  38.   for (
  39.     cmd <- input
  40.   ) yield runCommand(cmd)
  41.  
  42.   println(display.map(_.mkString).mkString("\n")
  43.     map {case '0' => ' ' case '1' => '*' case _ => '\n'})
  44.  
  45.   println("pixel count: " + display.flatten.count (_ == 1))
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment