@@ -18,6 +18,8 @@ var dirmap = map[byte][2]int{
18
18
19
19
type Board struct {
20
20
board map [[2 ]int ]byte
21
+ obs map [[2 ]int ]bool
22
+ boxes [][2 ]int
21
23
robot [2 ]int
22
24
maxx int
23
25
maxy int
@@ -30,22 +32,22 @@ func dumpboard(b Board) (int, int) {
30
32
gps := 0
31
33
for y := 0 ; y < b .maxy ; y ++ {
32
34
for x := 0 ; x < b .maxx ; x ++ {
33
- if [2 ]int {x , y } == b .robot {
35
+ coord := [2 ]int {x , y }
36
+
37
+ if coord == b .robot {
34
38
if display {
35
39
fmt .Print ("@" )
36
40
}
37
41
continue
38
42
}
39
- c := b .board [[ 2 ] int { x , y } ]
43
+ c := b .board [coord ]
40
44
if c == 'O' {
41
45
gps += y * 100 + x
42
46
}
43
47
if display {
44
48
switch c {
45
49
case '#' , 'O' :
46
-
47
50
fmt .Print (string (c ))
48
-
49
51
default :
50
52
fmt .Print ("." )
51
53
}
@@ -59,6 +61,49 @@ func dumpboard(b Board) (int, int) {
59
61
return gps , 0
60
62
}
61
63
64
+ func dumpboard2 (b Board ) (int , int ) {
65
+ _ = dirmap
66
+ display := true
67
+ gps := 0
68
+
69
+ boxMap := map [[2 ]int ]byte {}
70
+ for _ , v := range b .boxes {
71
+ boxMap [v ] = '['
72
+ boxMap [[2 ]int {v [0 ] + 1 , v [1 ]}] = ']'
73
+ }
74
+
75
+ for y := 0 ; y < b .maxy ; y ++ {
76
+ for x := 0 ; x < b .maxx ; x ++ {
77
+ coord := [2 ]int {x , y }
78
+ coordhalf := [2 ]int {x / 2 , y }
79
+ coordp1 := [2 ]int {x - 1 , y }
80
+
81
+ if coord == b .robot || coordp1 == b .robot {
82
+ if display {
83
+ fmt .Print ("@" )
84
+ }
85
+ continue
86
+ }
87
+
88
+ if b .obs [coordhalf ] {
89
+ fmt .Print ("#" )
90
+ continue
91
+ }
92
+
93
+ if boxMap [coord ] != byte (0 ) {
94
+ fmt .Print (string (boxMap [coord ]))
95
+ continue
96
+ }
97
+
98
+ fmt .Print ("." )
99
+ }
100
+ if display {
101
+ fmt .Println ()
102
+ }
103
+ }
104
+ return gps , 0
105
+ }
106
+
62
107
func day15 (file string ) (part1 , part2 int ) {
63
108
f , err := os .Open (file )
64
109
if err != nil {
@@ -71,20 +116,29 @@ func day15(file string) (part1, part2 int) {
71
116
robot := [2 ]int {0 , 0 }
72
117
y := 0
73
118
maxx := 0
119
+ obs := map [[2 ]int ]bool {}
120
+ boxes := [][2 ]int {}
74
121
for scanner .Scan () {
75
122
t := scanner .Text ()
76
123
if t == "" {
77
124
break
78
125
}
79
126
for x , c := range t {
127
+ coord := [2 ]int {x , y }
80
128
if c == '@' {
81
- robot = [ 2 ] int { x , y }
129
+ robot = coord
82
130
continue
83
131
}
84
132
if c == '.' {
85
133
continue
86
134
}
87
- board [[2 ]int {x , y }] = byte (c )
135
+ board [coord ] = byte (c )
136
+ if c == '#' {
137
+ obs [[2 ]int {x , y }] = true
138
+ }
139
+ if c == 'O' {
140
+ boxes = append (boxes , coord )
141
+ }
88
142
}
89
143
maxx = len (t )
90
144
y ++
@@ -95,13 +149,20 @@ func day15(file string) (part1, part2 int) {
95
149
maxx : maxx ,
96
150
maxy : y ,
97
151
halfx : false ,
152
+ obs : obs ,
153
+ boxes : boxes ,
98
154
}
99
155
b2 := Board {
100
- board : board ,
101
- robot : [2 ]int {robot [0 ] * 2 , robot [1 ]}, // robot's x is twice
102
- maxx : maxx ,
156
+ board : map [[ 2 ] int ] byte {}, // not used
157
+ robot : [2 ]int {robot [0 ] * 2 , robot [1 ]}, // fullx
158
+ maxx : maxx * 2 , // fullx
103
159
maxy : y ,
104
160
halfx : true ,
161
+ obs : obs , // halfx
162
+ boxes : boxes , // fullx
163
+ }
164
+ for k := range b2 .boxes {
165
+ b2 .boxes [k ][0 ] *= 2
105
166
}
106
167
107
168
dumpboard (b )
@@ -147,40 +208,35 @@ func day15(file string) (part1, part2 int) {
147
208
}
148
209
b:
149
210
for m , move := range moves {
150
- _ = m
151
211
if true {
152
212
fmt .Println (m )
153
- dumpboard ( b )
213
+ dumpboard2 ( b2 )
154
214
}
155
-
156
215
d := dirmap [byte (move )]
157
216
158
217
coord1 := [2 ]int {b .robot [0 ] + d [0 ], b .robot [1 ] + d [1 ]}
159
218
// Empty
160
- bb := b .board [coord1 ]
161
219
162
- switch bb {
163
- case byte (0 ):
164
- b .robot = coord1
165
- continue a
166
- case '#' :
167
- continue a
220
+ coord1m := [2 ]int {coord1 [0 ] - 1 , coord1 [1 ]}
221
+ coordhalf := [2 ]int {coord1 [0 ] / 2 , coord1 [1 ]}
222
+
223
+ if b .obs [coordhalf ] {
224
+ // blocked
225
+ fmt .Println ("Blocked" , string (move ))
226
+ continue
168
227
}
169
228
170
- l1 := coord1
171
- r1 := coord1
172
- for b .board [r1 ] == 'O' {
173
- r1 = [2 ]int {r1 [0 ] + d [0 ], r1 [1 ] + d [1 ]}
229
+ intheway := []int {}
230
+ for k , v := range b .boxes {
231
+ if v == coord1 || v == coord1m {
232
+ intheway = append (intheway , k )
233
+ }
174
234
}
175
- if b . board [ r1 ] == '#' {
176
- // Can't move.
177
- fmt . Println ( "Can't move O" )
178
- continue a
235
+ if len ( intheway ) == 0 {
236
+ fmt . Println ( "no blockj" , string ( move ))
237
+ b2 . robot = coord1
238
+ continue b
179
239
}
180
- b .board [r1 ] = 'O'
181
- delete (b .board , l1 )
182
- b .robot = coord1
183
- fmt .Println ("Move block O" )
184
240
}
185
241
}
186
242
part1 , part2 = dumpboard (b )
@@ -199,9 +255,9 @@ func main() {
199
255
defer pf .Close ()
200
256
}
201
257
t1 := time .Now ()
202
- part1 , part2 := day15 ("test .txt" )
258
+ part1 , part2 := day15 ("test3 .txt" )
203
259
fmt .Println (part1 , part2 )
204
- if part1 != 2028 {
260
+ if part1 != 2028 + 1 {
205
261
log .Fatal ("Test failed " , part1 , part2 )
206
262
}
207
263
part1 , part2 = day15 ("test2.txt" )
0 commit comments