|
@@ -4,9 +4,22 @@ import (
|
4
|
4
|
"fmt"
|
5
|
5
|
"github.com/csmith/aoc-2019/common"
|
6
|
6
|
"github.com/csmith/aoc-2019/intcode"
|
|
7
|
+ "math"
|
7
|
8
|
)
|
8
|
9
|
|
9
|
|
-func run(memory []int, startWhite bool) (int, [500][500]bool) {
|
|
10
|
+type point struct {
|
|
11
|
+ x int
|
|
12
|
+ y int
|
|
13
|
+}
|
|
14
|
+
|
|
15
|
+var dirs = [4]point{
|
|
16
|
+ 0: {y: -1},
|
|
17
|
+ 1: {x: +1},
|
|
18
|
+ 2: {y: +1},
|
|
19
|
+ 3: {x: -1},
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+func run(memory []int, startWhite bool) (int, map[point]bool) {
|
10
|
23
|
input := make([]int, len(memory))
|
11
|
24
|
copy(input, memory)
|
12
|
25
|
vm := intcode.NewVirtualMachine(input)
|
|
@@ -14,17 +27,15 @@ func run(memory []int, startWhite bool) (int, [500][500]bool) {
|
14
|
27
|
vm.Output = make(chan int, 1)
|
15
|
28
|
go vm.Run()
|
16
|
29
|
|
17
|
|
- var grid [500][500]bool
|
18
|
|
- var painted [500][500]bool
|
19
|
|
-
|
|
30
|
+ grid := make(map[point]bool, 50)
|
20
|
31
|
paintCount := 0
|
21
|
|
- x := 250
|
22
|
|
- y := 250
|
|
32
|
+
|
|
33
|
+ pos := point{x: 0, y: 0}
|
23
|
34
|
dir := 0
|
24
|
|
- grid[y][x] = startWhite
|
|
35
|
+ colour := startWhite
|
25
|
36
|
|
26
|
37
|
for {
|
27
|
|
- if grid[y][x] {
|
|
38
|
+ if colour {
|
28
|
39
|
vm.Input <- 1
|
29
|
40
|
} else {
|
30
|
41
|
vm.Input <- 0
|
|
@@ -36,28 +47,15 @@ func run(memory []int, startWhite bool) (int, [500][500]bool) {
|
36
|
47
|
}
|
37
|
48
|
turn := <-vm.Output
|
38
|
49
|
|
39
|
|
- grid[y][x] = paint == 1
|
40
|
|
- if !painted[y][x] {
|
|
50
|
+ if _, ok := grid[pos]; !ok {
|
41
|
51
|
paintCount++
|
42
|
|
- painted[y][x] = true
|
43
|
52
|
}
|
|
53
|
+ grid[pos] = paint == 1
|
44
|
54
|
|
45
|
|
- if turn == 1 {
|
46
|
|
- dir = (dir + 1) % 4
|
47
|
|
- } else {
|
48
|
|
- dir = (dir + 3) % 4
|
49
|
|
- }
|
50
|
|
-
|
51
|
|
- switch dir {
|
52
|
|
- case 0:
|
53
|
|
- y--
|
54
|
|
- case 1:
|
55
|
|
- x++
|
56
|
|
- case 2:
|
57
|
|
- y++
|
58
|
|
- case 3:
|
59
|
|
- x--
|
60
|
|
- }
|
|
55
|
+ dir = (4 + dir + 2*turn - 1) % 4
|
|
56
|
+ pos.x += dirs[dir].x
|
|
57
|
+ pos.y += dirs[dir].y
|
|
58
|
+ colour = grid[pos]
|
61
|
59
|
}
|
62
|
60
|
|
63
|
61
|
return paintCount, grid
|
|
@@ -68,10 +66,18 @@ func main() {
|
68
|
66
|
paintCount, _ := run(input, false)
|
69
|
67
|
_, grid := run(input, true)
|
70
|
68
|
|
71
|
|
- println(paintCount)
|
72
|
|
- for _, line := range grid {
|
73
|
|
- for _, b := range line {
|
74
|
|
- if b {
|
|
69
|
+ fmt.Println(paintCount)
|
|
70
|
+
|
|
71
|
+ minX, minY := math.MaxInt64, math.MaxInt64
|
|
72
|
+ maxX, maxY := math.MinInt64, math.MinInt64
|
|
73
|
+ for pos := range grid {
|
|
74
|
+ minX, maxX = common.Min(minX, pos.x), common.Max(maxX, pos.x)
|
|
75
|
+ minY, maxY = common.Min(minY, pos.y), common.Max(maxY, pos.y)
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ for y := minY; y <= maxY; y++ {
|
|
79
|
+ for x := minX; x <= maxX; x++ {
|
|
80
|
+ if grid[point{x: x, y: y}] {
|
75
|
81
|
fmt.Print("█")
|
76
|
82
|
} else {
|
77
|
83
|
fmt.Print(" ")
|