@@ -76,9 +76,9 @@ func dumpboard2(b Board) (int, int) {
76
76
for x := 0 ; x < b .maxx ; x ++ {
77
77
coord := [2 ]int {x , y }
78
78
coordhalf := [2 ]int {x / 2 , y }
79
- coordp1 := [2 ]int {x - 1 , y }
79
+ // coordp1 := [2]int{x - 1, y}
80
80
81
- if coord == b .robot || coordp1 == b . robot {
81
+ if coord == b .robot {
82
82
if display {
83
83
fmt .Print ("@" )
84
84
}
@@ -104,6 +104,41 @@ func dumpboard2(b Board) (int, int) {
104
104
return gps , 0
105
105
}
106
106
107
+ func intheway (b Board , coord1 [2 ]int , d [2 ]int , dwidth bool ) (bool , []int ) {
108
+ out := []int {}
109
+
110
+ coord1 = [2 ]int {coord1 [0 ] + d [0 ], coord1 [1 ] + d [1 ]}
111
+ coord1m := [2 ]int {coord1 [0 ] - 1 , coord1 [1 ]}
112
+ coord1m2 := [2 ]int {coord1 [0 ] + 1 , coord1 [1 ]}
113
+
114
+ coordhalf := [2 ]int {coord1 [0 ] / 2 , coord1 [1 ]}
115
+ coordhalf2 := [2 ]int {coord1m [0 ] / 2 , coord1 [1 ]}
116
+ coordhalf3 := [2 ]int {coord1m2 [0 ] / 2 , coord1 [1 ]}
117
+
118
+ if b .obs [coordhalf ] {
119
+ fmt .Println ("itw: obs" )
120
+ return true , []int {}
121
+ }
122
+ if dwidth {
123
+ if b .obs [coordhalf2 ] || b .obs [coordhalf3 ] {
124
+ fmt .Println ("itw: obs dwidth" )
125
+ return true , []int {}
126
+ }
127
+ }
128
+
129
+ for k , v := range b .boxes {
130
+ if v == coord1 || v == coord1m {
131
+ out = append (out , k )
132
+ } else if dwidth {
133
+ if v == coord1m2 {
134
+ out = append (out , k )
135
+ }
136
+ }
137
+ }
138
+ fmt .Println ("itw" , coord1 , out )
139
+ return false , out
140
+ }
141
+
107
142
func day15 (file string ) (part1 , part2 int ) {
108
143
f , err := os .Open (file )
109
144
if err != nil {
@@ -198,45 +233,79 @@ func day15(file string) (part1, part2 int) {
198
233
}
199
234
if b .board [r1 ] == '#' {
200
235
// Can't move.
201
- fmt .Println ("Can't move O" )
236
+ // fmt.Println("Can't move O")
202
237
continue a
203
238
}
204
239
b .board [r1 ] = 'O'
205
240
delete (b .board , l1 )
206
241
b .robot = coord1
207
- fmt .Println ("Move block O" )
242
+ // fmt.Println("Move block O")
208
243
}
209
244
b:
210
245
for m , move := range moves {
211
246
if true {
212
- fmt .Println (m )
247
+ fmt .Println ("Part2" , m , string ( move ) )
213
248
dumpboard2 (b2 )
214
249
}
215
250
d := dirmap [byte (move )]
216
251
217
- coord1 := [2 ]int {b .robot [0 ] + d [0 ], b .robot [1 ] + d [1 ]}
252
+ coord1 := [2 ]int {b2 .robot [0 ] + d [0 ], b2 .robot [1 ] + d [1 ]}
218
253
// Empty
219
254
220
- coord1m := [2 ]int {coord1 [0 ] - 1 , coord1 [1 ]}
221
- coordhalf := [2 ]int {coord1 [0 ] / 2 , coord1 [1 ]}
255
+ // coord1m := [2]int{coord1[0] - 1, coord1[1]}
256
+ // coordhalf := [2]int{coord1[0] / 2, coord1[1]}
222
257
223
- if b .obs [coordhalf ] {
224
- // blocked
225
- fmt .Println ("Blocked" , string (move ))
226
- continue
258
+ // fmt.Println(coord1, coord1m, coordhalf)
259
+ // if b2.obs[coordhalf] {
260
+ // // blocked
261
+ // fmt.Println("Blocked", string(move))
262
+ // continue
263
+ // }
264
+
265
+ obstructed , itw := intheway (b2 , b2 .robot , d , false )
266
+ if ! obstructed && len (itw ) == 0 {
267
+ fmt .Println ("no block" , string (move ))
268
+ b2 .robot = coord1
269
+ continue b
227
270
}
271
+ fmt .Println ("Blocked by boxes" , itw , obstructed )
228
272
229
- intheway := []int {}
230
- for k , v := range b .boxes {
231
- if v == coord1 || v == coord1m {
232
- intheway = append (intheway , k )
273
+ boxes := map [int ]bool {}
274
+ for _ , k := range itw {
275
+ boxes [k ] = true
276
+ }
277
+ c:
278
+ for _ , box := range itw {
279
+ obstructed , itw = intheway (b2 , b2 .boxes [box ], d , true )
280
+ if obstructed {
281
+ fmt .Println ("Can't move boxes" , string (move ))
282
+ continue b
283
+ }
284
+ if len (itw ) == 0 {
285
+ // Ok, move the boxes
286
+ continue c
287
+ } else {
288
+ // More boxes in the way
289
+ itw2 := []int {}
290
+ for _ , k := range itw {
291
+ if ! boxes [k ] {
292
+ boxes [k ] = true
293
+ itw2 = append (itw2 , k )
294
+ }
295
+ }
296
+ // itw = []int{}
297
+ itw = itw2
298
+ fmt .Println ("Again" , boxes )
299
+ goto c // again
233
300
}
234
301
}
235
- if len ( intheway ) == 0 {
236
- fmt . Println ( "no blockj" , string ( move ))
237
- b2 .robot = coord1
238
- continue b
302
+ fmt . Println ( "Moving boxes" , boxes )
303
+ for k := range boxes {
304
+ b2 .boxes [ k ][ 0 ] += d [ 0 ]
305
+ b2 . boxes [ k ][ 1 ] += d [ 1 ]
239
306
}
307
+ b2 .robot = coord1
308
+
240
309
}
241
310
}
242
311
part1 , part2 = dumpboard (b )
0 commit comments