@@ -2,61 +2,62 @@ package y2019
22
33import (
44 "isaacgood.com/aoc/helpers"
5- "time"
65)
76
87// Day 13 solves 2019/13.
9- type Day13 struct {
8+ type Day13 struct {}
9+
10+ func (q * Day13 ) read (ic * IntCode , o <- chan int ) int {
11+ v := <- o
12+ ic .run ()
13+ return v
1014}
1115
1216// Solve returns the solution for one part.
1317func (q * Day13 ) Solve (data string , part int ) string {
14- blocks := make (map [[2 ]int ]int )
1518
16- i , o := make (chan int ), make (chan int )
17- ic := NewIntCode (data , false , i , o )
19+ i , o := make (chan int , 1 ), make (chan int , 1 )
20+ ic := NewIntCode (data , IO ( i , o ), Synchronous () )
1821 if part == 2 {
1922 ic .mem [0 ] = 2
2023 }
21- go ic .run ()
2224
2325 var ball , paddle , score = 0 , 0 , 0
26+ blocks := make (map [[2 ]int ]int )
2427 for ic .state != StateHalt {
25- for ic .state != StateHalt {
26- x , ok := read (o , 2 * time .Millisecond )
27- if ! ok {
28- break
29- }
30- y , tileType := <- o , <- o
31- if part == 1 && tileType == 2 { // block
28+ ic .run ()
29+ for ic .state != StateBlockInput && ic .state != StateHalt {
30+ x := q .read (ic , o )
31+ y := q .read (ic , o )
32+ tileType := q .read (ic , o )
33+ if tileType == 2 {
3234 blocks [[2 ]int {x , y }] = 1
33- } else if tileType == 3 {
34- paddle = x
35- } else if tileType == 4 {
36- ball = x
3735 } else if x == - 1 && y == 0 {
3836 score = tileType
37+ } else {
38+ if tileType == 3 {
39+ paddle = x
40+ } else if tileType == 4 {
41+ ball = x
42+ }
43+ delete (blocks , [2 ]int {x , y })
3944 }
40-
4145 }
4246 if part == 1 {
4347 return helpers .Itoa (len (blocks ))
4448 }
49+ if len (blocks ) == 0 {
50+ return helpers .Itoa (score )
51+ }
4552 if paddle == ball {
46- if ok := write (i , 0 , 10 * time .Millisecond ); ! ok {
47- break
48- }
53+ i <- 0
4954 } else if paddle > ball {
50- if ok := write (i , - 1 , 10 * time .Millisecond ); ! ok {
51- break
52- }
55+ i <- - 1
5356 } else {
54- if ok := write (i , 1 , 10 * time .Millisecond ); ! ok {
55- break
56- }
57+ i <- 1
5758 }
5859 }
59- return helpers . Itoa ( score )
60+ return ""
6061}
6162
6263func init () {
0 commit comments