@@ -106,6 +106,8 @@ func day16(file string) (part1, part2 int) {
106
106
eval := []Moves {moves }
107
107
evals := 0
108
108
bestScore := - 1
109
+ bestMoves := [][2 ]int {}
110
+ bestScoreMap := map [[2 ]int ][]int {}
109
111
t2 := time .Now ()
110
112
done := map [[2 ]int ]int {}
111
113
for len (eval ) > 0 {
@@ -134,7 +136,15 @@ func day16(file string) (part1, part2 int) {
134
136
if moves .move [len (moves .move )- 1 ] == end {
135
137
if bestScore == - 1 || moves .score < bestScore {
136
138
bestScore = moves .score
137
- log .Fatal ("best score" , bestScore )
139
+ bestMoves = moves .move
140
+ log .Println ("Best score" , moves .score , len (eval ))
141
+ } else {
142
+ if moves .score == bestScore {
143
+ log .Println ("Found another best score" , moves .score , len (eval ))
144
+ bestMoves = append (bestMoves , moves .move ... )
145
+ } else {
146
+ continue
147
+ }
138
148
}
139
149
continue
140
150
}
@@ -151,17 +161,7 @@ func day16(file string) (part1, part2 int) {
151
161
continue
152
162
}
153
163
154
- if olddir , ok := done [newPos ]; ok {
155
- if olddir & (1 << dirIndex ) > 0 {
156
- continue
157
- }
158
- }
159
-
160
- if olddir , ok := moves .movemap [newPos ]; ok {
161
- if olddir == dirIndex {
162
- continue
163
- }
164
- // _ = olddir
164
+ if _ , ok := moves .movemap [newPos ]; ok {
165
165
continue
166
166
}
167
167
@@ -173,6 +173,25 @@ func day16(file string) (part1, part2 int) {
173
173
}
174
174
// fmt.Println("Facing is ", facing, "dirIndex is", dirIndex, "addScore is", addScore)
175
175
176
+ // This check kills part 2:
177
+ if false {
178
+ _ , ok := bestScoreMap [newPos ]
179
+ if ! ok {
180
+ bestScoreMap [newPos ] = make ([]int , 4 )
181
+ }
182
+ score := bestScoreMap [newPos ][dirIndex ]
183
+ if score > moves .score + addScore {
184
+ continue
185
+ }
186
+ bestScoreMap [newPos ][dirIndex ] = moves .score + addScore
187
+
188
+ // if olddir, ok := done[newPos]; ok {
189
+ // if olddir&(1<<dirIndex) > 0 {
190
+ // continue
191
+ // }
192
+ // }
193
+ }
194
+
176
195
cloneMap := make (map [[2 ]int ]int )
177
196
for k , v := range moves .movemap {
178
197
cloneMap [k ] = v
@@ -197,21 +216,33 @@ func day16(file string) (part1, part2 int) {
197
216
})
198
217
}
199
218
}
200
- log .Println ("no more moves" , evals )
219
+ // log.Println("no more moves", evals)
201
220
part1 = bestScore
221
+ // log.Println(bestMoves)
222
+
223
+ seen := map [[2 ]int ]bool {}
224
+ for _ , m := range bestMoves {
225
+ seen [m ] = true
226
+ }
227
+ part2 = len (seen )
202
228
203
229
return
204
230
}
205
231
206
232
func main () {
207
233
log .SetFlags (log .Lshortfile | log .LstdFlags )
208
234
t1 := time .Now ()
209
- // part1, part2 := day16("test.txt")
210
- // fmt.Println(part1, part2)
211
- // if part1 != 7036 || part2 != 0 {
212
- // log.Fatal("Test failed ", part1, part2)
213
- // }
214
- // 7036 too low
235
+ part1 , part2 := day16 ("test.txt" )
236
+ fmt .Println (part1 , part2 )
237
+ if part1 != 7036 || part2 != 45 {
238
+ log .Fatal ("Test failed " , part1 , part2 )
239
+ }
240
+ part1 , part2 = day16 ("test2.txt" )
241
+ fmt .Println (part1 , part2 )
242
+ if part1 != 11048 || part2 != 64 {
243
+ log .Fatal ("Test failed " , part1 , part2 )
244
+ }
245
+ // part2: 511 too high
215
246
fmt .Println (day16 ("input.txt" ))
216
247
fmt .Println ("Elapsed time:" , time .Since (t1 ))
217
248
}
0 commit comments