mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
restructured data-structures folder, changed heap_test.go to external tests (package -> heap_test)
This commit is contained in:
+3
-3
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/datastructures"
|
||||
"github.com/alexchao26/advent-of-code-go/data-structures/heap"
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
@@ -114,7 +114,7 @@ func part2(input string) int {
|
||||
depth, targetX, targetY := parseInput(input)
|
||||
regionCalculator := memoRegionTypeCalculator(depth, targetX, targetY)
|
||||
|
||||
heap := datastructures.NewMinHeap()
|
||||
heap := heap.NewMinHeap()
|
||||
firstNode := node{
|
||||
coords: [2]int{0, 0},
|
||||
regionType: regionCalculator(0, 0),
|
||||
@@ -171,7 +171,7 @@ func (n node) Value() int {
|
||||
return n.totalTime
|
||||
}
|
||||
|
||||
func step(heap *datastructures.MinHeap, eqCoordsToMinDist map[equipmentType]map[[2]int]int, regionCalculator func(x, y int) regionType) node {
|
||||
func step(heap *heap.MinHeap, eqCoordsToMinDist map[equipmentType]map[[2]int]int, regionCalculator func(x, y int) regionType) node {
|
||||
// remove node from heap, this will be returned at the end
|
||||
minNodeInterface := heap.Remove()
|
||||
if minNodeInterface == nil {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package datastructures
|
||||
package heap
|
||||
|
||||
// MinHeap is an implementation of a min heap
|
||||
type MinHeap struct {
|
||||
@@ -47,6 +47,14 @@ type heapNode interface {
|
||||
Value() int
|
||||
}
|
||||
|
||||
// Front returns the first node in the heap, nil if the heap is empty
|
||||
func (h *heap) Front() heapNode {
|
||||
if len(h.nodes) == 0 {
|
||||
return nil
|
||||
}
|
||||
return h.nodes[0]
|
||||
}
|
||||
|
||||
// Add appends a new node onto the heap and heapifies it
|
||||
// to ensure correct ordering
|
||||
func (h *heap) Add(newNode heapNode) {
|
||||
@@ -1,7 +1,9 @@
|
||||
package datastructures
|
||||
package heap_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/data-structures/heap"
|
||||
)
|
||||
|
||||
type mockNode int
|
||||
@@ -11,21 +13,26 @@ func (n mockNode) Value() int {
|
||||
}
|
||||
|
||||
func TestMinHeap(t *testing.T) {
|
||||
h := NewMinHeap()
|
||||
h := heap.NewMinHeap()
|
||||
h.Add(mockNode(5))
|
||||
h.Add(mockNode(93))
|
||||
|
||||
if h.nodes[0].Value() != 5 {
|
||||
t.Errorf("After adding 5, h.nodes[0].Value() = %d, want 5", h.nodes[0].Value())
|
||||
if h.Front().(mockNode).Value() != 5 {
|
||||
t.Errorf("After adding 5, h.Front().Value() = %d, want 5", h.Front().Value())
|
||||
}
|
||||
if h.nodes[1].Value() != 93 {
|
||||
t.Errorf("After adding 93, h.nodes[1].Value() = %d, want 93", h.nodes[1].Value())
|
||||
if h.Front().(mockNode).Value() != 5 {
|
||||
t.Errorf("After adding 5 & 93, h.Front() = %d, want 5", h.Front().Value())
|
||||
}
|
||||
|
||||
// Add a bunch of nodes, make sure they are removed in order
|
||||
h.Add(mockNode(10))
|
||||
h.Add(mockNode(2))
|
||||
h.Add(mockNode(1))
|
||||
|
||||
if h.Front().(mockNode).Value() != 1 {
|
||||
t.Errorf("After adding 5, 93, 10, 2 & 1, h.Front() = %d, want 1", h.Front().Value())
|
||||
}
|
||||
|
||||
h.Add(mockNode(3))
|
||||
h.Add(mockNode(4))
|
||||
h.Add(mockNode(123))
|
||||
@@ -41,15 +48,15 @@ func TestMinHeap(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMaxHeap(t *testing.T) {
|
||||
h := NewMaxHeap()
|
||||
h := heap.NewMaxHeap()
|
||||
h.Add(mockNode(5))
|
||||
h.Add(mockNode(93))
|
||||
|
||||
if h.nodes[0].Value() != 93 {
|
||||
t.Errorf("After adding 93, h.nodes[0].Value() = %d, want 93", h.nodes[1].Value())
|
||||
if h.Front().Value() != 93 {
|
||||
t.Errorf("After adding 93, h.Front().Value() = %d, want 93", h.Front().Value())
|
||||
}
|
||||
if h.nodes[1].Value() != 5 {
|
||||
t.Errorf("After adding 5, h.nodes[1].Value() = %d, want 5", h.nodes[0].Value())
|
||||
if h.Front().Value() != 93 {
|
||||
t.Errorf("After adding 93 & 5, h.Front().Value() = %d, want 93", h.Front().Value())
|
||||
}
|
||||
|
||||
// Add a bunch of nodes, make sure they are removed in order
|
||||
@@ -59,6 +66,10 @@ func TestMaxHeap(t *testing.T) {
|
||||
h.Add(mockNode(3))
|
||||
h.Add(mockNode(4))
|
||||
h.Add(mockNode(123))
|
||||
if h.Front().(mockNode).Value() != 123 {
|
||||
t.Errorf("After adding 5, 93, 10, 2, 1, 3, 4 & 123, h.Front() = %d, want 123", h.Front().Value())
|
||||
}
|
||||
|
||||
h.Add(mockNode(32))
|
||||
h.Add(mockNode(-15))
|
||||
|
||||
Reference in New Issue
Block a user