-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart2.scala
83 lines (71 loc) · 1.53 KB
/
Part2.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.brahle.adventofcode.year2019
import scala.collection.mutable
object Part2 extends App {
val values = scala.io.StdIn.readLine()
val robot = new IntMachine(values, "")
val BLACK = 0
val WHITE = 1
val dx = Array(0, 1, 0, -1)
val dy = Array(1, 0, -1, 0)
val turn = Array(-1, 1)
var x = 0
var y = 0
var minx = 0
var maxx = 0
var miny = 0
var maxy = 0
var direction = 0
var idx = 0
var first = true
val memory = mutable.Map[(Int, Int), Int]()
robot.input = () => {
if (first) {
first = false
WHITE
} else {
BigInt.int2bigInt(memory.getOrElse((x, y), BLACK))
}
}
def first(value: BigInt): Unit = {
memory((x, y)) = value.toInt
}
def second(value: BigInt): Unit = {
direction = (4 + direction + turn(value.toInt)) % 4
x += dx(direction)
if (x > maxx) {
maxx = x
}
if (x < minx) {
minx = x
}
y += dy(direction)
if (y > maxy) {
maxy = y
}
if (y < miny) {
miny = y
}
println(x, y)
}
robot.output = (value: BigInt) => {
idx += 1
idx % 2 match {
case 1 => first(value)
case 0 => second(value)
}
}
robot.evaluate()
for (j <- maxy to miny by -1) {
for (i <- minx to maxx by 1) {
val t = memory.getOrElse((i, j), BLACK)
if (t == BLACK) {
print(".")
} else {
print("#")
}
}
println()
}
println((minx, maxx))
println((miny, maxy))
}