Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.io.Source
- object day22 extends App {
- case class Node(y: Int, x: Int, used: Int, avail: Int) {
- def viablePair(other: Node): Boolean = used > 0 && other.avail >= used
- override def toString: String = if (used > 99) "#" else if (used == 0) "_" else "."
- }
- val input = Source.fromFile("22.txt").getLines.toList
- val pattern = """^.*-x(\d+)-y(\d+)\s+(\d+)T\s+(\d+)T\s+(\d+)T\s+(\d+)%$""".r
- val nodes = input.drop(2).map {
- case pattern(x, y, size, used, avail, load) => Node(y.toInt, x.toInt, used.toInt, avail.toInt)
- }
- // Part one
- val pairs = nodes.combinations(2).toList.flatMap(_.permutations.toList)
- println("number of viable pairs: " + pairs.count(p => p.head.viablePair(p.last)))
- // Part two. Display the damn grid. Lets solve this by hand from there.
- val rows = nodes.groupBy(_.y)
- val grid = rows.keys.toList.indices.map(rows(_))
- println(grid.map(_.mkString(" ")).mkString("\n"))
- }
Advertisement
Add Comment
Please, Sign In to add comment