Compare commits
11 Commits
1ce67efa35
...
7847d11f95
Author | SHA1 | Date | |
---|---|---|---|
7847d11f95 | |||
f0696a6fe6 | |||
786a5bae35 | |||
e82c9a97c4 | |||
b2da6a35e7 | |||
970f5b2d76 | |||
8e451050b1 | |||
761013df81 | |||
6a03be9ca9 | |||
eb9e770a94 | |||
13888edaff |
191
day17/main.go
191
day17/main.go
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
)
|
||||
|
@ -21,39 +22,193 @@ func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
|||
|
||||
func run(scan *bufio.Scanner) (*result, error) {
|
||||
var m Map
|
||||
var pq aoc.PriorityQueue[int, uint]
|
||||
|
||||
for scan.Scan() {
|
||||
text := scan.Text()
|
||||
m = append(m, []rune(text))
|
||||
}
|
||||
|
||||
rows := len(m)
|
||||
cols := len(m[0])
|
||||
result := result{}
|
||||
result.valuePT1 = search(m, 1, 3)
|
||||
result.valuePT2 = search(m, 4, 10)
|
||||
|
||||
END := [2]int{rows-1, cols-1}
|
||||
|
||||
return &result{}, nil
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
var (
|
||||
ZERO = [2]int{0, 0}
|
||||
ZERO = point{0, 0}
|
||||
|
||||
UP = [2]int{-1, 0}
|
||||
DN = [2]int{1, 0}
|
||||
LF = [2]int{0, -1}
|
||||
RT = [2]int{0, 1}
|
||||
UP = point{-1, 0}
|
||||
DN = point{1, 0}
|
||||
LF = point{0, -1}
|
||||
RT = point{0, 1}
|
||||
|
||||
INF = int(^uint(0) >> 1)
|
||||
)
|
||||
|
||||
type Map [][]rune
|
||||
|
||||
func (m *Map) Get(p [2]int) rune {
|
||||
if p[0] < 0 || p[0] >= len((*m)) {
|
||||
return 0
|
||||
}
|
||||
if p[1] < 0 || p[1] >= len((*m)[0]) {
|
||||
return 0
|
||||
func (m *Map) Get(p point) (point, int, bool) {
|
||||
if !m.Valid(p) {
|
||||
return [2]int{0, 0}, 0, false
|
||||
}
|
||||
|
||||
return (*m)[p[0]][p[1]]
|
||||
return p, int((*m)[p[0]][p[1]] - '0'), true
|
||||
}
|
||||
func (m *Map) GetNeighbor(p point, d point) (point, int, bool) {
|
||||
return m.Get(p.add(d))
|
||||
}
|
||||
func (m *Map) Size() (int, int) {
|
||||
if m == nil || len(*m) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
return len(*m), len((*m)[0])
|
||||
}
|
||||
func (m *Map) Neighbors(p point) []point {
|
||||
var lis []point
|
||||
for _, d := range []point{UP, DN, LF, RT} {
|
||||
if p, _, ok := m.GetNeighbor(p, d); ok {
|
||||
lis = append(lis, p)
|
||||
}
|
||||
}
|
||||
return lis
|
||||
}
|
||||
func (m *Map) NeighborDirections(p point) []point {
|
||||
var lis []point
|
||||
for _, d := range []point{UP, DN, LF, RT} {
|
||||
if m.Valid(p.add(d)) {
|
||||
lis = append(lis, d)
|
||||
}
|
||||
}
|
||||
return lis
|
||||
}
|
||||
func (m *Map) Valid(p point) bool {
|
||||
rows, cols := m.Size()
|
||||
return p[0] >= 0 && p[0] < rows && p[1] >= 0 && p[1] < cols
|
||||
}
|
||||
|
||||
type memo struct {
|
||||
h int
|
||||
s int
|
||||
p point
|
||||
d point
|
||||
}
|
||||
|
||||
func (memo) sort(a, b memo) bool {
|
||||
if a.h != b.h {
|
||||
return a.h < b.h
|
||||
}
|
||||
|
||||
if a.s != b.s {
|
||||
return a.s < b.s
|
||||
}
|
||||
|
||||
if a.p != b.p {
|
||||
return a.p.less(b.p)
|
||||
}
|
||||
|
||||
return a.d.less(b.d)
|
||||
}
|
||||
|
||||
type priorityQueue[T any, U []T] struct {
|
||||
elems U
|
||||
sort func(a, b T) bool
|
||||
}
|
||||
|
||||
func PriorityQueue[T any, U []T](sort func(a, b T) bool) *priorityQueue[T, U] {
|
||||
return &priorityQueue[T, U]{sort: sort}
|
||||
}
|
||||
func (pq *priorityQueue[T, U]) Enqueue(elem T) {
|
||||
pq.elems = append(pq.elems, elem)
|
||||
sort.Slice(pq.elems, func(i, j int) bool { return pq.sort(pq.elems[i], pq.elems[j]) })
|
||||
}
|
||||
func (pq *priorityQueue[T, I]) IsEmpty() bool {
|
||||
return len(pq.elems) == 0
|
||||
}
|
||||
func (pq *priorityQueue[T, I]) Dequeue() (T, bool) {
|
||||
var elem T
|
||||
if pq.IsEmpty() {
|
||||
return elem, false
|
||||
}
|
||||
|
||||
elem, pq.elems = pq.elems[0], pq.elems[1:]
|
||||
return elem, true
|
||||
}
|
||||
|
||||
func heuristic(m Map, p point) int {
|
||||
rows, cols := m.Size()
|
||||
return rows - p[0] + cols - p[1]
|
||||
}
|
||||
|
||||
func search(m Map, minSize, maxSize int) int {
|
||||
rows, cols := m.Size()
|
||||
END := point{rows - 1, cols - 1}
|
||||
|
||||
visited := make(map[vector]int)
|
||||
pq := PriorityQueue(memo{}.sort)
|
||||
pq.Enqueue(memo{h: heuristic(m, point{0, 0}), p: point{0, 0}, d: DN})
|
||||
|
||||
for !pq.IsEmpty() {
|
||||
mem, _ := pq.Dequeue()
|
||||
fmt.Println(mem)
|
||||
if mem.h > dmap(visited, vector{mem.p[0], mem.p[1], mem.d[0], mem.d[1]}, INF) {
|
||||
continue
|
||||
}
|
||||
|
||||
if mem.p == END {
|
||||
return mem.s
|
||||
}
|
||||
|
||||
for _, nd := range m.NeighborDirections(mem.p) {
|
||||
if nd[0] == 0 && mem.d == RT || nd[1] == 0 && mem.d == DN {
|
||||
continue
|
||||
}
|
||||
|
||||
dscore := 0
|
||||
|
||||
for _, size := range irange(1, maxSize+1) {
|
||||
np := mem.p.add(nd.scale(size))
|
||||
_, s, ok := m.Get(np)
|
||||
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
dscore += s
|
||||
pscore := mem.s + dscore
|
||||
|
||||
nh := heuristic(m, np) + pscore
|
||||
vec := vector{np[0], np[1], nd[0], nd[1]}
|
||||
|
||||
if size >= minSize && nh < dmap(visited, vec, INF) {
|
||||
pq.Enqueue(memo{nh, pscore, np, nd})
|
||||
visited[vec] = nh
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func dmap[K comparable, V any](m map[K]V, k K, d V) V {
|
||||
if v, ok := m[k]; ok {
|
||||
return v
|
||||
}
|
||||
return d
|
||||
}
|
||||
func irange(a, b int) []int {
|
||||
lis := make([]int, b-a)
|
||||
for i := range lis {
|
||||
lis[i] = i + a
|
||||
}
|
||||
return lis
|
||||
}
|
||||
|
||||
type point [2]int
|
||||
|
||||
func (p point) add(a point) point { return point{p[0] + a[0], p[1] + a[1]} }
|
||||
func (p point) scale(m int) point { return point{p[0] * m, p[1] * m} }
|
||||
func (p point) less(a point) bool { return p[0] < a[0] || p[1] < a[1] }
|
||||
|
||||
type vector [4]int
|
||||
|
|
|
@ -25,7 +25,7 @@ func TestExample(t *testing.T) {
|
|||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 102)
|
||||
is.Equal(result.valuePT2, 0)
|
||||
is.Equal(result.valuePT2, 94)
|
||||
}
|
||||
|
||||
func TestSolution(t *testing.T) {
|
||||
|
@ -36,6 +36,6 @@ func TestSolution(t *testing.T) {
|
|||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 0)
|
||||
is.Equal(result.valuePT2, 0)
|
||||
is.Equal(result.valuePT1, 843)
|
||||
is.Equal(result.valuePT2, 1017)
|
||||
}
|
||||
|
|
5
day20/example1.txt
Normal file
5
day20/example1.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
broadcaster -> a, b, c
|
||||
%a -> b
|
||||
%b -> c
|
||||
%c -> inv
|
||||
&inv -> a
|
5
day20/example2.txt
Normal file
5
day20/example2.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
broadcaster -> a
|
||||
%a -> inv, con
|
||||
&inv -> b
|
||||
%b -> con
|
||||
&con -> output
|
58
day20/input.txt
Normal file
58
day20/input.txt
Normal file
|
@ -0,0 +1,58 @@
|
|||
%vg -> lf, vd
|
||||
%dr -> kg
|
||||
%cn -> mv, pt
|
||||
%rq -> bk, gr
|
||||
%vp -> lp, bk
|
||||
%kg -> lv
|
||||
%lv -> jc, tp
|
||||
%sj -> rm, vd
|
||||
%jc -> tp, qr
|
||||
%km -> tp, dr
|
||||
%jx -> cn
|
||||
&vd -> tf, lf, nb, cx, hx, lr
|
||||
%lp -> jt, bk
|
||||
%vj -> ps
|
||||
broadcaster -> km, lr, xh, rf
|
||||
%dj -> pt, gc
|
||||
%cg -> vd, hx
|
||||
&ln -> tg
|
||||
%fl -> pt, sk
|
||||
%lm -> tr, bk
|
||||
%lr -> vd, vg
|
||||
&pt -> vq, rf, cm, jx, rg
|
||||
%cx -> gp
|
||||
%gp -> vd, sj
|
||||
&db -> tg
|
||||
%st -> vd
|
||||
%jt -> bk
|
||||
%jh -> lm, bk
|
||||
%xf -> bd, tp
|
||||
%gc -> cm, pt
|
||||
&tp -> dr, km, kg, db, vj, qr
|
||||
%ps -> xf, tp
|
||||
%rf -> pt, dj
|
||||
%lf -> nb
|
||||
%bd -> tp, gg
|
||||
%dk -> tp, vj
|
||||
%mn -> jh, bk
|
||||
&tg -> rx
|
||||
%ql -> bk, zx
|
||||
%tr -> bk, vp
|
||||
%sk -> pt
|
||||
%nb -> cg
|
||||
%sb -> vd, cx
|
||||
%qr -> dk
|
||||
%xh -> bk, ql
|
||||
%rg -> sd
|
||||
%hx -> sb
|
||||
%sd -> pt, jx
|
||||
%gr -> bk, mn
|
||||
%gg -> tp
|
||||
%zx -> rq
|
||||
&bk -> xh, ln, zx
|
||||
%rm -> st, vd
|
||||
%hq -> fl, pt
|
||||
&vq -> tg
|
||||
%cm -> rg
|
||||
&tf -> tg
|
||||
%mv -> pt, hq
|
282
day20/main.go
Normal file
282
day20/main.go
Normal file
|
@ -0,0 +1,282 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
// var log = aoc.Log
|
||||
|
||||
func main() { aoc.MustResult(aoc.Runner(run)) }
|
||||
|
||||
type result struct {
|
||||
valuePT1 int
|
||||
valuePT2 int
|
||||
}
|
||||
|
||||
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||
|
||||
func run(scan *bufio.Scanner) (*result, error) {
|
||||
m := &machine{}
|
||||
receivers := make(map[string][]string)
|
||||
|
||||
for scan.Scan() {
|
||||
text := scan.Text()
|
||||
|
||||
name, text, _ := strings.Cut(text, " -> ")
|
||||
dest := strings.Split(text, ", ")
|
||||
|
||||
switch {
|
||||
case name == "broadcaster":
|
||||
m.Add(name, &broadcaster{dest: dest})
|
||||
case strings.HasPrefix(name, "%"):
|
||||
name = strings.TrimPrefix(name, "%")
|
||||
m.Add(name, &flipflop{name: name, dest: dest})
|
||||
|
||||
case strings.HasPrefix(name, "&"):
|
||||
name = strings.TrimPrefix(name, "&")
|
||||
m.Add(name, &conjunction{name: name, dest: dest})
|
||||
}
|
||||
|
||||
for _, d := range dest {
|
||||
// rx is present so enable pt 2
|
||||
if d == "rx" {
|
||||
m.Add("rx", &rx{})
|
||||
}
|
||||
receivers[d] = append(receivers[d], name)
|
||||
}
|
||||
}
|
||||
|
||||
m.setup(receivers)
|
||||
|
||||
result := &result{}
|
||||
|
||||
for i := 0; i < 10_000; i++ { // need enough presses to find the best LCM values for each conjunction
|
||||
if i == 1000 {
|
||||
result.valuePT1 = m.highPulses * m.lowPulses
|
||||
}
|
||||
m.Push(i)
|
||||
}
|
||||
|
||||
// rx is present.. perform part 2.
|
||||
if rx, ok := receivers["rx"]; ok {
|
||||
tip := m.m[rx[0]].(*conjunction) // panic if missing!
|
||||
|
||||
var lvalues []int
|
||||
for k, v := range tip.pushes {
|
||||
for i, h := range makeHistory(v) {
|
||||
if i == 1 && len(h) > 0 && h[0] > 0 {
|
||||
fmt.Println(tip.name, k, "frequency", h[0])
|
||||
lvalues = append(lvalues, h[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.valuePT2 = aoc.LCM(lvalues...)
|
||||
fmt.Println(tip.name, "LCM", result.valuePT2, lvalues)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type signal bool
|
||||
|
||||
const (
|
||||
LOW signal = false
|
||||
HIGH signal = true
|
||||
)
|
||||
|
||||
type message struct {
|
||||
signal
|
||||
from, to string
|
||||
}
|
||||
|
||||
type machine struct {
|
||||
m map[string]pulser
|
||||
|
||||
queue []message
|
||||
|
||||
press int
|
||||
highPulses int
|
||||
lowPulses int
|
||||
}
|
||||
|
||||
func (m *machine) Add(name string, p pulser) {
|
||||
if m.m == nil {
|
||||
m.m = make(map[string]pulser)
|
||||
}
|
||||
p.SetMachine(m)
|
||||
m.m[name] = p
|
||||
}
|
||||
func (m *machine) Send(msgs ...message) {
|
||||
m.queue = append(m.queue, msgs...)
|
||||
for _, msg := range msgs {
|
||||
// fmt.Println(msg)
|
||||
if msg.signal {
|
||||
m.highPulses++
|
||||
} else {
|
||||
m.lowPulses++
|
||||
}
|
||||
}
|
||||
}
|
||||
func (m *machine) Push(i int) {
|
||||
m.press = i
|
||||
m.Send(generate(LOW, "button", "broadcaster")...)
|
||||
m.processQueue(i)
|
||||
}
|
||||
func (m *machine) processQueue(i int) {
|
||||
// look for work and process up to the queue length. repeat.
|
||||
hwm := 0
|
||||
for hwm < len(m.queue) {
|
||||
end := len(m.queue)
|
||||
|
||||
for ; hwm < end; hwm++ {
|
||||
msg := m.queue[hwm]
|
||||
|
||||
if p, ok := m.m[msg.to]; ok {
|
||||
// fmt.Println(i, "S:", m.m[msg.from], msg.signal, "R:", p)
|
||||
p.Pulse(msg)
|
||||
}
|
||||
}
|
||||
|
||||
hwm = 0
|
||||
copy(m.queue, m.queue[end:])
|
||||
m.queue = m.queue[:len(m.queue)-end]
|
||||
// fmt.Println("")
|
||||
}
|
||||
}
|
||||
func (m *machine) setup(receivers map[string][]string) {
|
||||
for name, recv := range receivers {
|
||||
if p, ok := m.m[name]; ok {
|
||||
if p, ok := p.(interface{ Receive(...string) }); ok {
|
||||
p.Receive(recv...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type pulser interface {
|
||||
Pulse(message)
|
||||
SetMachine(*machine)
|
||||
}
|
||||
|
||||
// IsModule implements the machine registration for each module.
|
||||
type IsModule struct {
|
||||
*machine
|
||||
}
|
||||
|
||||
func (p *IsModule) SetMachine(m *machine) { p.machine = m }
|
||||
|
||||
type broadcaster struct {
|
||||
dest []string
|
||||
IsModule
|
||||
}
|
||||
|
||||
func (b *broadcaster) Pulse(msg message) {
|
||||
b.Send(generate(msg.signal, "broadcaster", b.dest...)...)
|
||||
}
|
||||
|
||||
type flipflop struct {
|
||||
name string
|
||||
state signal
|
||||
dest []string
|
||||
|
||||
IsModule
|
||||
}
|
||||
|
||||
func (b *flipflop) Pulse(msg message) {
|
||||
if !msg.signal {
|
||||
b.state = !b.state
|
||||
b.Send(generate(b.state, b.name, b.dest...)...)
|
||||
}
|
||||
}
|
||||
|
||||
type conjunction struct {
|
||||
name string
|
||||
state map[string]signal
|
||||
dest []string
|
||||
|
||||
pushes map[string][]int
|
||||
|
||||
IsModule
|
||||
}
|
||||
|
||||
func (b *conjunction) Receive(names ...string) {
|
||||
if b.state == nil {
|
||||
b.state = make(map[string]signal)
|
||||
b.pushes = make(map[string][]int)
|
||||
}
|
||||
for _, name := range names {
|
||||
b.state[name] = false
|
||||
b.pushes[name] = []int{}
|
||||
}
|
||||
}
|
||||
func (b *conjunction) Pulse(msg message) {
|
||||
b.state[msg.from] = msg.signal
|
||||
|
||||
if msg.signal {
|
||||
// collect frequency of pushes to esti,ate rate
|
||||
b.pushes[msg.from] = append(b.pushes[msg.from], b.press)
|
||||
}
|
||||
|
||||
if all(HIGH, maps.Values(b.state)...) {
|
||||
b.Send(generate(LOW, b.name, b.dest...)...)
|
||||
return
|
||||
}
|
||||
b.Send(generate(HIGH, b.name, b.dest...)...)
|
||||
}
|
||||
|
||||
type rx struct {
|
||||
IsModule
|
||||
}
|
||||
|
||||
func (rx *rx) Pulse(msg message) {
|
||||
if !msg.signal {
|
||||
panic("pulse received") // will never happen...
|
||||
}
|
||||
}
|
||||
|
||||
// helper funcs
|
||||
func all[T comparable](match T, lis ...T) bool {
|
||||
for _, b := range lis {
|
||||
if b != match {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
func generate(t signal, from string, destinations ...string) []message {
|
||||
msgs := make([]message, len(destinations))
|
||||
for i, to := range destinations {
|
||||
msgs[i] = message{signal: t, from: from, to: to}
|
||||
}
|
||||
return msgs
|
||||
}
|
||||
|
||||
// makeHistory from day 9
|
||||
func makeHistory(in []int) [][]int {
|
||||
var history [][]int
|
||||
history = append(history, in)
|
||||
|
||||
// for {
|
||||
var diffs []int
|
||||
|
||||
current := history[len(history)-1]
|
||||
|
||||
for i := range current[1:] {
|
||||
diffs = append(diffs, current[i+1]-current[i])
|
||||
}
|
||||
|
||||
history = append(history, diffs)
|
||||
|
||||
// if len(diffs) == 0 || aoc.Max(diffs[0], diffs[1:]...) == 0 && aoc.Min(diffs[0], diffs[1:]...) == 0 {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
return history
|
||||
}
|
56
day20/main_test.go
Normal file
56
day20/main_test.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/matryer/is"
|
||||
)
|
||||
|
||||
//go:embed example1.txt
|
||||
var example1 []byte
|
||||
|
||||
//go:embed example2.txt
|
||||
var example2 []byte
|
||||
|
||||
//go:embed input.txt
|
||||
var input []byte
|
||||
|
||||
func TestExample1(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(example1))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 32000000)
|
||||
is.Equal(result.valuePT2, 0)
|
||||
}
|
||||
|
||||
func TestExample2(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(example2))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 11687500)
|
||||
is.Equal(result.valuePT2, 0)
|
||||
}
|
||||
|
||||
func TestSolution(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(input))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 819397964)
|
||||
is.Equal(result.valuePT2, 252667369442479)
|
||||
}
|
36
tools.go
36
tools.go
|
@ -51,10 +51,10 @@ func Reverse[T any](arr []T) []T {
|
|||
return arr
|
||||
}
|
||||
|
||||
type uinteger interface{
|
||||
type uinteger interface {
|
||||
uint | uint8 | uint16 | uint32 | uint64
|
||||
}
|
||||
type sinteger interface{
|
||||
type sinteger interface {
|
||||
int | int8 | int16 | int32 | int64
|
||||
}
|
||||
type integer interface {
|
||||
|
@ -342,21 +342,22 @@ type Vertex[V comparable, I integer] struct {
|
|||
type graph[V comparable, I uinteger] struct {
|
||||
adj map[V][]Vertex[V, I]
|
||||
}
|
||||
func Graph[V comparable, I uinteger](size int) *graph[V,I] {
|
||||
return &graph[V,I]{
|
||||
adj: make(map[V][]Vertex[V,I], size),
|
||||
|
||||
func Graph[V comparable, I uinteger](size int) *graph[V, I] {
|
||||
return &graph[V, I]{
|
||||
adj: make(map[V][]Vertex[V, I], size),
|
||||
}
|
||||
}
|
||||
func (g *graph[V,I]) AddEdge(u, v V, w I) {
|
||||
func (g *graph[V, I]) AddEdge(u, v V, w I) {
|
||||
g.adj[u] = append(g.adj[u], Vertex[V, I]{to: v, score: w})
|
||||
g.adj[v] = append(g.adj[v], Vertex[V, I]{to: u, score: w})
|
||||
}
|
||||
func (g *graph[V,I]) Dijkstra(src V) {
|
||||
pq := PriorityQueue[V,I]{}
|
||||
func (g *graph[V, I]) Dijkstra(m interface{Get()}, src V) map[V]I {
|
||||
pq := PriorityQueue[V, I]{}
|
||||
dist := make(map[V]I, len(g.adj))
|
||||
visited := make(map[V]bool, len(g.adj))
|
||||
var INF I
|
||||
INF = ^INF>>1
|
||||
INF = ^INF
|
||||
|
||||
pq.Enqueue(src, 0)
|
||||
dist[src] = 0
|
||||
|
@ -373,23 +374,22 @@ func (g *graph[V,I]) Dijkstra(src V) {
|
|||
_, ok := visited[v.to]
|
||||
var du, dv I
|
||||
if d, inf := dist[u]; !inf {
|
||||
du=INF
|
||||
du = INF
|
||||
} else {
|
||||
du = d
|
||||
}
|
||||
if d, inf := dist[v.to]; !inf {
|
||||
dv=INF
|
||||
dv = INF
|
||||
} else {
|
||||
dv = d
|
||||
}
|
||||
}
|
||||
|
||||
if !ok && du + v.score < dv {
|
||||
if !ok && du+v.score < dv {
|
||||
dist[v.to] = du + v.score
|
||||
pq.Enqueue(v.to, du + v.score)
|
||||
pq.Enqueue(v.to, du+v.score)
|
||||
}
|
||||
}
|
||||
}
|
||||
for v, w := range dist {
|
||||
fmt.Printf("%v, %v\n", v, w)
|
||||
}
|
||||
}
|
||||
|
||||
return dist
|
||||
}
|
||||
|
|
|
@ -89,5 +89,5 @@ func TestGraph(t *testing.T) {
|
|||
g.AddEdge(3, 5, 15)
|
||||
g.AddEdge(4, 6, 2)
|
||||
g.AddEdge(5, 6, 6)
|
||||
g.Dijkstra(0)
|
||||
// g.Dijkstra(0)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user