-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
265 lines (215 loc) · 4.78 KB
/
main.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/hamzah-hayat/adventofcode/intcode"
)
func main() {
// Use Flags to run a part
methodP := flag.String("method", "p2", "The method/part that should be run, valid are p1,p2 and test")
flag.Parse()
switch *methodP {
case "p1":
partOne()
break
case "p2":
partTwo()
break
case "test":
break
}
}
func partOne() {
input := readInput()
program := convertToInts(input)
// First channel is for input
inputChan := make(chan int)
// Second channel is for output
outputChan := make(chan int)
// Terimnation channel
t := make(chan bool)
// Message channel
message := make(chan intcode.Message)
go intcode.RunIntCodeProgramWaitForTermination(program, inputChan, outputChan, t, message)
screen := createGameScreen(outputChan, t, message)
count := 0
for _, val := range screen {
if val == tIDBlock {
count++
}
}
fmt.Println("The number of block tiles is", count)
}
func createGameScreen(outputChan chan int, t chan bool, message chan intcode.Message) map[space]int {
screen := make(map[space]int)
teriminate := false
for {
select {
case <-t:
teriminate = true
break
case m := <-message:
if m.MessageType == 1 {
x := <-outputChan
<-message
y := <-outputChan
<-message
tileID := <-outputChan
screen[space{x: x, y: y}] = tileID
}
}
if teriminate {
break
}
}
return screen
}
type space struct {
x, y int
}
const (
tIDEmpty int = 0 // 0 is an empty tile. No game object appears in this tile.
tIDWall int = 1 // 1 is a wall tile. Walls are indestructible barriers.
tIDBlock int = 2 // 2 is a block tile. Blocks can be broken by the ball.
tIDPaddle int = 3 // 3 is a horizontal paddle tile. The paddle is indestructible.
tIDBall int = 4 // 4 is a ball tile. The ball moves diagonally and bounces off objects.
)
func partTwo() {
input := readInput()
program := convertToInts(input)
// First channel is for input
inputChan := make(chan int)
// Second channel is for output
outputChan := make(chan int)
// Terimnation channel
t := make(chan bool)
// Message channel
message := make(chan intcode.Message)
// Put some money in!
program[0] = 2
go intcode.RunIntCodeProgramWaitForTermination(program, inputChan, outputChan, t, message)
score := playGame(inputChan, outputChan, t, message)
fmt.Println("The score is", score)
}
func playGame(inputChan chan int, outputChan chan int, t chan bool, message chan intcode.Message) int {
screen := make(map[space]int)
teriminate := false
ballAndPaddleReady := false
score := 0
ballX := -1
paddleX := -1
for {
select {
case <-t:
teriminate = true
break
case m := <-message:
if m.MessageType == 0 {
// Waiting for input
if ballX != -1 && paddleX != -1 && !ballAndPaddleReady {
ballAndPaddleReady = true
}
if ballAndPaddleReady {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
fmt.Println(printScreen(screen))
time.Sleep(time.Millisecond)
// Now move towards the ball
if ballX < paddleX {
inputChan <- -1
} else if ballX > paddleX {
inputChan <- 1
} else {
inputChan <- 0
}
}
} else if m.MessageType == 1 {
// Giving output
x := <-outputChan
<-message
y := <-outputChan
<-message
tileID := <-outputChan
if x == -1 && y == 0 {
score = tileID
continue
}
screen[space{x: x, y: y}] = tileID
if tileID == tIDBall {
ballX = x
}
if tileID == tIDPaddle {
paddleX = x
}
}
}
if teriminate {
break
}
}
return score
}
func findObject(screen map[space]int, tID int) space {
for s, val := range screen {
if val == tID {
return s
}
}
// Cant find this object
return space{x: -1, y: -1}
}
func printScreen(screen map[space]int) string {
// Find the xMin,xMax and ymin,yMax
// Hardcode these for now
xMin := 0
xMax := 23
yMin := 0
yMax := 50
// now print the grid
picture := ""
for i := xMin; i < xMax; i++ {
for j := yMin; j < yMax; j++ {
switch screen[space{x: j, y: i}] {
case 0:
picture += " "
case 1:
picture += "|"
case 2:
picture += "█"
case 3:
picture += "_"
case 4:
picture += "O"
}
}
picture += "\n"
}
return picture
}
// Read data from input.txt
// Return the string, so that we can deal with it however
func readInput() []string {
var input []string
f, _ := os.Open("input.txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func convertToInts(input []string) []int {
programStr := strings.Split(input[0], ",")
var program []int
for _, s := range programStr {
i, _ := strconv.Atoi(s)
program = append(program, i)
}
return program
}