Add files via upload
This commit is contained in:
parent
2ee9535af6
commit
940411f4f6
18
2019/Scala/day11/Day11.scala
Normal file
18
2019/Scala/day11/Day11.scala
Normal file
|
@ -0,0 +1,18 @@
|
|||
package day11
|
||||
|
||||
class Day11(original: Array[Long])
|
||||
{
|
||||
def solveP1(): Long =
|
||||
{
|
||||
val robot = new Robot(original)
|
||||
robot.run()
|
||||
robot.paintedPanels
|
||||
}
|
||||
|
||||
def solveP2(): Unit =
|
||||
{
|
||||
val robot = new Robot(original, 1)
|
||||
robot.run()
|
||||
robot.printMap()
|
||||
}
|
||||
}
|
20
2019/Scala/day11/Main.scala
Normal file
20
2019/Scala/day11/Main.scala
Normal file
|
@ -0,0 +1,20 @@
|
|||
package day11
|
||||
|
||||
import kamlib.{Reader, Wrapper}
|
||||
|
||||
object Main {
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
// Initialize initial program memory
|
||||
val memory: Array[Long] = Array.ofDim[Long](1200)
|
||||
val input: Array[Long] = Reader.readString("/input11.txt").split("[^\\d-]+").map(x => x.toLong)
|
||||
for(i <- input.indices) { memory(i) = input(i)}
|
||||
val solution: Day11 = new Day11(memory)
|
||||
|
||||
println("Part 1:")
|
||||
Wrapper(solution.solveP1()).print()
|
||||
println("Part 2:")
|
||||
Wrapper(solution.solveP2()).print()
|
||||
|
||||
}
|
||||
}
|
8
2019/Scala/day11/Point.scala
Normal file
8
2019/Scala/day11/Point.scala
Normal file
|
@ -0,0 +1,8 @@
|
|||
package day11
|
||||
|
||||
case class Point(x: Int, y: Int) {
|
||||
def up = Point(x, y+1)
|
||||
def down = Point(x, y-1)
|
||||
def left = Point(x-1, y)
|
||||
def right = Point(x+1, y)
|
||||
}
|
81
2019/Scala/day11/Robot.scala
Normal file
81
2019/Scala/day11/Robot.scala
Normal file
|
@ -0,0 +1,81 @@
|
|||
package day11
|
||||
|
||||
import day11.direction._
|
||||
import intcode.{Finished, Input, Machine, Ready}
|
||||
|
||||
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] var direction: Direction = Up
|
||||
|
||||
if (init != 0) map.addOne(Point(0,0), init) // when starting on white
|
||||
|
||||
private[this] var paintedPanelsCount: Int = 0
|
||||
def paintedPanels: Int = paintedPanelsCount
|
||||
|
||||
def updateColor(point: Point, color: Int): Unit = {
|
||||
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 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)
|
||||
case _ => throw new Exception("Something went wrong")
|
||||
}
|
||||
}
|
||||
|
||||
// This print function uses a naive approach of having stuff hardcoded for my output, so might require calibration
|
||||
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 '#'
|
||||
})
|
||||
for (i <- matrix.indices.reverse; j <- matrix(0).indices) {
|
||||
print(matrix(i)(j))
|
||||
if (j == 0) println()
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
@scala.annotation.tailrec
|
||||
final def run(): Unit = {
|
||||
brain.getState match {
|
||||
// Ready to run
|
||||
case Ready =>
|
||||
brain.run()
|
||||
this.run()
|
||||
|
||||
// Finished the program
|
||||
case Finished => ()
|
||||
|
||||
// Program requires input of some sorts
|
||||
case Input =>
|
||||
brain.enqueue(findColor(location))
|
||||
brain.run()
|
||||
updateColor(location, brain.output.toInt) // 1st output -> color
|
||||
val (p, d) = nextLocation(location, direction, brain.output.toInt) // 2nd output -> direction
|
||||
location = p
|
||||
direction = d
|
||||
this.run()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
7
2019/Scala/day11/direction/Direction.scala
Normal file
7
2019/Scala/day11/direction/Direction.scala
Normal file
|
@ -0,0 +1,7 @@
|
|||
package day11.direction
|
||||
|
||||
sealed trait Direction
|
||||
case object Up extends Direction
|
||||
case object Down extends Direction
|
||||
case object Left extends Direction
|
||||
case object Right extends Direction
|
Loading…
Reference in a new issue