-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (59 loc) · 1.07 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
package main
import (
"fmt"
"os"
"github.com/sanderploegsma/advent-of-code/2019/go/utils"
)
const (
TileEmpty = 0
TileWall = 1
TileBlock = 2
TilePaddle = 3
TileBall = 4
)
func main() {
instructions, err := utils.ReadIntCode("input.txt")
if err != nil {
fmt.Printf("failed to read input file: %v\n", err)
os.Exit(1)
}
fmt.Println(CountBlockTiles(instructions))
fmt.Println(Play(instructions))
}
func CountBlockTiles(instructions []int) int {
out := make(chan int)
go utils.RunIntCode(make(chan int), out, instructions)
blockTiles := 0
for range out {
<-out
if TileBlock == <-out {
blockTiles++
}
}
return blockTiles
}
func Play(instructions []int) int {
in := make(chan int, 1)
out := make(chan int)
instructions[0] = 2
go utils.RunIntCode(in, out, instructions)
score := 0
ballX := -1
paddleX := -1
for x := range out {
y := <-out
v := <-out
if x == -1 && y == 0 {
score = v
continue
}
switch v {
case TilePaddle:
paddleX = x
case TileBall:
ballX = x
in <- utils.Compare(ballX, paddleX)
}
}
return score
}