Skip to content

Commit c6195f3

Browse files
committed
chore(angch/2024-16): WIP part2
1 parent 53d809f commit c6195f3

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

angch/2024-16/main.go

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ func day16(file string) (part1, part2 int) {
106106
eval := []Moves{moves}
107107
evals := 0
108108
bestScore := -1
109+
bestMoves := [][2]int{}
110+
bestScoreMap := map[[2]int][]int{}
109111
t2 := time.Now()
110112
done := map[[2]int]int{}
111113
for len(eval) > 0 {
@@ -134,7 +136,15 @@ func day16(file string) (part1, part2 int) {
134136
if moves.move[len(moves.move)-1] == end {
135137
if bestScore == -1 || moves.score < bestScore {
136138
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+
}
138148
}
139149
continue
140150
}
@@ -151,17 +161,7 @@ func day16(file string) (part1, part2 int) {
151161
continue
152162
}
153163

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 {
165165
continue
166166
}
167167

@@ -173,6 +173,25 @@ func day16(file string) (part1, part2 int) {
173173
}
174174
// fmt.Println("Facing is ", facing, "dirIndex is", dirIndex, "addScore is", addScore)
175175

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+
176195
cloneMap := make(map[[2]int]int)
177196
for k, v := range moves.movemap {
178197
cloneMap[k] = v
@@ -197,21 +216,33 @@ func day16(file string) (part1, part2 int) {
197216
})
198217
}
199218
}
200-
log.Println("no more moves", evals)
219+
// log.Println("no more moves", evals)
201220
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)
202228

203229
return
204230
}
205231

206232
func main() {
207233
log.SetFlags(log.Lshortfile | log.LstdFlags)
208234
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
215246
fmt.Println(day16("input.txt"))
216247
fmt.Println("Elapsed time:", time.Since(t1))
217248
}

0 commit comments

Comments
 (0)