lluque

AoC day 22

Dec 23rd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.94 KB | None | 0 0
  1. import scala.io.Source
  2.  
  3. object day22 extends App {
  4.  
  5.   case class Node(y: Int, x: Int, used: Int, avail: Int) {
  6.     def viablePair(other: Node): Boolean = used > 0 && other.avail >= used
  7.     override def toString: String = if (used > 99) "#" else if (used == 0) "_" else "."
  8.   }
  9.  
  10.   val input = Source.fromFile("22.txt").getLines.toList
  11.   val pattern = """^.*-x(\d+)-y(\d+)\s+(\d+)T\s+(\d+)T\s+(\d+)T\s+(\d+)%$""".r
  12.   val nodes = input.drop(2).map {
  13.     case pattern(x, y, size, used, avail, load) => Node(y.toInt, x.toInt, used.toInt, avail.toInt)
  14.   }
  15.  
  16.   // Part one
  17.   val pairs = nodes.combinations(2).toList.flatMap(_.permutations.toList)
  18.   println("number of viable pairs: " + pairs.count(p => p.head.viablePair(p.last)))
  19.  
  20.   // Part two. Display the damn grid. Lets solve this by hand from there.
  21.   val rows = nodes.groupBy(_.y)
  22.   val grid = rows.keys.toList.indices.map(rows(_))
  23.   println(grid.map(_.mkString(" ")).mkString("\n"))
  24.  
  25. }
Advertisement
Add Comment
Please, Sign In to add comment