optimization
This commit is contained in:
parent
80667cb6f8
commit
d2976965d1
|
@ -7,37 +7,28 @@ import scala.collection.mutable
|
|||
|
||||
class Robot(software: Array[Long], init: Int = 0) {
|
||||
private[this] val brain: Machine = new Machine(software)
|
||||
private[this] val map: mutable.HashMap[Point, Int] = new mutable.HashMap()
|
||||
private[this] var location: Point = Point(0, 0)
|
||||
private[this] val map: mutable.HashMap[(Int, Int), Int] = new mutable.HashMap()
|
||||
private[this] var location: (Int, Int) = (0, 0)
|
||||
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 = paintedPanelsCount
|
||||
def paintedPanels: Int = map.size
|
||||
|
||||
def updateColor(point: Point, color: Int): Unit = {
|
||||
if (map.contains(point)) {
|
||||
map(point) = color
|
||||
}
|
||||
else {
|
||||
paintedPanelsCount += 1
|
||||
map.addOne(point, color)
|
||||
}
|
||||
}
|
||||
def updateColor(point: (Int, Int), color: Int): Unit = map(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) = {
|
||||
(direction, output) match {
|
||||
case (Up, 0) => (location.left, Left)
|
||||
case (Up, 1) => (location.right, Right)
|
||||
case (Down, 0) => (location.right, Right)
|
||||
case (Down, 1) => (location.left, Left)
|
||||
case (Left, 0) => (location.down, Down)
|
||||
case (Left, 1) => (location.up, Up)
|
||||
case (Right, 0) => (location.up, Up)
|
||||
case (Right, 1) => (location.down, Down)
|
||||
def nextLocation(point: (Int, Int), direction: Direction, command: Int): ((Int, Int), Direction) = {
|
||||
(point, direction, command) match {
|
||||
case ((x, y), Up, 0) => ((x-1, y), Left)
|
||||
case ((x, y), Up, 1) => ((x+1, y), Right)
|
||||
case ((x, y), Down, 0) => ((x+1, y), Right)
|
||||
case ((x, y), Down, 1) => ((x-1, y), Left)
|
||||
case ((x, y), Left, 0) => ((x, y-1), Down)
|
||||
case ((x, y), Left, 1) => ((x, y+1), Up)
|
||||
case ((x, y), Right, 0) => ((x, y+1), Up)
|
||||
case ((x, y), Right, 1) => ((x, y-1), Down)
|
||||
case _ => throw new Exception("Something went wrong")
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +37,7 @@ class Robot(software: Array[Long], init: Int = 0) {
|
|||
def printMap(): Unit = {
|
||||
val matrix = Array.ofDim[Char](6, 46)
|
||||
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) {
|
||||
print(matrix(i)(j))
|
||||
|
|
Loading…
Reference in a new issue