|
@@ -0,0 +1,101 @@
|
|
1
|
+package main
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "github.com/csmith/aoc-2019/common"
|
|
5
|
+ "github.com/csmith/aoc-2019/intcode"
|
|
6
|
+)
|
|
7
|
+
|
|
8
|
+func patch(input []int, width int) {
|
|
9
|
+ // Insert two quarters
|
|
10
|
+ input[0] = 2
|
|
11
|
+
|
|
12
|
+ // Find the bottom line of the play area and change it to wall
|
|
13
|
+ for i := len(input) - 1; i > 0; i-- {
|
|
14
|
+ if input[i] == 1 && input[i-width] == 1 {
|
|
15
|
+ allEmpty := true
|
|
16
|
+ for j := i - width + 1; j < i-1; j++ {
|
|
17
|
+ if input[j] != 0 {
|
|
18
|
+ allEmpty = false
|
|
19
|
+ break
|
|
20
|
+ }
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ if allEmpty {
|
|
24
|
+ for j := i - width + 1; j < i-1; j++ {
|
|
25
|
+ input[j] = 1
|
|
26
|
+ }
|
|
27
|
+ break
|
|
28
|
+ }
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+func feedInput(input chan int) {
|
|
34
|
+ for {
|
|
35
|
+ input <- 0
|
|
36
|
+ }
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+func readOutput(output chan int, handler func(int, int, int)) {
|
|
40
|
+ for {
|
|
41
|
+ x, more := <-output
|
|
42
|
+ if !more {
|
|
43
|
+ break
|
|
44
|
+ }
|
|
45
|
+ y := <-output
|
|
46
|
+ tile := <-output
|
|
47
|
+ handler(x, y, tile)
|
|
48
|
+ }
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+func countBlocks(screen [60][80]int) (blocks int) {
|
|
52
|
+ for _, line := range screen {
|
|
53
|
+ for _, pixel := range line {
|
|
54
|
+ if pixel == 2 {
|
|
55
|
+ blocks++
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+ return
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+func run(input []int, handler func(int, int, int)) {
|
|
63
|
+ memory := make([]int, len(input))
|
|
64
|
+ copy(memory, input)
|
|
65
|
+ vm := intcode.NewVirtualMachine(memory)
|
|
66
|
+ vm.Input = make(chan int, 1)
|
|
67
|
+ vm.Output = make(chan int, 6)
|
|
68
|
+
|
|
69
|
+ go feedInput(vm.Input)
|
|
70
|
+ go vm.Run()
|
|
71
|
+
|
|
72
|
+ readOutput(vm.Output, handler)
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+func main() {
|
|
76
|
+ input := common.ReadCsvAsInts("13/input.txt")
|
|
77
|
+ memory := make([]int, len(input))
|
|
78
|
+ copy(memory, input)
|
|
79
|
+
|
|
80
|
+ width := 0
|
|
81
|
+ var screen [60][80]int
|
|
82
|
+ run(memory, func(x int, y int, tile int) {
|
|
83
|
+ screen[y][x] = tile
|
|
84
|
+ if x > width {
|
|
85
|
+ width = x
|
|
86
|
+ }
|
|
87
|
+ })
|
|
88
|
+
|
|
89
|
+ println(countBlocks(screen))
|
|
90
|
+
|
|
91
|
+ patch(input, width)
|
|
92
|
+
|
|
93
|
+ score := 0
|
|
94
|
+ run(input, func(x int, y int, tile int) {
|
|
95
|
+ if x == -1 {
|
|
96
|
+ score = tile
|
|
97
|
+ }
|
|
98
|
+ })
|
|
99
|
+
|
|
100
|
+ println(score)
|
|
101
|
+}
|