optimization

This commit is contained in:
kamoshi 2019-12-11 12:17:28 +01:00 committed by GitHub
parent 80667cb6f8
commit d2976965d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,37 +7,28 @@ import scala.collection.mutable
class Robot(software: Array[Long], init: Int = 0) { class Robot(software: Array[Long], init: Int = 0) {
private[this] val brain: Machine = new Machine(software) private[this] val brain: Machine = new Machine(software)
private[this] val map: mutable.HashMap[Point, Int] = new mutable.HashMap() private[this] val map: mutable.HashMap[(Int, Int), Int] = new mutable.HashMap()
private[this] var location: Point = Point(0, 0) private[this] var location: (Int, Int) = (0, 0)
private[this] var direction: Direction = Up private[this] var direction: Direction = Up
if (init != 0) map.addOne(Point(0,0), init) // when starting on white if (init != 0) map.addOne((0,0), init) // when starting on white
private[this] var paintedPanelsCount: Int = 0 def paintedPanels: Int = map.size
def paintedPanels: Int = paintedPanelsCount
def updateColor(point: Point, color: Int): Unit = { def updateColor(point: (Int, Int), color: Int): Unit = map(point) = color
if (map.contains(point)) {
map(point) = color
}
else {
paintedPanelsCount += 1
map.addOne(point, color)
}
}
def findColor(point: Point): Int = if (map.contains(point)) map(point) else 0 def findColor(point: (Int, Int)): Int = map.getOrElse(point, 0)
def nextLocation(location: Point, direction: Direction, output: Int): (Point, Direction) = { def nextLocation(point: (Int, Int), direction: Direction, command: Int): ((Int, Int), Direction) = {
(direction, output) match { (point, direction, command) match {
case (Up, 0) => (location.left, Left) case ((x, y), Up, 0) => ((x-1, y), Left)
case (Up, 1) => (location.right, Right) case ((x, y), Up, 1) => ((x+1, y), Right)
case (Down, 0) => (location.right, Right) case ((x, y), Down, 0) => ((x+1, y), Right)
case (Down, 1) => (location.left, Left) case ((x, y), Down, 1) => ((x-1, y), Left)
case (Left, 0) => (location.down, Down) case ((x, y), Left, 0) => ((x, y-1), Down)
case (Left, 1) => (location.up, Up) case ((x, y), Left, 1) => ((x, y+1), Up)
case (Right, 0) => (location.up, Up) case ((x, y), Right, 0) => ((x, y+1), Up)
case (Right, 1) => (location.down, Down) case ((x, y), Right, 1) => ((x, y-1), Down)
case _ => throw new Exception("Something went wrong") case _ => throw new Exception("Something went wrong")
} }
} }
@ -46,7 +37,7 @@ class Robot(software: Array[Long], init: Int = 0) {
def printMap(): Unit = { def printMap(): Unit = {
val matrix = Array.ofDim[Char](6, 46) val matrix = Array.ofDim[Char](6, 46)
map.foreach(tuple => { map.foreach(tuple => {
matrix(tuple._1.y+5)(tuple._1.x) = if (tuple._2 == 0) '.' else '#' matrix(tuple._1._2+5)(tuple._1._1) = if (tuple._2 == 0) '.' else '#'
}) })
for (i <- matrix.indices.reverse; j <- matrix(0).indices) { for (i <- matrix.indices.reverse; j <- matrix(0).indices) {
print(matrix(i)(j)) print(matrix(i)(j))