From 0abdabad645fcfef80a2068f560979395b7efe71 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Wed, 10 Dec 2025 07:21:28 +0000 Subject: [PATCH] still too slow --- day10/sol2.go | 57 ++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/day10/sol2.go b/day10/sol2.go index ee6e1d2..44d496f 100644 --- a/day10/sol2.go +++ b/day10/sol2.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "container/heap" "fmt" "log" "os" @@ -114,40 +113,38 @@ func (h *CostHeap) Pop() any { return x } +// it doesn't matter what order we push the buttons in -- +// only how many times we push each one. + func best(target Jolts, pool []Jolts) int { var cost = make(map[Jolts]int) - h := &CostHeap{nil, cost} - heap.Push(h, Jolts{}) - for h.Len() > 0 { - s := heap.Pop(h).(Jolts) - if s == target { - break - } + var states []Jolts + states = append(states, Jolts{}) + cost[Jolts{}] = 0 + for pi, p := range pool { // enumerate all the states that can be reached by toggling button p - next: - for _, p := range pool { - t := s - for i, v := range p { - t[i] += v - if t[i] > target[i] { - // blown target, cut path - continue next - } - } - - if cost_t, ok := cost[t]; ok { - if cost[s]+1 < cost_t { - cost[t] = cost[s] + 1 - for i, x := range h.heap { - if x == t { - heap.Fix(h, i) - break - } + queue := states + fmt.Print(pi, len(states), len(queue)) + for _, s := range queue { + here: + for t, c := s, cost[s]; ; { + for i, v := range p { + t[i] += v + if t[i] > target[i] { + // blown target, cut path + break here } } - } else { - cost[t] = cost[s] + 1 - heap.Push(h, t) + c += 1 + + if cost_t, ok := cost[t]; ok { + if c < cost_t { + cost[t] = c + } + } else { + cost[t] = c + states = append(states, t) + } } } }