Compare commits
3 Commits
day23
...
37c999e331
| Author | SHA1 | Date | |
|---|---|---|---|
| 37c999e331 | |||
| 5e0889010a | |||
|
724fb14ff2
|
@@ -28,6 +28,6 @@ jobs:
|
|||||||
go-version: 1.21.3
|
go-version: 1.21.3
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: go test -timeout 240s -race -cover ./...
|
run: go test --race -cover ./...
|
||||||
|
|
||||||
- run: echo "🍏 This job's status is ${{ job.status }}."
|
- run: echo "🍏 This job's status is ${{ job.status }}."
|
||||||
|
|||||||
300
aoc_test.go
300
aoc_test.go
@@ -9,6 +9,66 @@ import (
|
|||||||
aoc "go.sour.is/advent-of-code"
|
aoc "go.sour.is/advent-of-code"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestReverse(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
is.Equal(aoc.Reverse([]int{1, 2, 3, 4}), []int{4, 3, 2, 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLCM(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
is.Equal(aoc.LCM([]int{}...), 0)
|
||||||
|
is.Equal(aoc.LCM(5), 5)
|
||||||
|
is.Equal(aoc.LCM(5, 3), 15)
|
||||||
|
is.Equal(aoc.LCM(5, 3, 2), 30)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadStringToInts(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
is.Equal(aoc.ReadStringToInts([]string{"1", "2", "3"}), []int{1, 2, 3})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepeat(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
is.Equal(aoc.Repeat(5, 3), []int{5, 5, 5})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPower2(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
is.Equal(aoc.Power2(0), 1)
|
||||||
|
is.Equal(aoc.Power2(1), 2)
|
||||||
|
is.Equal(aoc.Power2(2), 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestABS(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
is.Equal(aoc.ABS(1), 1)
|
||||||
|
is.Equal(aoc.ABS(0), 0)
|
||||||
|
is.Equal(aoc.ABS(-1), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTranspose(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
is.Equal(
|
||||||
|
aoc.Transpose(
|
||||||
|
[][]int{
|
||||||
|
{1, 1},
|
||||||
|
{0, 0},
|
||||||
|
{1, 1},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
[][]int{
|
||||||
|
{1, 0, 1},
|
||||||
|
{1, 0, 1},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func TestList(t *testing.T) {
|
func TestList(t *testing.T) {
|
||||||
is := is.New(t)
|
is := is.New(t)
|
||||||
@@ -25,44 +85,75 @@ func TestPriorityQueue(t *testing.T) {
|
|||||||
is := is.New(t)
|
is := is.New(t)
|
||||||
|
|
||||||
type elem [2]int
|
type elem [2]int
|
||||||
less := func(b, a *elem) bool {
|
less := func(a, b elem) bool {
|
||||||
return (*a)[0] < (*b)[0]
|
return b[0] < a[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
pq := aoc.PriorityQueue(less)
|
pq := aoc.PriorityQueue(less)
|
||||||
|
|
||||||
pq.Insert(&elem{1, 4})
|
pq.Enqueue(elem{1, 4})
|
||||||
pq.Insert(&elem{3, 2})
|
pq.Enqueue(elem{3, 2})
|
||||||
pq.Insert(&elem{2, 3})
|
pq.Enqueue(elem{2, 3})
|
||||||
pq.Insert(&elem{4, 1})
|
pq.Enqueue(elem{4, 1})
|
||||||
|
|
||||||
v := pq.ExtractMin()
|
v, ok := pq.Dequeue()
|
||||||
is.True(v != nil)
|
is.True(ok)
|
||||||
is.Equal(v, &elem{4, 1})
|
is.Equal(v, elem{4, 1})
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
v, ok = pq.Dequeue()
|
||||||
is.True(v != nil)
|
is.True(ok)
|
||||||
is.Equal(v, &elem{3, 2})
|
is.Equal(v, elem{3, 2})
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
v, ok = pq.Dequeue()
|
||||||
is.True(v != nil)
|
is.True(ok)
|
||||||
is.Equal(v, &elem{2, 3})
|
is.Equal(v, elem{2, 3})
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
v, ok = pq.Dequeue()
|
||||||
is.True(v != nil)
|
is.True(ok)
|
||||||
is.Equal(v, &elem{1, 4})
|
is.Equal(v, elem{1, 4})
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
v, ok = pq.Dequeue()
|
||||||
is.True(v == nil)
|
is.True(!ok)
|
||||||
|
is.Equal(v, elem{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSet(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
|
||||||
|
s := aoc.Set(1, 2, 3)
|
||||||
|
is.True(!s.Has(0))
|
||||||
|
is.True(s.Has(1))
|
||||||
|
is.True(s.Has(2))
|
||||||
|
is.True(s.Has(3))
|
||||||
|
is.True(!s.Has(4))
|
||||||
|
|
||||||
|
s.Add(4)
|
||||||
|
is.True(s.Has(4))
|
||||||
|
|
||||||
|
items := s.Items()
|
||||||
|
sort.Ints(items)
|
||||||
|
is.Equal(items, []int{1, 2, 3, 4})
|
||||||
|
}
|
||||||
|
|
||||||
|
// func TestGraph(t *testing.T) {
|
||||||
|
// g := aoc.Graph[int, uint](7)
|
||||||
|
// g.AddEdge(0, 1, 2)
|
||||||
|
// g.AddEdge(0, 2, 6)
|
||||||
|
// g.AddEdge(1, 3, 5)
|
||||||
|
// g.AddEdge(2, 3, 8)
|
||||||
|
// g.AddEdge(3, 4, 10)
|
||||||
|
// g.AddEdge(3, 5, 15)
|
||||||
|
// g.AddEdge(4, 6, 2)
|
||||||
|
// g.AddEdge(5, 6, 6)
|
||||||
|
// // g.Dijkstra(0)
|
||||||
|
// }
|
||||||
|
|
||||||
func ExamplePriorityQueue() {
|
func ExamplePriorityQueue() {
|
||||||
type memo struct {
|
type memo struct {
|
||||||
pt int
|
pt int
|
||||||
score int
|
score int
|
||||||
}
|
}
|
||||||
less := func(a, b *memo) bool { return a.score < b.score }
|
less := func(a, b memo) bool { return a.score < b.score }
|
||||||
|
|
||||||
adj := map[int][][2]int{
|
adj := map[int][][2]int{
|
||||||
0: {{1, 2}, {2, 6}},
|
0: {{1, 2}, {2, 6}},
|
||||||
@@ -78,10 +169,10 @@ func ExamplePriorityQueue() {
|
|||||||
dist := aoc.DefaultMap[int](int(^uint(0) >> 1))
|
dist := aoc.DefaultMap[int](int(^uint(0) >> 1))
|
||||||
|
|
||||||
dist.Set(0, 0)
|
dist.Set(0, 0)
|
||||||
pq.Insert(&memo{0, 0})
|
pq.Enqueue(memo{0, 0})
|
||||||
|
|
||||||
for !pq.IsEmpty() {
|
for !pq.IsEmpty() {
|
||||||
m := pq.ExtractMin()
|
m, _ := pq.Dequeue()
|
||||||
|
|
||||||
u := m.pt
|
u := m.pt
|
||||||
if visited.Has(u) {
|
if visited.Has(u) {
|
||||||
@@ -97,7 +188,7 @@ func ExamplePriorityQueue() {
|
|||||||
|
|
||||||
if !visited.Has(v) && du+w < dv {
|
if !visited.Has(v) && du+w < dv {
|
||||||
dist.Set(v, du+w)
|
dist.Set(v, du+w)
|
||||||
pq.Insert(&memo{v, du + w})
|
pq.Enqueue(memo{v, du + w})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,164 +208,3 @@ func ExamplePriorityQueue() {
|
|||||||
// point 5 is 22 steps away.
|
// point 5 is 22 steps away.
|
||||||
// point 6 is 19 steps away.
|
// point 6 is 19 steps away.
|
||||||
}
|
}
|
||||||
func TestGraph(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
var adjacencyList = map[int][]int{
|
|
||||||
2: {3, 5, 1},
|
|
||||||
1: {2, 4},
|
|
||||||
3: {6, 2},
|
|
||||||
4: {1, 5, 7},
|
|
||||||
5: {2, 6, 8, 4},
|
|
||||||
6: {3, 0, 9, 5},
|
|
||||||
7: {4, 8},
|
|
||||||
8: {5, 9, 7},
|
|
||||||
9: {6, 0, 8},
|
|
||||||
}
|
|
||||||
|
|
||||||
g := aoc.Graph(aoc.WithAdjacencyList[int, int](adjacencyList))
|
|
||||||
is.Equal(g.Neighbors(1), []int{2, 4})
|
|
||||||
is.Equal(map[int][]int(g.AdjacencyList()), adjacencyList)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleFibHeap() {
|
|
||||||
type memo struct {
|
|
||||||
pt int
|
|
||||||
score int
|
|
||||||
}
|
|
||||||
less := func(a, b *memo) bool { return (*a).score < (*b).score }
|
|
||||||
|
|
||||||
adj := map[int][][2]int{
|
|
||||||
0: {{1, 2}, {2, 6}},
|
|
||||||
1: {{3, 5}},
|
|
||||||
2: {{3, 8}},
|
|
||||||
3: {{4, 10}, {5, 15}},
|
|
||||||
4: {{6, 2}},
|
|
||||||
5: {{6, 6}},
|
|
||||||
}
|
|
||||||
|
|
||||||
pq := aoc.FibHeap(less)
|
|
||||||
visited := aoc.Set([]int{}...)
|
|
||||||
dist := aoc.DefaultMap[int](int(^uint(0) >> 1))
|
|
||||||
|
|
||||||
dist.Set(0, 0)
|
|
||||||
pq.Insert(&memo{0, 0})
|
|
||||||
|
|
||||||
for !pq.IsEmpty() {
|
|
||||||
m := pq.ExtractMin()
|
|
||||||
|
|
||||||
u := m.pt
|
|
||||||
if visited.Has(u) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
visited.Add(u)
|
|
||||||
|
|
||||||
du, _ := dist.Get(u)
|
|
||||||
|
|
||||||
for _, edge := range adj[u] {
|
|
||||||
v, w := edge[0], edge[1]
|
|
||||||
dv, _ := dist.Get(v)
|
|
||||||
|
|
||||||
if !visited.Has(v) && du+w < dv {
|
|
||||||
dist.Set(v, du+w)
|
|
||||||
pq.Insert(&memo{v, du + w})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
items := dist.Items()
|
|
||||||
sort.Slice(items, func(i, j int) bool { return items[i].K < items[j].K })
|
|
||||||
for _, v := range items {
|
|
||||||
fmt.Printf("point %d is %d steps away.\n", v.K, v.V)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// point 0 is 0 steps away.
|
|
||||||
// point 1 is 2 steps away.
|
|
||||||
// point 2 is 6 steps away.
|
|
||||||
// point 3 is 7 steps away.
|
|
||||||
// point 4 is 17 steps away.
|
|
||||||
// point 5 is 22 steps away.
|
|
||||||
// point 6 is 19 steps away.
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFibHeap(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
type elem [2]int
|
|
||||||
less := func(a, b *elem) bool {
|
|
||||||
return (*a)[0] < (*b)[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
pq := aoc.FibHeap(less)
|
|
||||||
|
|
||||||
pq.Insert(&elem{1, 4})
|
|
||||||
pq.Insert(&elem{3, 2})
|
|
||||||
pq.Insert(&elem{2, 3})
|
|
||||||
pq.Insert(&elem{4, 1})
|
|
||||||
|
|
||||||
v := pq.ExtractMin()
|
|
||||||
is.True(v != nil)
|
|
||||||
is.Equal(v, &elem{1, 4})
|
|
||||||
|
|
||||||
pq.Insert(&elem{5, 8})
|
|
||||||
pq.Insert(&elem{6, 7})
|
|
||||||
pq.Insert(&elem{7, 6})
|
|
||||||
pq.Insert(&elem{8, 5})
|
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
|
||||||
is.True(v != nil)
|
|
||||||
is.Equal(v, &elem{2, 3})
|
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
|
||||||
is.True(v != nil)
|
|
||||||
is.Equal(v, &elem{3, 2})
|
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
|
||||||
is.True(v != nil)
|
|
||||||
is.Equal(v, &elem{4, 1})
|
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
|
||||||
is.True(v != nil)
|
|
||||||
is.Equal(v, &elem{5, 8})
|
|
||||||
|
|
||||||
m := aoc.FibHeap(less)
|
|
||||||
m.Insert(&elem{1, 99})
|
|
||||||
m.Insert(&elem{12, 9})
|
|
||||||
m.Insert(&elem{11, 10})
|
|
||||||
m.Insert(&elem{10, 11})
|
|
||||||
m.Insert(&elem{9, 12})
|
|
||||||
|
|
||||||
pq.Merge(m)
|
|
||||||
|
|
||||||
v = pq.Find(func(t *elem) bool {
|
|
||||||
return (*t)[0] == 6
|
|
||||||
})
|
|
||||||
is.Equal(v, &elem{6, 7})
|
|
||||||
|
|
||||||
v = pq.Find(func(t *elem) bool {
|
|
||||||
return (*t)[0] == 12
|
|
||||||
})
|
|
||||||
is.Equal(v, &elem{12, 9})
|
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
|
||||||
is.True(v != nil)
|
|
||||||
is.Equal(v, &elem{1, 99})
|
|
||||||
|
|
||||||
pq.DecreaseKey(
|
|
||||||
func(t *elem) bool { return t[0] == 12 },
|
|
||||||
func(t *elem) { t[0] = 3 },
|
|
||||||
)
|
|
||||||
|
|
||||||
v = pq.ExtractMin()
|
|
||||||
is.True(v != nil)
|
|
||||||
is.Equal(v, &elem{3, 9})
|
|
||||||
|
|
||||||
var keys []int
|
|
||||||
for !pq.IsEmpty() {
|
|
||||||
v := pq.ExtractMin()
|
|
||||||
fmt.Println(v)
|
|
||||||
keys = append(keys, v[0])
|
|
||||||
}
|
|
||||||
is.Equal(keys, []int{6, 7, 8, 9, 10, 11})
|
|
||||||
}
|
|
||||||
|
|||||||
232
day17/main.go
232
day17/main.go
@@ -8,7 +8,7 @@ import (
|
|||||||
aoc "go.sour.is/advent-of-code"
|
aoc "go.sour.is/advent-of-code"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = aoc.Log
|
// var log = aoc.Log
|
||||||
|
|
||||||
func main() { aoc.MustResult(aoc.Runner(run)) }
|
func main() { aoc.MustResult(aoc.Runner(run)) }
|
||||||
|
|
||||||
@@ -20,28 +20,22 @@ type result struct {
|
|||||||
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||||
|
|
||||||
func run(scan *bufio.Scanner) (*result, error) {
|
func run(scan *bufio.Scanner) (*result, error) {
|
||||||
var m aoc.Map[int16, rune]
|
var m aoc.Map[rune]
|
||||||
|
|
||||||
for scan.Scan() {
|
for scan.Scan() {
|
||||||
text := scan.Text()
|
text := scan.Text()
|
||||||
m = append(m, []rune(text))
|
m = append(m, []rune(text))
|
||||||
}
|
}
|
||||||
log("start day 17")
|
|
||||||
|
|
||||||
result := result{}
|
result := result{}
|
||||||
result.valuePT1 = search(m, 1, 3, seenFn)
|
result.valuePT1 = search(m, 1, 3)
|
||||||
log("result from part 1 = ", result.valuePT1)
|
result.valuePT2 = search(m, 4, 10)
|
||||||
|
|
||||||
result.valuePT2 = search(m, 4, 10, nil)
|
|
||||||
log("result from part 2 = ", result.valuePT2)
|
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Point = aoc.Point[int16]
|
func search(m aoc.Map[rune], minSteps, maxSteps int) int {
|
||||||
type Map = aoc.Map[int16, rune]
|
type direction int8
|
||||||
|
|
||||||
// rotate for changing direction
|
|
||||||
type rotate int8
|
type rotate int8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -49,177 +43,95 @@ const (
|
|||||||
CCW rotate = -1
|
CCW rotate = -1
|
||||||
)
|
)
|
||||||
|
|
||||||
// diretion of path steps
|
|
||||||
type direction int8
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
U = Point{-1, 0}
|
U = aoc.Point{-1, 0}
|
||||||
R = Point{0, 1}
|
R = aoc.Point{0, 1}
|
||||||
D = Point{1, 0}
|
D = aoc.Point{1, 0}
|
||||||
L = Point{0, -1}
|
L = aoc.Point{0, -1}
|
||||||
)
|
)
|
||||||
|
|
||||||
var directions = []Point{U, R, D, L}
|
var Direction = []aoc.Point{U, R, D, L}
|
||||||
|
|
||||||
var directionIDX = func() map[Point]direction {
|
var Directions = make(map[aoc.Point]direction, len(Direction))
|
||||||
m := make(map[Point]direction, len(directions))
|
for k, v := range Direction {
|
||||||
for k, v := range directions {
|
Directions[v] = direction(k)
|
||||||
m[v] = direction(k)
|
|
||||||
}
|
}
|
||||||
return m
|
|
||||||
}()
|
|
||||||
|
|
||||||
// position on the map
|
rows, cols := m.Size()
|
||||||
|
target := aoc.Point{rows - 1, cols - 1}
|
||||||
|
|
||||||
type position struct {
|
type position struct {
|
||||||
loc Point
|
loc aoc.Point
|
||||||
direction Point
|
direction aoc.Point
|
||||||
steps int8
|
steps int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p position) step() position {
|
step := func(p position) position {
|
||||||
return position{p.loc.Add(p.direction), p.direction, p.steps + 1}
|
return position{p.loc.Add(p.direction), p.direction, p.steps + 1}
|
||||||
}
|
}
|
||||||
func (p position) rotateAndStep(towards rotate) position {
|
rotateAndStep := func(p position, towards rotate) position {
|
||||||
d := directions[(int8(directionIDX[p.direction])+int8(towards)+4)%4]
|
d := Direction[(int8(Directions[p.direction])+int8(towards)+4)%4]
|
||||||
|
// fmt.Println(towards, Directions[p.direction], "->", Directions[d])
|
||||||
return position{p.loc.Add(d), d, 1}
|
return position{p.loc.Add(d), d, 1}
|
||||||
}
|
}
|
||||||
|
|
||||||
// implements FindPath graph interface
|
type memo struct {
|
||||||
type graph struct {
|
cost int
|
||||||
min, max int8
|
position
|
||||||
m Map
|
}
|
||||||
target Point
|
less := func(a, b memo) bool {
|
||||||
reads int
|
if a.cost != b.cost {
|
||||||
seenFn func(a position) position
|
return a.cost < b.cost
|
||||||
|
}
|
||||||
|
if a.position.loc != b.position.loc {
|
||||||
|
return b.position.loc.Less(a.position.loc)
|
||||||
|
}
|
||||||
|
if a.position.direction != b.position.direction {
|
||||||
|
return b.position.direction.Less(a.position.direction)
|
||||||
|
}
|
||||||
|
return a.steps < b.steps
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neighbors returns valid steps from given position. if at target returns none.
|
pq := aoc.PriorityQueue(less)
|
||||||
func (g *graph) Neighbors(current position) []position {
|
pq.Enqueue(memo{position: position{direction: D}})
|
||||||
var nbs []position
|
pq.Enqueue(memo{position: position{direction: R}})
|
||||||
|
visited := aoc.Set[position]()
|
||||||
|
|
||||||
if current.steps == 0 {
|
for !pq.IsEmpty() {
|
||||||
return []position{
|
current, _ := pq.Dequeue()
|
||||||
{R, R, 1},
|
|
||||||
{D, D, 1},
|
if current.loc == target && current.steps >= minSteps {
|
||||||
}
|
return current.cost
|
||||||
}
|
}
|
||||||
|
|
||||||
if current.loc == g.target {
|
seen := position{loc: current.loc, direction: current.direction, steps: current.steps}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if left := current.rotateAndStep(CCW); current.steps >= g.min && g.m.Valid(left.loc) {
|
|
||||||
nbs = append(nbs, left)
|
|
||||||
}
|
|
||||||
|
|
||||||
if right := current.rotateAndStep(CW); current.steps >= g.min && g.m.Valid(right.loc) {
|
|
||||||
nbs = append(nbs, right)
|
|
||||||
}
|
|
||||||
|
|
||||||
if forward := current.step(); current.steps < g.max && g.m.Valid(forward.loc) {
|
|
||||||
nbs = append(nbs, forward)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nbs
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cost calculates heat cost to neighbor from map
|
|
||||||
func (g *graph) Cost(a, b position) int16 {
|
|
||||||
g.reads++
|
|
||||||
_, r, _ := g.m.Get(b.loc)
|
|
||||||
return int16(r - '0')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Potential calculates distance to target
|
|
||||||
// func (g *graph) Potential(a position) int16 {
|
|
||||||
// return aoc.ManhattanDistance(a.loc, g.target)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Target returns true when target reached. receives node and cost.
|
|
||||||
func (g *graph) Target(a position, c int16) bool {
|
|
||||||
if a.loc == g.target && a.steps >= g.min && a.steps <= g.max {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Seen attempt at simplifying the seen to use horizontal/vertical and no steps.
|
|
||||||
// It returns correct for part1 but not part 2..
|
|
||||||
func (g *graph) Seen(a position) position {
|
|
||||||
if g.seenFn != nil {
|
|
||||||
return g.seenFn(a)
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
func seenFn(a position) position {
|
|
||||||
if a.direction == U {
|
|
||||||
a.direction = D
|
|
||||||
}
|
|
||||||
if a.direction == L {
|
|
||||||
a.direction = R
|
|
||||||
}
|
|
||||||
// a.steps = 0
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
func search(m Map, minSteps, maxSteps int8, seenFn func(position) position) int {
|
|
||||||
rows, cols := m.Size()
|
|
||||||
start := Point{}
|
|
||||||
target := Point{rows - 1, cols - 1}
|
|
||||||
|
|
||||||
g := graph{min: minSteps, max: maxSteps, m: m, target: target, seenFn: seenFn}
|
|
||||||
|
|
||||||
cost, path, closed := aoc.FindPath[int16, position](&g, position{loc: start}, position{loc: target})
|
|
||||||
|
|
||||||
log("total map reads = ", g.reads, "cost = ", cost)
|
|
||||||
printGraph(m, path, closed, g.seenFn)
|
|
||||||
|
|
||||||
return int(cost)
|
|
||||||
}
|
|
||||||
|
|
||||||
// printGraph with the path/cost overlay
|
|
||||||
func printGraph(m Map, path []position, closed map[position]int16, seenFn func(a position) position) {
|
|
||||||
pts := make(map[Point]position, len(path))
|
|
||||||
for _, pt := range path {
|
|
||||||
pts[pt.loc] = pt
|
|
||||||
}
|
|
||||||
|
|
||||||
clpt := make(map[position]position, len(closed))
|
|
||||||
for pt := range closed {
|
|
||||||
clpt[position{loc: pt.loc, steps: pt.steps}] = pt
|
|
||||||
}
|
|
||||||
|
|
||||||
for r, row := range m {
|
|
||||||
// if r == 0 {
|
|
||||||
// for c := range row {
|
|
||||||
// if c == 0 {
|
|
||||||
// fmt.Print(" ")
|
|
||||||
// }
|
|
||||||
// fmt.Printf("% 5d", c)
|
|
||||||
// }
|
|
||||||
// fmt.Println("")
|
|
||||||
// }
|
|
||||||
for c := range row {
|
|
||||||
// if c == 0 {
|
|
||||||
// fmt.Printf("% 5d", r)
|
|
||||||
// }
|
|
||||||
|
|
||||||
if pt, ok := pts[Point{int16(r), int16(c)}]; ok {
|
|
||||||
if seenFn != nil {
|
|
||||||
pt = seenFn(pt)
|
|
||||||
}
|
|
||||||
_ = pt
|
|
||||||
// fmt.Printf("% 5d", closed[pt])
|
|
||||||
fmt.Print("*")
|
|
||||||
|
|
||||||
|
if visited.Has(seen) {
|
||||||
|
// fmt.Println("visited", seen)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
visited.Add(seen)
|
||||||
|
|
||||||
// fmt.Print(" ....")
|
// fmt.Print("\033[2J\033[H")
|
||||||
fmt.Print(" ")
|
// fmt.Println("step ", current.steps, " dir ", Directions[current.direction], " steps ", " score ", current.cost, current.loc)
|
||||||
|
|
||||||
|
if left := rotateAndStep(current.position, CCW); current.steps >= minSteps && m.Valid(left.loc) {
|
||||||
|
_, cost, _ := m.Get(left.loc)
|
||||||
|
// fmt.Println("turn left", current, left)
|
||||||
|
pq.Enqueue(memo{cost: current.cost + int(cost-'0'), position: left})
|
||||||
}
|
}
|
||||||
fmt.Println("")
|
|
||||||
|
if right := rotateAndStep(current.position, CW); current.steps >= minSteps && m.Valid(right.loc) {
|
||||||
|
_, cost, _ := m.Get(right.loc)
|
||||||
|
// fmt.Println("turn right", current, right)
|
||||||
|
pq.Enqueue(memo{cost: current.cost + int(cost-'0'), position: right})
|
||||||
}
|
}
|
||||||
fmt.Println("")
|
|
||||||
|
if forward := step(current.position); current.steps < maxSteps && m.Valid(forward.loc) {
|
||||||
|
_, cost, _ := m.Get(forward.loc)
|
||||||
|
// fmt.Println("go forward", current, forward)
|
||||||
|
pq.Enqueue(memo{cost: current.cost + int(cost-'0'), position: forward})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ func TestExample(t *testing.T) {
|
|||||||
is.Equal(result.valuePT2, 94)
|
is.Equal(result.valuePT2, 94)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSolution(t *testing.T) {
|
// func TestSolution(t *testing.T) {
|
||||||
is := is.New(t)
|
// is := is.New(t)
|
||||||
scan := bufio.NewScanner(bytes.NewReader(input))
|
// scan := bufio.NewScanner(bytes.NewReader(input))
|
||||||
|
|
||||||
result, err := run(scan)
|
// result, err := run(scan)
|
||||||
is.NoErr(err)
|
// is.NoErr(err)
|
||||||
|
|
||||||
t.Log(result)
|
// t.Log(result)
|
||||||
is.Equal(result.valuePT1, 843)
|
// is.Equal(result.valuePT1, 843)
|
||||||
is.Equal(result.valuePT2, 1017)
|
// is.Equal(result.valuePT2, 1017)
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func run(scan *bufio.Scanner) (*result, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var OFFSET = map[string]aoc.Point[int]{
|
var OFFSET = map[string]aoc.Point{
|
||||||
"R": {0, 1},
|
"R": {0, 1},
|
||||||
"D": {1, 0},
|
"D": {1, 0},
|
||||||
"L": {0, -1},
|
"L": {0, -1},
|
||||||
@@ -77,7 +77,7 @@ func fromColor(c string) aoc.Vector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func findArea(vecs []aoc.Vector) int {
|
func findArea(vecs []aoc.Vector) int {
|
||||||
shoelace := []aoc.Point[int]{{0, 0}}
|
shoelace := []aoc.Point{{0, 0}}
|
||||||
borderLength := 0
|
borderLength := 0
|
||||||
|
|
||||||
for _, vec := range vecs {
|
for _, vec := range vecs {
|
||||||
|
|||||||
216
day19/main.go
216
day19/main.go
@@ -15,7 +15,7 @@ func main() { aoc.MustResult(aoc.Runner(run)) }
|
|||||||
|
|
||||||
type result struct {
|
type result struct {
|
||||||
valuePT1 int
|
valuePT1 int
|
||||||
valuePT2 uint
|
valuePT2 int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||||
@@ -31,36 +31,9 @@ func run(scan *bufio.Scanner) (*result, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is Part
|
|
||||||
if p, ok := scanPart(text); ok {
|
|
||||||
parts = append(parts, p)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if name, r, ok := scanRule(text); ok {
|
|
||||||
workflows[name] = r
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var result result
|
|
||||||
result.valuePT1 = solveWorkflow(parts, workflows)
|
|
||||||
result.valuePT2 = solveRanges(workflows)
|
|
||||||
|
|
||||||
return &result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type part struct {
|
|
||||||
x, m, a, s int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p part) String() string {
|
|
||||||
return fmt.Sprintf("{x:%v m:%v a:%v s:%v}", p.x, p.m, p.a, p.s)
|
|
||||||
}
|
|
||||||
func scanPart(text string) (part, bool) {
|
|
||||||
var p part
|
|
||||||
|
|
||||||
// Is Part
|
// Is Part
|
||||||
if text[0] == '{' {
|
if text[0] == '{' {
|
||||||
|
var p part
|
||||||
for _, s := range strings.Split(text[1:], ",") {
|
for _, s := range strings.Split(text[1:], ",") {
|
||||||
a, b, _ := strings.Cut(s, "=")
|
a, b, _ := strings.Cut(s, "=")
|
||||||
i := aoc.Atoi(b)
|
i := aoc.Atoi(b)
|
||||||
@@ -75,19 +48,10 @@ func scanPart(text string) (part, bool) {
|
|||||||
p.s = i
|
p.s = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return p, true
|
parts = append(parts, p)
|
||||||
}
|
continue
|
||||||
return p, false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type rule struct {
|
|
||||||
match string
|
|
||||||
op string
|
|
||||||
value int
|
|
||||||
queue string
|
|
||||||
}
|
|
||||||
|
|
||||||
func scanRule(text string) (string, []rule, bool) {
|
|
||||||
name, text, _ := strings.Cut(text, "{")
|
name, text, _ := strings.Cut(text, "{")
|
||||||
var r []rule
|
var r []rule
|
||||||
for _, s := range strings.Split(text, ",") {
|
for _, s := range strings.Split(text, ",") {
|
||||||
@@ -117,7 +81,66 @@ func scanRule(text string) (string, []rule, bool) {
|
|||||||
r = append(r, rule{queue: s})
|
r = append(r, rule{queue: s})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return name, r, len(r) > 0
|
workflows[name] = r
|
||||||
|
}
|
||||||
|
|
||||||
|
var rejected []part
|
||||||
|
var accepted []part
|
||||||
|
|
||||||
|
for _, p := range parts {
|
||||||
|
workflow := "in"
|
||||||
|
|
||||||
|
nextStep:
|
||||||
|
for workflow != "" {
|
||||||
|
for _, r := range workflows[workflow] {
|
||||||
|
if !r.Match(p) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
workflow = r.queue
|
||||||
|
|
||||||
|
if workflow == "A" {
|
||||||
|
accepted = append(accepted, p)
|
||||||
|
workflow = ""
|
||||||
|
break nextStep
|
||||||
|
}
|
||||||
|
if workflow == "R" {
|
||||||
|
rejected = append(rejected, p)
|
||||||
|
workflow = ""
|
||||||
|
break nextStep
|
||||||
|
}
|
||||||
|
|
||||||
|
continue nextStep
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("accepted", accepted)
|
||||||
|
fmt.Println("rejected", rejected)
|
||||||
|
|
||||||
|
var result result
|
||||||
|
|
||||||
|
for _, p := range accepted {
|
||||||
|
result.valuePT1 += p.x
|
||||||
|
result.valuePT1 += p.m
|
||||||
|
result.valuePT1 += p.a
|
||||||
|
result.valuePT1 += p.s
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type part struct {
|
||||||
|
x, m, a, s int
|
||||||
|
}
|
||||||
|
func (p part) String() string {
|
||||||
|
return fmt.Sprintf("{x:%v m:%v a:%v s:%v}", p.x,p.m,p.a,p.s)
|
||||||
|
}
|
||||||
|
type rule struct {
|
||||||
|
match string
|
||||||
|
op string
|
||||||
|
value int
|
||||||
|
queue string
|
||||||
}
|
}
|
||||||
func (r rule) Match(p part) bool {
|
func (r rule) Match(p part) bool {
|
||||||
var value int
|
var value int
|
||||||
@@ -143,113 +166,12 @@ func (r rule) Match(p part) bool {
|
|||||||
return false // no match
|
return false // no match
|
||||||
}
|
}
|
||||||
|
|
||||||
func solveWorkflow(parts []part, workflows map[string][]rule) int {
|
|
||||||
// var rejected []part
|
|
||||||
var accepted []part
|
|
||||||
|
|
||||||
for _, p := range parts {
|
func in(n string, haystack ...string) bool {
|
||||||
workflow := "in"
|
for _, h := range haystack {
|
||||||
|
if n == h {
|
||||||
nextStep:
|
return true
|
||||||
for workflow != "" {
|
|
||||||
for _, r := range workflows[workflow] {
|
|
||||||
if !r.Match(p) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
workflow = r.queue
|
|
||||||
|
|
||||||
if workflow == "A" {
|
|
||||||
accepted = append(accepted, p)
|
|
||||||
workflow = ""
|
|
||||||
break nextStep
|
|
||||||
}
|
|
||||||
if workflow == "R" {
|
|
||||||
// rejected = append(rejected, p)
|
|
||||||
workflow = ""
|
|
||||||
break nextStep
|
|
||||||
}
|
|
||||||
|
|
||||||
continue nextStep
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return false
|
||||||
|
|
||||||
sum := 0
|
|
||||||
for _, p := range accepted {
|
|
||||||
sum += p.x
|
|
||||||
sum += p.m
|
|
||||||
sum += p.a
|
|
||||||
sum += p.s
|
|
||||||
}
|
|
||||||
return sum
|
|
||||||
}
|
|
||||||
|
|
||||||
func solveRanges(workflows map[string][]rule) uint {
|
|
||||||
|
|
||||||
pq := aoc.PriorityQueue(func(a, b *queue) bool { return false })
|
|
||||||
pq.Insert(&queue{
|
|
||||||
"in",
|
|
||||||
block{
|
|
||||||
ranger{1, 4000},
|
|
||||||
ranger{1, 4000},
|
|
||||||
ranger{1, 4000},
|
|
||||||
ranger{1, 4000},
|
|
||||||
}})
|
|
||||||
|
|
||||||
var accepted []block
|
|
||||||
// var rejected []block
|
|
||||||
|
|
||||||
for !pq.IsEmpty() {
|
|
||||||
current := pq.ExtractMin()
|
|
||||||
for _, rule := range workflows[current.name] {
|
|
||||||
next := &queue{name: rule.queue, block: current.block}
|
|
||||||
|
|
||||||
switch rule.match {
|
|
||||||
case "x":
|
|
||||||
current.x, next.x = split(current.x, rule.value, rule.op == ">")
|
|
||||||
case "m":
|
|
||||||
current.m, next.m = split(current.m, rule.value, rule.op == ">")
|
|
||||||
case "a":
|
|
||||||
current.a, next.a = split(current.a, rule.value, rule.op == ">")
|
|
||||||
case "s":
|
|
||||||
current.s, next.s = split(current.s, rule.value, rule.op == ">")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch next.name {
|
|
||||||
case "R":
|
|
||||||
// rejected = append(rejected, next.block)
|
|
||||||
|
|
||||||
case "A":
|
|
||||||
accepted = append(accepted, next.block)
|
|
||||||
|
|
||||||
default:
|
|
||||||
pq.Insert(next)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var sum uint
|
|
||||||
for _, a := range accepted {
|
|
||||||
sum += uint((a.x[1] - a.x[0] + 1) * (a.m[1] - a.m[0] + 1) * (a.a[1] - a.a[0] + 1) * (a.s[1] - a.s[0] + 1))
|
|
||||||
}
|
|
||||||
|
|
||||||
return sum
|
|
||||||
}
|
|
||||||
|
|
||||||
type ranger [2]int
|
|
||||||
type block struct {
|
|
||||||
x, m, a, s ranger
|
|
||||||
}
|
|
||||||
type queue struct {
|
|
||||||
name string
|
|
||||||
block
|
|
||||||
}
|
|
||||||
|
|
||||||
func split(a ranger, n int, gt bool) (current ranger, next ranger) {
|
|
||||||
if gt { // x > N => [0,N] [N++,inf]
|
|
||||||
return ranger{a[0], n}, ranger{n + 1, a[1]}
|
|
||||||
}
|
|
||||||
|
|
||||||
// x < N => [N,inf] [0,N--]
|
|
||||||
return ranger{n, a[1]}, ranger{a[0], n - 1}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func TestExample(t *testing.T) {
|
|||||||
|
|
||||||
t.Log(result)
|
t.Log(result)
|
||||||
is.Equal(result.valuePT1, 19114)
|
is.Equal(result.valuePT1, 19114)
|
||||||
is.Equal(result.valuePT2, uint(167409079868000))
|
is.Equal(result.valuePT2, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSolution(t *testing.T) {
|
func TestSolution(t *testing.T) {
|
||||||
@@ -37,5 +37,5 @@ func TestSolution(t *testing.T) {
|
|||||||
|
|
||||||
t.Log(result)
|
t.Log(result)
|
||||||
is.Equal(result.valuePT1, 377025)
|
is.Equal(result.valuePT1, 377025)
|
||||||
is.Equal(result.valuePT2, uint(135506683246673))
|
is.Equal(result.valuePT2, 0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
#.#####################
|
|
||||||
#.......#########...###
|
|
||||||
#######.#########.#.###
|
|
||||||
###.....#.>.>.###.#.###
|
|
||||||
###v#####.#v#.###.#.###
|
|
||||||
###.>...#.#.#.....#...#
|
|
||||||
###v###.#.#.#########.#
|
|
||||||
###...#.#.#.......#...#
|
|
||||||
#####.#.#.#######.#.###
|
|
||||||
#.....#.#.#.......#...#
|
|
||||||
#.#####.#.#.#########v#
|
|
||||||
#.#...#...#...###...>.#
|
|
||||||
#.#.#v#######v###.###v#
|
|
||||||
#...#.>.#...>.>.#.###.#
|
|
||||||
#####v#.#.###v#.#.###.#
|
|
||||||
#.....#...#...#.#.#...#
|
|
||||||
#.#########.###.#.#.###
|
|
||||||
#...###...#...#...#.###
|
|
||||||
###.###.#.###v#####v###
|
|
||||||
#...#...#.#.>.>.#.>.###
|
|
||||||
#.###.###.#.###.#.#v###
|
|
||||||
#.....###...###...#...#
|
|
||||||
#####################.#
|
|
||||||
141
day23/input.txt
141
day23/input.txt
@@ -1,141 +0,0 @@
|
|||||||
#.###########################################################################################################################################
|
|
||||||
#.#...#.......#...#.....#...#...#######...###...#...#.......#...#...#...#...#...#...#...###...###...###...............#.....#...#######...###
|
|
||||||
#.#.#.#.#####.#.#.#.###.#.#.#.#.#######.#.###.#.#.#.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.###.#############.#.###.#.#.#######.#.###
|
|
||||||
#.#.#.#.#.....#.#.#...#.#.#...#...#.....#...#.#.#.#.#.....#...#.#.#.#.#.#.#.#.#...#...#.....#.#...#...#.............#.#...#...#.......#.#...#
|
|
||||||
#.#.#.#.#.#####.#.###.#.#.#######.#.#######.#.#.#.#.#####.#####.#.#.#.#.#.#.#.###############.#.#####.#############.#.###.###########.#.###.#
|
|
||||||
#...#...#.....#.#.###.#.#.#.......#.....#...#.#.#.#.#.>.>.#.....#.#.#.#.#.#.#...#.............#.#.....#...#.......#.#.....#...........#.#...#
|
|
||||||
#############.#.#v###.#.#.#.###########.#.###.#.#.#.#.#v###.#####.#.#.#.#.#.###.#.#############.#.#####.#.#.#####.#.#######.###########.#.###
|
|
||||||
#...###...###...#.>.#.#.#.#.#...###.....#.....#.#.#...#...#.#...#.#.#.#.#.#.###.#.#####...###...#...#...#.#...#...#.....#...#...###...#.#.###
|
|
||||||
#.#.###.#.#######v#.#.#.#.#.#.#.###.###########.#.#######.#.#.#.#.#.#.#.#.#.###.#.#####.#.###.#####.#.###.###.#.#######.#.###.#.###.#.#.#.###
|
|
||||||
#.#.#...#.#.....#.#.#.#.#.#...#...#.........#...#.###.....#.#.#.#.#.#.#.#.#.>.>.#.......#.#...#.....#...#.#...#.#.......#...#.#.....#.#.#.###
|
|
||||||
#.#.#.###.#.###.#.#.#.#.#.#######.#########.#.###.###.#####.#.#.#.#.#.#.#.###v###########.#.###.#######.#.#.###.#.#########.#.#######.#.#.###
|
|
||||||
#.#.#...#.#...#.#.#.#.#.#...#.....#...#...#.#...#...#.....#.#.#.#.#.#.#...###.#...........#...#.....#...#.#...#.#.........#.#.#.......#.#.###
|
|
||||||
#.#.###.#.###.#.#.#.#.#.###.#.#####.#.#.#.#.###.###.#####.#.#.#.#.#.#.#######.#.#############.#####.#.###.###.#.#########.#.#.#.#######.#.###
|
|
||||||
#.#.....#.#...#.#.#.#.#.#...#.....#.#...#.#...#.#...#.....#...#...#.#.#.......#.#.....#.....#.#.....#...#.###.#.#...#.....#...#.#...###.#.###
|
|
||||||
#.#######.#.###.#.#.#.#.#.#######.#.#####.###.#.#.###.#############.#.#.#######.#.###.#.###.#.#.#######.#.###.#.#.#.#.#########.#.#.###.#.###
|
|
||||||
#.#...###...###.#.#...#.#...#.....#.#...#...#.#.#...#...........###...#.......#.#.#...#...#.#.#.#...###.#.###.#.#.#.#.....#.....#.#.#...#...#
|
|
||||||
#.#.#.#########.#.#####.###.#.#####.#.#.###.#.#.###.###########.#############.#.#.#.#####.#.#.#.#.#.###.#.###.#.#.#.#####.#.#####.#.#.#####.#
|
|
||||||
#.#.#.........#...#.....#...#.#...#...#...#...#.....#####.......#...###.......#.#.#.#.....#.#.#.#.#.#...#.>.>.#.#.#.#.....#...#...#...###...#
|
|
||||||
#.#.#########.#####.#####.###.#.#.#######.###############.#######.#.###.#######.#.#.#.#####.#.#.#.#.#.#####v###.#.#.#.#######.#.#########.###
|
|
||||||
#...#.......#.#...#...#...###.#.#.>.>...#...#...#...###...###...#.#...#.......#.#.#...###...#.#.#.#.#...#...###...#...###.....#.###.......###
|
|
||||||
#####.#####.#.#.#.###.#.#####.#.###v###.###.#.#.#.#.###.#####.#.#.###.#######.#.#.#######.###.#.#.#.###.#.###############.#####.###.#########
|
|
||||||
#...#.....#...#.#.###...#####...#...###.....#.#.#.#.#...#...#.#...#...#.......#.#.#.....#.....#.#.#.....#...#...........#.#...#...#...#.....#
|
|
||||||
#.#.#####.#####.#.###############.###########.#.#.#.#.###.#.#.#####.###.#######.#.#.###.#######.#.#########.#.#########.#.#.#.###.###.#.###.#
|
|
||||||
#.#.......#...#.#.#...#...###...#.....#.......#.#.#...#...#...#.....###.......#...#...#.#.....#.#...#...###...#.........#...#.#...###...#...#
|
|
||||||
#.#########.#.#.#.#.#.#.#.###.#.#####.#.#######.#.#####.#######.#############.#######.#.#.###.#.###.#.#.#######.#############.#.#########.###
|
|
||||||
#.........#.#.#.#.#.#.#.#...#.#.......#.#.......#.......###.....#...#.........###...#.#.#.#...#.....#.#.#...###.............#...#...#...#...#
|
|
||||||
#########.#.#.#.#.#.#.#.###.#.#########.#.#################.#####.#.#.###########.#.#.#.#.#.#########.#.#.#.###############.#####.#.#.#.###.#
|
|
||||||
#.........#.#.#.#.#.#.#...#.#.......###.#.#...#...#...#...#.....#.#.#.......#...#.#...#.#.#...#.......#.#.#...#.............#...#.#...#.....#
|
|
||||||
#.#########.#.#.#.#.#.###.#.#######v###.#.#.#.#.#.#.#.#.#.#####v#.#.#######.#.#.#.#####.#.###.#.#######.#.###.#.#############.#.#.###########
|
|
||||||
#...........#.#.#...#.#...#...#...>.>.#.#...#.#.#...#.#.#...#.>.>.#.#...###.#.#.#.....#.#.#...#.......#.#...#.#...#...#...###.#.#...........#
|
|
||||||
#############.#.#####.#.#####.#.###v#.#.#####.#.#####.#.###.#.#v###.#.#.###v#.#.#####.#.#.#.#########.#.###.#.###v#.#.#.#.###.#.###########.#
|
|
||||||
#...#...#.....#.....#.#.#.....#.#...#.#.....#.#...###...#...#.#...#.#.#.#.>.>.#.#...#.#.#.#...#.......#...#.#.#.>.>.#...#...#.#...#.........#
|
|
||||||
#.#.#.#.#.#########.#.#.#.#####.#.###.#####.#.###.#######.###.###.#.#.#.#.#v###.#.#.#.#.#.###.#.#########.#.#.#.#v#########.#.###.#.#########
|
|
||||||
#.#...#.#.#####...#.#.#.#.#.....#...#...#...#.#...#.....#...#...#.#...#...#.###...#...#...#...#.......###...#...#.#...#...#.#.#...#...#...###
|
|
||||||
#.#####.#.#####.#.#.#.#.#.#.#######.###.#.###.#.###.###.###.###.#.#########.###############.#########.###########.#.#.#.#.#.#.#.#####.#.#.###
|
|
||||||
#.....#...###...#.#.#...#...#.....#...#.#.#...#.....#...###.....#.....#.....#...#...#...###...#.......#...#...###.#.#...#...#.#...###...#...#
|
|
||||||
#####.#######.###.#.#########.###.###.#.#.#.#########.###############.#.#####.#.#.#.#.#.#####.#.#######.#.#.#.###.#.#########.###.#########.#
|
|
||||||
#.....#...#...###...#...#.....###...#.#.#.#.#...#...#...#.............#...#...#...#...#.....#.#.....#...#...#...#.#.###...###...#.#...#.....#
|
|
||||||
#.#####.#.#.#########.#.#.#########.#.#.#.#.#.#.#.#.###.#.###############.#.###############.#.#####.#.#########.#.#.###.#.#####.#.#.#.#.#####
|
|
||||||
#.....#.#...#####.....#.#.........#...#.#.#.#.#...#.#...#...............#...#...............#.#.....#.#.....#...#.#.#...#.#...#.#.#.#.#...###
|
|
||||||
#####.#.#########.#####.#########.#####.#.#.#.#####.#.#################.#####.###############.#.#####.#.###.#.###.#.#.###.#.#.#.#.#.#.###v###
|
|
||||||
#...#.#.........#.#...#...........#...#...#.#.....#.#.#.................#.....#.....#.......#.#.....#...#...#.....#.#.###.#.#.#.#.#.#.#.>.###
|
|
||||||
#.#.#v#########.#.#.#.#############.#.#####.#####.#.#.#.#################.#####.###.#.#####.#.#####.#####.#########.#.###.#.#.#.#.#.#.#.#v###
|
|
||||||
#.#.#.>...#.....#...#...............#.....#.#...#.#...#.#...#...........#.#.....###...#.....#.......#...#.........#.#...#.#.#...#.#.#...#...#
|
|
||||||
#.#.#v###.#.#############################.#.#.#.#.#####.#.#.#.#########.#.#.###########.#############.#.#########.#.###.#.#.#####.#.#######.#
|
|
||||||
#.#.#...#.#.......#.......................#...#...#...#...#...#...#.....#.#.#...........#...#...###...#...........#.#...#.#.#.....#.#.......#
|
|
||||||
#.#.###.#.#######.#.###############################.#.#########.#.#.#####.#.#.###########.#.#.#.###.###############.#.###.#.#.#####.#.#######
|
|
||||||
#.#.....#.........#...#...#.........###...#...#...#.#.#.........#...#...#...#.#...........#.#.#...#.........#.....#...###.#.#.#.....#.#.....#
|
|
||||||
#.###################.#.#.#.#######.###.#.#.#.#.#.#.#.#.#############.#.#####.#.###########.#.###.#########.#.###.#######.#.#.#.#####.#.###.#
|
|
||||||
#...#...###.....#...#...#.#.#.......#...#...#.#.#.#.#.#.............#.#.....#...#...........#.#...#.........#...#.....###...#...#...#...#...#
|
|
||||||
###.#.#.###.###.#.#.#####.#.#.#######.#######.#.#.#.#.#############.#.#####.#####.###########.#.###.###########.#####.###########.#.#####.###
|
|
||||||
#...#.#...#...#.#.#.#.....#.#.....###.#.......#.#...#...#...#...###...#.....#...#.......#...#.#.###.......###...#.....#...#.....#.#...###...#
|
|
||||||
#.###.###.###.#.#.#.#.#####.#####.###.#.#######.#######.#.#.#.#.#######.#####.#.#######.#.#.#.#.#########v###.###.#####.#.#.###.#.###.#####.#
|
|
||||||
#.....###.....#.#.#.#.......#...#...#.#.....###.......#.#.#...#.#.......#####.#.........#.#.#.#...#.....>.>.#...#.#...#.#.#.#...#...#.###...#
|
|
||||||
###############.#.#.#########.#.###.#.#####.#########.#.#.#####.#.###########.###########.#.#.###.#.#####v#.###.#.#.#.#.#.#.#.#####.#.###v###
|
|
||||||
#...............#.#.#.......#.#.....#.#.....#...#...#.#.#.....#.#...........#...#...#...#.#.#.#...#.....#.#.....#.#.#.#.#.#.#...#...#...>.###
|
|
||||||
#.###############.#.#.#####.#.#######.#.#####.#.#.#.#.#.#####.#.###########.###.#.#.#.#.#.#.#.#.#######.#.#######.#.#.#.#.#.###.#.#######v###
|
|
||||||
#...#.....#...###.#.#.....#.#.#...###.#.#...#.#.#.#.#.#.#...#.#.###.........###...#...#.#.#...#...#.....#.....#...#.#...#.#...#.#.#.....#.###
|
|
||||||
###.#.###.#.#.###.#.#####.#.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.###v###################.#.#######.#.#########.#.###.#####.###.#.#.#.###.#.###
|
|
||||||
###...###.#.#.#...#.#.....#...#.#.#...#.#.#.#.#.#.#.#.#.#.#...#...>.>.........#...#.....#.....#...#...#.......#.....#.....#...#...#...#...###
|
|
||||||
#########v#.#.#.###.#.#########.#.#.###.#.#.#.#.#.#.#.#.#.#########v#########.#.#.#.#########.#.#####.#.#############.#####.#########.#######
|
|
||||||
#...#...#.>.#.#...#.#.........#.#.#.#...#.#.#.#.#.#.#.#.#.#.........###.......#.#.#.....#.....#.....#.#...#.........#...#...#.........#...###
|
|
||||||
#.#.#.#.#v###.###.#.#########.#.#.#.#.###.#.#.#.#.#.#.#.#.#.###########.#######.#.#####.#.#########.#.###.#.#######.###.#.###.#########.#.###
|
|
||||||
#.#...#...###.....#.#...#.....#.#...#...#.#.#.#.#.#...#...#...#.....#...#.....#.#.#.....#.......###.#.###...#...#...#...#...#.#...#.....#...#
|
|
||||||
#.#################.#.#.#.#####.#######.#.#.#.#.#.###########.#.###.#.###.###.#.#.#.###########.###.#.#######.#.#.###.#####.#.#.#.#.#######.#
|
|
||||||
#.............#.....#.#.#.....#.....###...#.#.#.#.###...#...#...#...#...#.#...#.#.#.#.....#...#...#.#.#.......#...###...#...#...#...#.......#
|
|
||||||
#############.#.#####.#.#####v#####.#######.#.#.#.###.#.#.#.#####.#####.#.#.###.#.#v#.###.#.#.###.#.#.#.###############.#.###########.#######
|
|
||||||
#.............#.#...#.#...#.>.>.....#.....#.#.#.#.#...#...#.......#####.#.#.....#.>.>.###.#.#.....#.#.#.......#.....###.#...#.......#.......#
|
|
||||||
#.#############.#.#.#.###.#.#v#######.###.#.#.#.#.#.###################.#.#########v#####.#.#######.#.#######.#.###.###.###.#.#####.#######.#
|
|
||||||
#.........#...#...#.#.###.#.#.........#...#...#...#.......#.........#...#...#.......#...#...#...###.#.#.....#...#...#...#...#.....#.........#
|
|
||||||
#########.#.#.#####.#.###.#.###########.#################.#.#######.#.#####.#.#######.#.#####.#.###.#.#.###.#####.###.###.#######.###########
|
|
||||||
#.........#.#.#...#...###...#.........#.#...#...#.......#...#...#...#.#.....#.......#.#.......#...#...#...#.#...#...#.....#...###...........#
|
|
||||||
#.#########.#.#.#.###########.#######.#.#.#.#.#.#.#####.#####.#.#.###.#.###########.#.###########.#######.#.#.#.###.#######.#.#############.#
|
|
||||||
#...#...#...#.#.#.#...#.......#...###...#.#.#.#.#.....#...#...#...###...#...###...#.#.#...........#...#...#...#.....#...###.#.#...#.......#.#
|
|
||||||
###.#.#.#.###.#.#v#.#.#.#######.#.#######.#.#.#.#####.###.#.#############.#.###.#.#.#.#.###########.#.#.#############.#.###.#.#.#.#.#####.#.#
|
|
||||||
#...#.#.#.#...#.#.>.#.#.....#...#...#...#.#.#.#.#...#.#...#.......#...#...#.#...#.#...#.....#.....#.#...#.....###...#.#.#...#.#.#.#...###...#
|
|
||||||
#.###.#.#.#.###.#v###.#####.#.#####.#.#.#.#.#.#.#.#.#.#.#########.#.#.#.###.#.###.#########.#.###.#.#####.###.###.#.#.#.#.###.#.#.###v#######
|
|
||||||
#.....#.#.#...#.#.#...#...#...###...#.#.#.#.#.#.#.#...#.#.......#.#.#.#...#.#...#.###...#...#.#...#...#...###...#.#...#.#...#.#.#...>.###...#
|
|
||||||
#######.#.###.#.#.#.###.#.#######.###.#.#.#.#.#.#.#####.#.#####.#.#.#.###.#.###.#.###.#.#v###.#.#####.#.#######.#.#####.###.#.#.#####v###.#.#
|
|
||||||
#...###...###...#.#...#.#...#...#.###.#.#.#.#.#.#.....#.#.#.....#.#.#.#...#...#.#.#...#.>.>.#.#.....#...#.......#.#.....#...#.#...#...###.#.#
|
|
||||||
#.#.#############.###.#.###.#.#.#v###.#.#.#.#.#.#####.#.#.#.#####v#.#.#.#####.#.#.#.#####v#.#.#####.#####.#######.#.#####.###.###.#.#####.#.#
|
|
||||||
#.#.#...#.....#...###.#.#...#.#.>.>.#.#.#.#.#.#...#...#.#.#.#...>.>.#.#.....#.#.#.#.#.....#...#.....#...#.....#...#.....#...#.#...#.......#.#
|
|
||||||
#.#.#.#.#.###.#.#####.#.#.###.###v#.#.#.#.#.#.###.#.###.#.#.#.###v###.#####.#.#.#.#.#.#########.#####.#.#####.#.#######.###.#.#.###########.#
|
|
||||||
#.#.#.#.#...#.#...#...#.#...#.#...#...#.#.#.#.###...#...#.#.#.#...###...#...#.#.#.#.#.......#...###...#...###.#.......#.#...#.#.#.....#...#.#
|
|
||||||
#.#.#.#.###.#.###.#.###.###.#.#.#######.#.#.#.#######.###.#.#.#.#######.#.###.#.#.#.#######.#.#####.#####.###.#######.#.#.###.#.#.###.#.#.#.#
|
|
||||||
#.#...#.#...#.#...#...#...#...#.......#.#.#.#...#.....#...#...#.......#.#...#...#...#...#...#.......#.....#...#...#...#.#.###...#...#...#...#
|
|
||||||
#.#####.#.###.#.#####.###.###########.#.#.#.###.#.#####.#############.#.###.#########.#.#.###########.#####.###.#.#.###.#.#########.#########
|
|
||||||
#.#...#...###...#.....#...#...........#...#.....#...#...#...#...#...#.#.#...#...#...#.#.#...#.........#...#...#.#.#...#...#####...#.........#
|
|
||||||
#.#.#.###########.#####.###.#######################.#.###.#.#.#.#.#.#.#.#.###.#.#.#.#.#.###.#.#########.#.###v#.#.###.#########.#.#########.#
|
|
||||||
#...#.........###.....#...#.......###...#.........#...#...#...#...#.#.#...#...#...#...#.....#.....#...#.#.#.>.>.#...#.#.........#...........#
|
|
||||||
#############.#######.###.#######.###.#.#.#######.#####.###########.#.#####.#####################.#.#.#.#.#.#v#####.#.#.#####################
|
|
||||||
#.............#...###.....#.....#.....#...#...#...#.....#.......#...#.#...#.................#.....#.#...#...#...###...#.#...#...#.......#...#
|
|
||||||
#.#############.#.#########.###.###########.#.#.###.#####.#####.#.###.#.#.#################.#.#####.###########.#######.#.#.#.#.#.#####.#.#.#
|
|
||||||
#.#.....#...###.#.....#...#...#...........#.#...###.......#...#.#...#.#.#.#.........#...#...#...#...#.......#...#...###...#...#...#...#...#.#
|
|
||||||
#.#.###.#.#.###.#####.#.#.###.###########.#.###############.#.#.###.#.#.#.#.#######.#.#.#.#####.#.###.#####.#.###.#.###############.#.#####.#
|
|
||||||
#...###...#...#...#...#.#.###.........#...#.......#.........#.#.#...#.#.#.#.......#.#.#.#.#...#...###.....#.#.....#...#.............#.....#.#
|
|
||||||
#############.###.#.###.#.###########.#.#########.#.#########.#.#.###.#.#.#######.#.#.#.#.#.#.###########.#.#########.#.#################.#.#
|
|
||||||
#...###.......#...#...#.#.#...........#.....#.....#.........#...#.....#.#.........#.#.#.#.#.#.............#...#.......#.................#.#.#
|
|
||||||
#.#.###v#######.#####.#.#.#.###############.#.#############.###########.###########.#.#.#.#.#################.#.#######################.#.#.#
|
|
||||||
#.#...#.>.#.....#.....#.#.#...............#.#...#...#######.....#...###...........#.#.#.#.#.#.......#...#####...###.....#...#...###.....#.#.#
|
|
||||||
#.###.#v#.#.#####.#####.#.###############.#.###.#.#.###########.#.#.#############.#.#.#.#.#.#.#####.#.#.###########.###.#.#.#.#.###.#####.#.#
|
|
||||||
#...#...#...#.....#...#.#...#.............#.#...#.#.###.........#.#.#...###.......#...#...#...###...#.#...#...#...#...#.#.#.#.#...#.....#...#
|
|
||||||
###.#########.#####.#.#.###.#.#############.#.###.#.###.#########.#.#.#.###.#####################.###.###.#.#.#.#.###.#.#.#.#.###.#####.#####
|
|
||||||
#...#.......#.....#.#.#.#...#.....#...#...#...###.#...#.......#...#...#...#.........#...#...#...#.....#...#.#.#.#.###.#.#.#.#.#...#.....#...#
|
|
||||||
#.###.#####.#####.#.#.#.#.#######.#.#.#.#.#######.###.#######.#.#########.#########.#.#.#.#.#.#.#######.###.#.#.#.###.#.#.#.#.#.###v#####.#.#
|
|
||||||
#.....#.....#...#...#...#...#.....#.#...#.#.......#...#.......#...#.......#.........#.#.#.#.#.#.#...###.###.#.#.#.#...#.#.#.#.#.#.>.###...#.#
|
|
||||||
#######.#####.#.###########.#.#####.#####.#.#######.###.#########.#.#######.#########.#.#.#.#.#.#.#.###v###.#.#.#.#.###.#.#.#.#.#.#v###.###.#
|
|
||||||
#.......#.....#.#...#.......#.......#.....#.......#.###.....#...#.#...#...#.....###...#.#.#.#.#.#.#.#.>.>...#.#.#...###.#.#.#.#...#...#.#...#
|
|
||||||
#.#######.#####.#.#.#.###############.###########.#.#######.#.#.#.###.#.#.#####.###.###.#.#.#.#.#.#.#.#v#####.#.#######.#.#.#.#######.#.#.###
|
|
||||||
#.......#.#.....#.#.#.....#.....#.....#...#...#...#...#...#.#.#.#.#...#.#.#...#.#...###.#.#...#.#.#...#.....#.#.......#.#.#.#...#.....#.#...#
|
|
||||||
#######.#.#.#####.#.#####.#.###.#.#####.#.#.#.#.#####.#.#.#.#.#.#.#.###.#.#.#.#v#.#####.#.#####.#.#########.#.#######.#.#.#.###.#.#####.###.#
|
|
||||||
#.......#.#...#...#...#...#...#.#.#...#.#.#.#.#...#...#.#.#.#.#.#.#...#.#.#.#.>.>.###...#.....#.#.###.......#.#.......#.#.#.#...#.......#...#
|
|
||||||
#.#######.###.#.#####.#.#####.#.#v#.#.#.#.#.#.###.#.###.#.#v#.#.#.###.#.#.#.###v#####.#######.#.#.###.#######.#.#######.#.#.#.###########.###
|
|
||||||
#...#...#...#.#.#.....#.#...#.#.>.>.#.#.#...#.#...#...#.#.>.>.#.#.#...#.#...###.....#...#...#.#.#.#...#.....#.#.....###...#.#...#.........###
|
|
||||||
###.#.#.###.#.#.#.#####.#.#.#.###v###.#.#####.#.#####.#.###v###.#.#.###.###########.###.#.#.#.#.#.#.###.###.#.#####.#######.###.#.###########
|
|
||||||
###.#.#.#...#...#.....#...#...###.#...#.....#...#.....#.###...#...#.....#...#...#...#...#.#.#.#.#.#...#.#...#...#...#...###.....#...........#
|
|
||||||
###.#.#.#.###########.###########.#.#######.#####.#####.#####.###########.#.#.#.#.###.###.#.#.#.#.###.#.#.#####.#.###.#.###################.#
|
|
||||||
#...#.#.#.#...........###...#.....#...#...#.....#.#.....#####.......#.....#...#.#...#.#...#...#...###...#.....#.#.###.#.#...#...#...........#
|
|
||||||
#.###.#.#.#.#############.#.#.#######.#.#.#####.#.#.###############.#.#########.###.#.#.#####################.#.#.###.#.#.#.#.#.#.###########
|
|
||||||
#...#.#.#.#.#...........#.#.#.#.....#.#.#.#.....#...#...............#...#...###.....#.#...#.................#.#.#.#...#...#.#.#.#.........###
|
|
||||||
###.#.#.#.#.#.#########.#.#.#.#.###.#.#.#.#.#########.#################.#.#.#########.###.#.###############.#.#.#.#.#######.#.#.#########.###
|
|
||||||
###.#.#.#.#.#.#.......#...#.#...#...#.#.#.#...#...#...#...#.......#...#...#.........#.....#...............#...#...#...#.....#.#.#...#...#...#
|
|
||||||
###.#.#.#.#.#.#.#####.#####.#####.###.#.#.###.#.#.#.###.#.#.#####.#.#.#############.#####################.###########.#.#####.#.#.#.#v#.###.#
|
|
||||||
###.#.#.#.#...#.....#.#...#.#...#.###...#.....#.#.#...#.#...#.....#.#.#.............###...#...#.....#...#.#.......#...#...#...#.#.#.>.#.....#
|
|
||||||
###.#.#.#.#########.#.#.#.#.#.#.#.#############.#.###.#.#####.#####.#.#.###############.#.#.#.#.###.#.#.#.#.#####.#.#####.#.###.#.###v#######
|
|
||||||
###...#...#.....#...#...#...#.#.#...........###.#.###...#.....###...#.#.......#.......#.#.#.#.#...#...#.#.#.....#...###...#...#...#...#...###
|
|
||||||
###########.###.#.###########.#.###########.###.#.#######.#######.###.#######.#.#####.#.#.#.#.###.#####.#.#####.#######.#####.#####.###.#.###
|
|
||||||
#.......###...#...#...###.....#.............#...#.......#.......#.#...#...#...#.#.....#.#.#.#.###.....#...#...#.......#.......#...#.....#...#
|
|
||||||
#.#####.#####.#####.#.###.###################.#########.#######.#.#.###.#.#.###.#.#####.#.#.#.#######.#####.#.#######.#########.#.#########.#
|
|
||||||
#.....#.......#...#.#...#.........#...###...#...#.......#...#...#.#...#.#...#...#.#...#.#.#.#.#...#...#...#.#.###...#...###.....#...........#
|
|
||||||
#####.#########.#.#.###.#########v#.#.###.#.###.#.#######.#.#.###.###.#.#####.###.#.#.#.#.#.#.#.#.#.###.#.#.#.###.#.###.###.#################
|
|
||||||
#.....#.......#.#.#.###...#...#.>.>.#...#.#.#...#...#.....#...###...#.#.....#.#...#.#.#.#.#.#.#.#.#...#.#.#.#.#...#.....#...#.....#.....#####
|
|
||||||
#.#####.#####.#.#.#.#####.#.#.#.#######.#.#.#.#####.#.#############.#.#####v#.#.###.#.#.#.#.#.#.#.###v#.#.#.#.#.#########.###.###.#.###.#####
|
|
||||||
#.....#.#.....#.#.#.#####.#.#...#.......#.#.#...#...#.........###...#.#...>.>.#.#...#.#.#...#.#.#...>.>.#.#.#.#.........#...#.#...#.###...###
|
|
||||||
#####.#.#.#####.#.#.#####.#.#####.#######.#.###.#.###########v###.###.#.#######.#.###.#.#####.#.#########.#.#.#########.###.#.#.###.#####.###
|
|
||||||
#####...#.....#.#...#.....#.....#.......#.#...#.#...#...#...>.>.#.###.#.....###.#.###.#.###...#.......#...#.#.#.......#.#...#.#.....#...#...#
|
|
||||||
#############.#.#####.#########.#######.#.###.#.###.#.#.#.#####.#.###.#####.###.#.###.#.###.#########.#.###.#.#.#####.#.#.###.#######.#.###.#
|
|
||||||
#.............#...#...#...#.....#.......#.#...#...#.#.#.#.....#.#.#...#.....#...#...#.#...#.....#.....#...#.#.#.....#.#.#...#.#.......#.....#
|
|
||||||
#.###############.#.###.#.#.#####.#######.#.#####.#.#.#.#####.#.#.#.###.#####.#####.#.###.#####.#.#######.#.#.#####.#.#.###.#.#.#############
|
|
||||||
#.................#.....#...#####.........#.......#...#.......#...#.....#####.......#.....#####...#######...#.......#...###...#.............#
|
|
||||||
###########################################################################################################################################.#
|
|
||||||
236
day23/main.go
236
day23/main.go
@@ -1,236 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
_ "embed"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
aoc "go.sour.is/advent-of-code"
|
|
||||||
)
|
|
||||||
|
|
||||||
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) {
|
|
||||||
var m aoc.Map[int16, rune]
|
|
||||||
|
|
||||||
start := Point{0, 0}
|
|
||||||
target := Point{0, 0}
|
|
||||||
var text string
|
|
||||||
for scan.Scan() {
|
|
||||||
text = scan.Text()
|
|
||||||
if start == target {
|
|
||||||
start[1] = int16(strings.IndexRune(text, '.'))
|
|
||||||
}
|
|
||||||
m = append(m, []rune(text))
|
|
||||||
}
|
|
||||||
target[0] = int16(len(m) - 1)
|
|
||||||
target[1] = int16(strings.IndexRune(text, '.'))
|
|
||||||
|
|
||||||
result := &result{}
|
|
||||||
|
|
||||||
result.valuePT1 = search(&graph{m: m, start: start, target: target, neighbors: part1nbs})
|
|
||||||
result.valuePT2 = search(&graph{m: m, start: start, target: target, neighbors: part2nbs})
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Point = aoc.Point[int16]
|
|
||||||
type Map = aoc.Map[int16, rune]
|
|
||||||
|
|
||||||
// diretion of path steps
|
|
||||||
type direction int8
|
|
||||||
|
|
||||||
var (
|
|
||||||
U = Point{-1, 0}
|
|
||||||
R = Point{0, 1}
|
|
||||||
D = Point{1, 0}
|
|
||||||
L = Point{0, -1}
|
|
||||||
)
|
|
||||||
|
|
||||||
var directions = []Point{U, R, D, L}
|
|
||||||
|
|
||||||
var dirIDX = func() map[Point]direction {
|
|
||||||
m := make(map[Point]direction, len(directions))
|
|
||||||
for k, v := range directions {
|
|
||||||
m[v] = direction(k)
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}()
|
|
||||||
|
|
||||||
var arrows = []rune{'^', '>', 'v', '<'}
|
|
||||||
|
|
||||||
var arrowIDX = func() map[rune]Point {
|
|
||||||
m := make(map[rune]Point, len(arrows))
|
|
||||||
for k, v := range arrows {
|
|
||||||
m[v] = directions[k]
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}()
|
|
||||||
|
|
||||||
// position on the map
|
|
||||||
type position struct {
|
|
||||||
loc Point
|
|
||||||
direction Point
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p position) step(to Point) position {
|
|
||||||
return position{p.loc.Add(to), to}
|
|
||||||
}
|
|
||||||
|
|
||||||
// implements FindPath graph interface
|
|
||||||
type graph struct {
|
|
||||||
m Map
|
|
||||||
start Point
|
|
||||||
target Point
|
|
||||||
|
|
||||||
neighbors func(g *graph, current position) []position
|
|
||||||
}
|
|
||||||
|
|
||||||
// Neighbors returns valid steps from given position. if at target returns none.
|
|
||||||
func (g *graph) Neighbors(current position) []position {
|
|
||||||
return g.neighbors(g, current)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cost calculates heat cost to neighbor from map
|
|
||||||
func (g *graph) Cost(a, b position) int16 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *graph) Target(a position, c int16) bool {
|
|
||||||
return a.loc == g.target
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *graph) Seen(a position) position {
|
|
||||||
a.direction = Point{}
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
func match[T comparable](match T, lis ...T) bool {
|
|
||||||
for _, b := range lis {
|
|
||||||
if b == match {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func search(g *graph) int {
|
|
||||||
costs, paths := aoc.FindPaths[int16, position](g, position{loc: g.start}, position{loc: g.target})
|
|
||||||
|
|
||||||
for i, path := range paths {
|
|
||||||
log("path length = ", costs[i])
|
|
||||||
printGraph(g.m, path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return int(aoc.Max(0, costs...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// printGraph with the path overlay
|
|
||||||
func printGraph(m Map, path []position) {
|
|
||||||
pts := make(map[Point]position, len(path))
|
|
||||||
for _, pt := range path {
|
|
||||||
pts[pt.loc] = pt
|
|
||||||
}
|
|
||||||
|
|
||||||
for r, row := range m {
|
|
||||||
for c, x := range row {
|
|
||||||
if _, ok := pts[Point{int16(r), int16(c)}]; ok {
|
|
||||||
if x == '.' {
|
|
||||||
fmt.Print("*")
|
|
||||||
} else {
|
|
||||||
fmt.Print(string(x))
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fmt.Print(".")
|
|
||||||
_ = x
|
|
||||||
// fmt.Print(string(x))
|
|
||||||
}
|
|
||||||
fmt.Println("")
|
|
||||||
}
|
|
||||||
fmt.Println("")
|
|
||||||
}
|
|
||||||
|
|
||||||
func opposite(d Point) Point {
|
|
||||||
return directions[(dirIDX[d]+2)%4]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func part1nbs(g *graph, current position) []position {
|
|
||||||
var nbs []position
|
|
||||||
|
|
||||||
if current.loc == g.start {
|
|
||||||
return []position{
|
|
||||||
{current.loc.Add(D), D},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if current.loc == g.target {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// only one direction on arrow.
|
|
||||||
_, r, _ := g.m.Get(current.loc)
|
|
||||||
if match(r, arrows...) {
|
|
||||||
to := arrowIDX[r]
|
|
||||||
if next := current.step(to); g.m.Valid(next.loc) {
|
|
||||||
_, r, _ := g.m.Get(next.loc)
|
|
||||||
d := arrows[(dirIDX[to]+2)%4] // flow from opposite direction
|
|
||||||
if !match(r, rune(d), '#') {
|
|
||||||
nbs = append(nbs, next)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nbs
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, to := range directions {
|
|
||||||
if next := current.step(to); g.m.Valid(next.loc) {
|
|
||||||
_, r, _ := g.m.Get(next.loc)
|
|
||||||
d := arrows[(dirIDX[to]+2)%4] // flow from opposite direction
|
|
||||||
if !match(r, rune(d), '#') {
|
|
||||||
nbs = append(nbs, next)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nbs
|
|
||||||
}
|
|
||||||
|
|
||||||
func part2nbs(g *graph, current position) []position {
|
|
||||||
var nbs []position
|
|
||||||
|
|
||||||
if current.loc == g.start {
|
|
||||||
return []position{
|
|
||||||
{current.loc.Add(D), D},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if current.loc == g.target {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, to := range directions {
|
|
||||||
if next := current.step(to); g.m.Valid(next.loc) {
|
|
||||||
if next.direction == opposite(current.direction) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, r, _ := g.m.Get(next.loc)
|
|
||||||
if r == '#' {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nbs = append(nbs, next)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nbs
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
_ "embed"
|
|
||||||
|
|
||||||
"github.com/matryer/is"
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:embed example.txt
|
|
||||||
var example []byte
|
|
||||||
|
|
||||||
//go:embed input.txt
|
|
||||||
var input []byte
|
|
||||||
|
|
||||||
func TestExample(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
scan := bufio.NewScanner(bytes.NewReader(example))
|
|
||||||
|
|
||||||
result, err := run(scan)
|
|
||||||
is.NoErr(err)
|
|
||||||
|
|
||||||
t.Log(result)
|
|
||||||
// is.Equal(result.valuePT1, 94)
|
|
||||||
is.Equal(result.valuePT2, 154)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.True(result.valuePT1 > 1918)
|
|
||||||
// is.Equal(result.valuePT1, 2074)
|
|
||||||
// is.Equal(result.valuePT2, 0)
|
|
||||||
// }
|
|
||||||
199
grids.go
199
grids.go
@@ -1,32 +1,23 @@
|
|||||||
package aoc
|
package aoc
|
||||||
|
|
||||||
import (
|
|
||||||
"cmp"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"golang.org/x/exp/maps"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Vector struct {
|
type Vector struct {
|
||||||
Offset Point[int]
|
Offset Point
|
||||||
Scale int
|
Scale int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Vector) Point() Point[int] {
|
func (v Vector) Point() Point {
|
||||||
return v.Offset.Scale(v.Scale)
|
return v.Offset.Scale(v.Scale)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Point[T integer] [2]T
|
type Point [2]int
|
||||||
|
|
||||||
func (p Point[T]) Add(a Point[T]) Point[T] {
|
func (p Point) Add(a Point) Point {
|
||||||
return Point[T]{p[0] + a[0], p[1] + a[1]}
|
return Point{p[0] + a[0], p[1] + a[1]}
|
||||||
}
|
}
|
||||||
func (p Point[T]) Scale(m T) Point[T] {
|
func (p Point) Scale(m int) Point {
|
||||||
return Point[T]{p[0] * m, p[1] * m}
|
return Point{p[0] * m, p[1] * m}
|
||||||
}
|
}
|
||||||
func (p Point[T]) Less(b Point[T]) bool {
|
func (p Point) Less(b Point) bool {
|
||||||
if p[0] != b[0] {
|
if p[0] != b[0] {
|
||||||
return p[0] < b[0]
|
return p[0] < b[0]
|
||||||
}
|
}
|
||||||
@@ -50,7 +41,7 @@ func Transpose[T any](matrix [][]T) [][]T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NumPoints the number of the points inside an outline plus the number of points in the outline
|
// NumPoints the number of the points inside an outline plus the number of points in the outline
|
||||||
func NumPoints(outline []Point[int], borderLength int) int {
|
func NumPoints(outline []Point, borderLength int) int {
|
||||||
// shoelace - find the float area in a shape
|
// shoelace - find the float area in a shape
|
||||||
sum := 0
|
sum := 0
|
||||||
for _, p := range Pairwise(outline) {
|
for _, p := range Pairwise(outline) {
|
||||||
@@ -65,183 +56,23 @@ func NumPoints(outline []Point[int], borderLength int) int {
|
|||||||
return (ABS(area) - borderLength/2 + 1) + borderLength
|
return (ABS(area) - borderLength/2 + 1) + borderLength
|
||||||
}
|
}
|
||||||
|
|
||||||
type Map[I integer, T any] [][]T
|
type Map[T any] [][]T
|
||||||
|
|
||||||
func (m *Map[I, T]) Get(p Point[I]) (Point[I], T, bool) {
|
func (m *Map[T]) Get(p Point) (Point, T, bool) {
|
||||||
var zero T
|
var zero T
|
||||||
if !m.Valid(p) {
|
if !m.Valid(p) {
|
||||||
return [2]I{0, 0}, zero, false
|
return [2]int{0, 0}, zero, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return p, (*m)[p[0]][p[1]], true
|
return p, (*m)[p[0]][p[1]], true
|
||||||
}
|
}
|
||||||
func (m *Map[I, T]) Size() (I, I) {
|
func (m *Map[T]) Size() (int, int) {
|
||||||
if m == nil || len(*m) == 0 {
|
if m == nil || len(*m) == 0 {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
return I(len(*m)), I(len((*m)[0]))
|
return len(*m), len((*m)[0])
|
||||||
}
|
}
|
||||||
func (m *Map[I, T]) Valid(p Point[I]) bool {
|
func (m *Map[T]) Valid(p Point) bool {
|
||||||
rows, cols := m.Size()
|
rows, cols := m.Size()
|
||||||
return p[0] >= 0 && p[0] < rows && p[1] >= 0 && p[1] < cols
|
return p[0] >= 0 && p[0] < rows && p[1] >= 0 && p[1] < cols
|
||||||
}
|
}
|
||||||
|
|
||||||
type cmap[C number, N comparable] struct {
|
|
||||||
base pather[C, N]
|
|
||||||
neighbors map[N]map[N]C
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *cmap[C, N]) Cost(a, b N) C {
|
|
||||||
if v, ok := m.neighbors[a]; ok {
|
|
||||||
return v[b]
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
func (m *cmap[C, N]) Neighbors(n N) []N {
|
|
||||||
if v, ok := m.neighbors[n]; ok {
|
|
||||||
return maps.Keys(v)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (m *cmap[C, N]) Target(n N, c C) bool {
|
|
||||||
return m.base.Target(n, c)
|
|
||||||
}
|
|
||||||
func (m *cmap[C, N]) String() string {
|
|
||||||
var b = &strings.Builder{}
|
|
||||||
|
|
||||||
for k, nbs := range m.neighbors {
|
|
||||||
fmt.Fprintln(b, k)
|
|
||||||
for to, v := range nbs {
|
|
||||||
fmt.Fprintln(b, " ", to, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func CompressMap[C number, N comparable](p pather[C, N], start N) pather[C, N] {
|
|
||||||
var next = []N{start}
|
|
||||||
var visited = make(map[N]map[N]C)
|
|
||||||
|
|
||||||
var n N
|
|
||||||
for len(next) > 0 {
|
|
||||||
n, next = next[len(next)-1], next[:len(next)-1]
|
|
||||||
|
|
||||||
if _, ok := visited[n]; ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nbs := p.Neighbors(n)
|
|
||||||
if len(nbs) == 2{
|
|
||||||
a, b := nbs[0], nbs[1]
|
|
||||||
if to, ok := visited[a]; ok {
|
|
||||||
to[b] = to[n] + p.Cost(n, b)
|
|
||||||
delete(to, n)
|
|
||||||
visited[a] = to
|
|
||||||
continue
|
|
||||||
} else if to, ok := visited[b]; ok {
|
|
||||||
to[a] = to[n] + p.Cost(n, a)
|
|
||||||
delete(to, n)
|
|
||||||
visited[b] = to
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
visited[n] = make(map[N]C)
|
|
||||||
next = append(next, nbs...)
|
|
||||||
for _, to := range nbs {
|
|
||||||
visited[n][to] = p.Cost(n, to)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &cmap[C, N]{base: p, neighbors: visited}
|
|
||||||
}
|
|
||||||
|
|
||||||
type adjacencyList[V any, C comparable] map[C][]V
|
|
||||||
type graph[V any, W cmp.Ordered, C comparable] map[C]*vertex[V, W]
|
|
||||||
type graphOption[V any, W cmp.Ordered, C comparable] func(g *graph[V, W, C])
|
|
||||||
type vertex[V any, W cmp.Ordered] struct {
|
|
||||||
Value V
|
|
||||||
Edges edges[V, W]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *vertex[V, W]) Neighbors() []V {
|
|
||||||
var nbs []V
|
|
||||||
sort.Sort(v.Edges)
|
|
||||||
for _, e := range v.Edges {
|
|
||||||
nbs = append(nbs, e.Vertex.Value)
|
|
||||||
}
|
|
||||||
return nbs
|
|
||||||
}
|
|
||||||
|
|
||||||
type edge[V any, W cmp.Ordered] struct {
|
|
||||||
Vertex *vertex[V, W]
|
|
||||||
Weight W
|
|
||||||
}
|
|
||||||
type edges[V any, W cmp.Ordered] []edge[V, W]
|
|
||||||
|
|
||||||
func (e edges[V, W]) Len() int { return len(e) }
|
|
||||||
func (e edges[V, W]) Less(i, j int) bool { return e[i].Weight < e[j].Weight }
|
|
||||||
func (e edges[V, W]) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
|
|
||||||
|
|
||||||
func Graph[V any, W cmp.Ordered, C comparable](opts ...graphOption[V, W, C]) *graph[V, W, C] {
|
|
||||||
g := make(graph[V, W, C])
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(&g)
|
|
||||||
}
|
|
||||||
return &g
|
|
||||||
}
|
|
||||||
func (g *graph[V, W, C]) AddVertex(id C, value V) {
|
|
||||||
(*g)[id] = &vertex[V, W]{Value: value}
|
|
||||||
}
|
|
||||||
func (g *graph[V, W, C]) AddEdge(from, to C, w W) {
|
|
||||||
if g == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if _, ok := (*g)[from]; !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if _, ok := (*g)[to]; !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
(*g)[from].Edges = append((*g)[from].Edges, edge[V, W]{(*g)[to], w})
|
|
||||||
}
|
|
||||||
func (g *graph[V, W, C]) Neighbors(v C) []V {
|
|
||||||
if g == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return (*g)[v].Neighbors()
|
|
||||||
}
|
|
||||||
func (g *graph[V, W, C]) AdjacencyList() adjacencyList[V, C] {
|
|
||||||
m := make(map[C][]V)
|
|
||||||
for id, v := range *g {
|
|
||||||
if len(v.Edges) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m[id] = v.Neighbors()
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func WithAdjacencyList[W cmp.Ordered, C comparable](list adjacencyList[C, C]) graphOption[C, W, C] {
|
|
||||||
var zeroW W
|
|
||||||
return func(g *graph[C, W, C]) {
|
|
||||||
for vertex, edges := range list {
|
|
||||||
if _, ok := (*g)[vertex]; !ok {
|
|
||||||
g.AddVertex(vertex, vertex)
|
|
||||||
}
|
|
||||||
|
|
||||||
// add edges to vertex
|
|
||||||
for _, edge := range edges {
|
|
||||||
// add edge as vertex, if not added
|
|
||||||
if _, ok := (*g)[edge]; !ok {
|
|
||||||
g.AddVertex(edge, edge)
|
|
||||||
}
|
|
||||||
|
|
||||||
g.AddEdge(vertex, edge, zeroW) // no weights in this adjacency list
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package aoc_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/matryer/is"
|
|
||||||
aoc "go.sour.is/advent-of-code"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestReverse(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
is.Equal(aoc.Reverse([]int{1, 2, 3, 4}), []int{4, 3, 2, 1})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadStringToInts(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
is.Equal(aoc.ReadStringToInts([]string{"1", "2", "3"}), []int{1, 2, 3})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRepeat(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
is.Equal(aoc.Repeat(5, 3), []int{5, 5, 5})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTranspose(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
is.Equal(
|
|
||||||
aoc.Transpose(
|
|
||||||
[][]int{
|
|
||||||
{1, 1},
|
|
||||||
{0, 0},
|
|
||||||
{1, 1},
|
|
||||||
},
|
|
||||||
),
|
|
||||||
[][]int{
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
20
math.go
20
math.go
@@ -3,19 +3,19 @@ package aoc
|
|||||||
import "cmp"
|
import "cmp"
|
||||||
|
|
||||||
type uinteger interface {
|
type uinteger interface {
|
||||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
|
uint | uint8 | uint16 | uint32 | uint64
|
||||||
}
|
}
|
||||||
type sinteger interface {
|
type sinteger interface {
|
||||||
~int | ~int8 | ~int16 | ~int32 | ~int64
|
int | int8 | int16 | int32 | int64
|
||||||
}
|
}
|
||||||
type integer interface {
|
type integer interface {
|
||||||
sinteger | uinteger
|
sinteger | uinteger
|
||||||
}
|
}
|
||||||
|
|
||||||
type float interface {
|
// type float interface {
|
||||||
complex64 | complex128 | float32 | float64
|
// complex64 | complex128 | float32 | float64
|
||||||
}
|
// }
|
||||||
type number interface{ integer | float }
|
// type number interface{ integer | float }
|
||||||
|
|
||||||
// greatest common divisor (GCD) via Euclidean algorithm
|
// greatest common divisor (GCD) via Euclidean algorithm
|
||||||
func GCD[T integer](a, b T) T {
|
func GCD[T integer](a, b T) T {
|
||||||
@@ -46,17 +46,17 @@ func LCM[T integer](integers ...T) T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func Sum[T number](arr ...T) T {
|
func Sum[T integer](arr ...T) T {
|
||||||
var acc T
|
var acc T
|
||||||
for _, a := range arr {
|
for _, a := range arr {
|
||||||
acc += a
|
acc += a
|
||||||
}
|
}
|
||||||
return acc
|
return acc
|
||||||
}
|
}
|
||||||
func SumFunc[T any, U number](fn func(T) U, input ...T) U {
|
func SumFunc[T any, U integer](fn func(T) U, input ...T) U {
|
||||||
return Sum(SliceMap(fn, input...)...)
|
return Sum(SliceMap(fn, input...)...)
|
||||||
}
|
}
|
||||||
func SumIFunc[T any, U number](fn func(int, T) U, input ...T) U {
|
func SumIFunc[T any, U integer](fn func(int, T) U, input ...T) U {
|
||||||
return Sum(SliceIMap(fn, input...)...)
|
return Sum(SliceIMap(fn, input...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ func Power2(n int) int {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func ABS[I integer](i I) I {
|
func ABS(i int) int {
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
return -i
|
return -i
|
||||||
}
|
}
|
||||||
|
|||||||
33
math_test.go
33
math_test.go
@@ -1,33 +0,0 @@
|
|||||||
package aoc_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/matryer/is"
|
|
||||||
aoc "go.sour.is/advent-of-code"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestLCM(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
is.Equal(aoc.LCM([]int{}...), 0)
|
|
||||||
is.Equal(aoc.LCM(5), 5)
|
|
||||||
is.Equal(aoc.LCM(5, 3), 15)
|
|
||||||
is.Equal(aoc.LCM(5, 3, 2), 30)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPower2(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
is.Equal(aoc.Power2(0), 1)
|
|
||||||
is.Equal(aoc.Power2(1), 2)
|
|
||||||
is.Equal(aoc.Power2(2), 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestABS(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
is.Equal(aoc.ABS(1), 1)
|
|
||||||
is.Equal(aoc.ABS(0), 0)
|
|
||||||
is.Equal(aoc.ABS(-1), 1)
|
|
||||||
}
|
|
||||||
54
runner.go
54
runner.go
@@ -2,70 +2,25 @@ package aoc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"runtime/pprof"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
|
|
||||||
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
|
|
||||||
|
|
||||||
func Runner[R any, F func(*bufio.Scanner) (R, error)](run F) (R, error) {
|
func Runner[R any, F func(*bufio.Scanner) (R, error)](run F) (R, error) {
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) != 2 {
|
||||||
Log("Usage:", filepath.Base(os.Args[0]), "FILE")
|
Log("Usage:", filepath.Base(os.Args[0]), "FILE")
|
||||||
os.Exit(22)
|
os.Exit(22)
|
||||||
}
|
}
|
||||||
|
|
||||||
inputFilename := os.Args[1]
|
input, err := os.Open(os.Args[1])
|
||||||
os.Args = append(os.Args[:1], os.Args[2:]...)
|
|
||||||
|
|
||||||
flag.Parse()
|
|
||||||
Log(cpuprofile, memprofile, *cpuprofile, *memprofile)
|
|
||||||
if *cpuprofile != "" {
|
|
||||||
Log("enabled cpu profile")
|
|
||||||
f, err := os.Create(*cpuprofile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("could not create CPU profile: ", err)
|
|
||||||
}
|
|
||||||
defer f.Close() // error handling omitted for example
|
|
||||||
Log("write cpu profile to", f.Name())
|
|
||||||
if err := pprof.StartCPUProfile(f); err != nil {
|
|
||||||
log.Fatal("could not start CPU profile: ", err)
|
|
||||||
}
|
|
||||||
defer pprof.StopCPUProfile()
|
|
||||||
}
|
|
||||||
|
|
||||||
if *memprofile != "" {
|
|
||||||
Log("enabled mem profile")
|
|
||||||
defer func() {
|
|
||||||
f, err := os.Create(*memprofile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("could not create memory profile: ", err)
|
|
||||||
}
|
|
||||||
Log("write mem profile to", f.Name())
|
|
||||||
defer f.Close() // error handling omitted for example
|
|
||||||
runtime.GC() // get up-to-date statistics
|
|
||||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
||||||
log.Fatal("could not write memory profile: ", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
input, err := os.Open(inputFilename)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log(err)
|
Log(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
scan := bufio.NewScanner(input)
|
scan := bufio.NewScanner(input)
|
||||||
|
|
||||||
return run(scan)
|
return run(scan)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,10 +33,7 @@ func MustResult[T any](result T, err error) {
|
|||||||
Log("result", result)
|
Log("result", result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Log(v ...any) {
|
func Log(v ...any) { fmt.Fprintln(os.Stderr, v...) }
|
||||||
fmt.Fprint(os.Stderr, time.Now(), ": ")
|
|
||||||
fmt.Fprintln(os.Stderr, v...)
|
|
||||||
}
|
|
||||||
func Logf(format string, v ...any) {
|
func Logf(format string, v ...any) {
|
||||||
if !strings.HasSuffix(format, "\n") {
|
if !strings.HasSuffix(format, "\n") {
|
||||||
format += "\n"
|
format += "\n"
|
||||||
|
|||||||
422
search.go
422
search.go
@@ -1,427 +1,35 @@
|
|||||||
package aoc
|
package aoc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"maps"
|
|
||||||
"math/bits"
|
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
type priorityQueue[T any] struct {
|
type priorityQueue[T any, U []T] struct {
|
||||||
elems []*T
|
elems U
|
||||||
less func(a, b *T) bool
|
less func(a, b T) bool
|
||||||
maxDepth int
|
|
||||||
totalEnqueue int
|
|
||||||
totalDequeue int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PriorityQueue implements a simple slice based queue.
|
func PriorityQueue[T any, U []T](less func(a, b T) bool) *priorityQueue[T, U] {
|
||||||
// less is the function for sorting. reverse a and b to reverse the sort.
|
return &priorityQueue[T, U]{less: less}
|
||||||
// T is the item
|
|
||||||
// U is a slice of T
|
|
||||||
func PriorityQueue[T any](less func(a, b *T) bool) *priorityQueue[T] {
|
|
||||||
return &priorityQueue[T]{less: less}
|
|
||||||
}
|
}
|
||||||
func (pq *priorityQueue[T]) Insert(elem *T) {
|
func (pq *priorityQueue[T, U]) Enqueue(elem T) {
|
||||||
pq.totalEnqueue++
|
|
||||||
|
|
||||||
pq.elems = append(pq.elems, elem)
|
pq.elems = append(pq.elems, elem)
|
||||||
pq.maxDepth = max(pq.maxDepth, len(pq.elems))
|
sort.Slice(pq.elems, func(i, j int) bool { return pq.less(pq.elems[j], pq.elems[i]) })
|
||||||
}
|
}
|
||||||
func (pq *priorityQueue[T]) IsEmpty() bool {
|
func (pq *priorityQueue[T, I]) IsEmpty() bool {
|
||||||
return len(pq.elems) == 0
|
return len(pq.elems) == 0
|
||||||
}
|
}
|
||||||
func (pq *priorityQueue[T]) ExtractMin() *T {
|
func (pq *priorityQueue[T, I]) Dequeue() (T, bool) {
|
||||||
pq.totalDequeue++
|
var elem T
|
||||||
|
|
||||||
var elem *T
|
|
||||||
if pq.IsEmpty() {
|
if pq.IsEmpty() {
|
||||||
return elem
|
return elem, false
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(pq.elems, func(i, j int) bool { return pq.less(pq.elems[j], pq.elems[i]) })
|
|
||||||
pq.elems, elem = pq.elems[:len(pq.elems)-1], pq.elems[len(pq.elems)-1]
|
pq.elems, elem = pq.elems[:len(pq.elems)-1], pq.elems[len(pq.elems)-1]
|
||||||
return elem
|
return elem, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// ManhattanDistance the distance between two points measured along axes at right angles.
|
type DS[T comparable] struct {
|
||||||
func ManhattanDistance[T integer](a, b Point[T]) T {
|
*priorityQueue[T, []T]
|
||||||
return ABS(a[0]-b[0]) + ABS(a[1]-b[1])
|
*set[T]
|
||||||
}
|
|
||||||
|
|
||||||
type pather[C number, N comparable] interface {
|
|
||||||
// Neighbors returns all neighbors to node N that should be considered next.
|
|
||||||
Neighbors(N) []N
|
|
||||||
|
|
||||||
// Cost returns
|
|
||||||
Cost(a, b N) C
|
|
||||||
|
|
||||||
// Target returns true when target reached. receives node and cost.
|
|
||||||
Target(N, C) bool
|
|
||||||
|
|
||||||
// OPTIONAL:
|
|
||||||
// Add heuristic for running as A* search.
|
|
||||||
// Potential(N) C
|
|
||||||
|
|
||||||
// Seen modify value used by seen pruning.
|
|
||||||
// Seen(N) N
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindPath uses the A* path finding algorithem.
|
|
||||||
// g is the graph source that implements the pather interface.
|
|
||||||
//
|
|
||||||
// C is an numeric type for calculating cost/potential
|
|
||||||
// N is the node values. is comparable for storing in visited table for pruning.
|
|
||||||
//
|
|
||||||
// start, end are nodes that dileniate the start and end of the search path.
|
|
||||||
// The returned values are the calculated cost and the path taken from start to end.
|
|
||||||
func FindPath[C integer, N comparable](g pather[C, N], start, end N) (C, []N, map[N]C) {
|
|
||||||
var zero C
|
|
||||||
|
|
||||||
var seenFn = func(a N) N { return a }
|
|
||||||
if s, ok := g.(interface{ Seen(N) N }); ok {
|
|
||||||
seenFn = s.Seen
|
|
||||||
}
|
|
||||||
|
|
||||||
var potentialFn = func(N) C { var zero C; return zero }
|
|
||||||
if p, ok := g.(interface{ Potential(N) C }); ok {
|
|
||||||
potentialFn = p.Potential
|
|
||||||
}
|
|
||||||
|
|
||||||
type node struct {
|
|
||||||
cost C
|
|
||||||
potential C
|
|
||||||
parent *node
|
|
||||||
position N
|
|
||||||
}
|
|
||||||
|
|
||||||
newPath := func(n *node) []N {
|
|
||||||
var path []N
|
|
||||||
for n.parent != nil {
|
|
||||||
path = append(path, n.position)
|
|
||||||
n = n.parent
|
|
||||||
}
|
|
||||||
path = append(path, n.position)
|
|
||||||
|
|
||||||
Reverse(path)
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
|
|
||||||
less := func(a, b *node) bool {
|
|
||||||
return a.cost+a.potential < b.cost+b.potential
|
|
||||||
}
|
|
||||||
|
|
||||||
closed := make(map[N]C)
|
|
||||||
open := FibHeap(less)
|
|
||||||
|
|
||||||
open.Insert(&node{position: start, potential: potentialFn(start)})
|
|
||||||
closed[start] = zero
|
|
||||||
|
|
||||||
for !open.IsEmpty() {
|
|
||||||
current := open.ExtractMin()
|
|
||||||
for _, nb := range g.Neighbors(current.position) {
|
|
||||||
next := &node{
|
|
||||||
position: nb,
|
|
||||||
parent: current,
|
|
||||||
cost: g.Cost(current.position, nb) + current.cost,
|
|
||||||
potential: potentialFn(nb),
|
|
||||||
}
|
|
||||||
|
|
||||||
seen := seenFn(nb)
|
|
||||||
cost, ok := closed[seen]
|
|
||||||
if !ok || next.cost < cost {
|
|
||||||
open.Insert(next)
|
|
||||||
closed[seen] = next.cost
|
|
||||||
}
|
|
||||||
|
|
||||||
if next.potential == zero && g.Target(next.position, next.cost) {
|
|
||||||
return next.cost, newPath(next), closed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return zero, nil, closed
|
|
||||||
}
|
|
||||||
|
|
||||||
type fibTree[T any] struct {
|
|
||||||
value *T
|
|
||||||
parent *fibTree[T]
|
|
||||||
child []*fibTree[T]
|
|
||||||
mark bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *fibTree[T]) Value() *T { return t.value }
|
|
||||||
func (t *fibTree[T]) addAtEnd(n *fibTree[T]) {
|
|
||||||
n.parent = t
|
|
||||||
t.child = append(t.child, n)
|
|
||||||
}
|
|
||||||
|
|
||||||
type fibHeap[T any] struct {
|
|
||||||
trees []*fibTree[T]
|
|
||||||
least *fibTree[T]
|
|
||||||
count uint
|
|
||||||
less func(a, b *T) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func FibHeap[T any](less func(a, b *T) bool) *fibHeap[T] {
|
|
||||||
return &fibHeap[T]{less: less}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) GetMin() *T {
|
|
||||||
return h.least.value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) IsEmpty() bool { return h.least == nil }
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) Insert(v *T) {
|
|
||||||
ntree := &fibTree[T]{value: v}
|
|
||||||
h.trees = append(h.trees, ntree)
|
|
||||||
if h.least == nil || h.less(v, h.least.value) {
|
|
||||||
h.least = ntree
|
|
||||||
}
|
|
||||||
h.count++
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) ExtractMin() *T {
|
|
||||||
smallest := h.least
|
|
||||||
if smallest != nil {
|
|
||||||
// Remove smallest from root trees.
|
|
||||||
for i := range h.trees {
|
|
||||||
pos := h.trees[i]
|
|
||||||
if pos == smallest {
|
|
||||||
h.trees[i] = h.trees[len(h.trees)-1]
|
|
||||||
h.trees = h.trees[:len(h.trees)-1]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add children to root
|
|
||||||
h.trees = append(h.trees, smallest.child...)
|
|
||||||
smallest.child = smallest.child[:0]
|
|
||||||
|
|
||||||
h.least = nil
|
|
||||||
if len(h.trees) > 0 {
|
|
||||||
h.consolidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
h.count--
|
|
||||||
return smallest.value
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) consolidate() {
|
|
||||||
aux := make([]*fibTree[T], bits.Len(h.count)+1)
|
|
||||||
for _, x := range h.trees {
|
|
||||||
order := len(x.child)
|
|
||||||
|
|
||||||
// consolidate the larger roots under smaller roots of same order until we have at most one tree per order.
|
|
||||||
for aux[order] != nil {
|
|
||||||
y := aux[order]
|
|
||||||
if h.less(y.value, x.value) {
|
|
||||||
x, y = y, x
|
|
||||||
}
|
|
||||||
x.addAtEnd(y)
|
|
||||||
aux[order] = nil
|
|
||||||
order++
|
|
||||||
}
|
|
||||||
aux[order] = x
|
|
||||||
}
|
|
||||||
|
|
||||||
h.trees = h.trees[:0]
|
|
||||||
// move ordered trees to root and find least node.
|
|
||||||
for _, k := range aux {
|
|
||||||
if k != nil {
|
|
||||||
k.parent = nil
|
|
||||||
h.trees = append(h.trees, k)
|
|
||||||
if h.least == nil || h.less(k.value, h.least.value) {
|
|
||||||
h.least = k
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) Merge(a *fibHeap[T]) {
|
|
||||||
h.trees = append(h.trees, a.trees...)
|
|
||||||
h.count += a.count
|
|
||||||
if h.least == nil || a.least != nil && h.less(a.least.value, h.least.value) {
|
|
||||||
h.least = a.least
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) find(fn func(*T) bool) *fibTree[T] {
|
|
||||||
var st []*fibTree[T]
|
|
||||||
st = append(st, h.trees...)
|
|
||||||
var tr *fibTree[T]
|
|
||||||
|
|
||||||
for len(st) > 0 {
|
|
||||||
tr, st = st[0], st[1:]
|
|
||||||
ro := *tr.value
|
|
||||||
if fn(&ro) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
st = append(st, tr.child...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return tr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) Find(fn func(*T) bool) *T {
|
|
||||||
if needle := h.find(fn); needle != nil {
|
|
||||||
return needle.value
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) DecreaseKey(find func(*T) bool, decrease func(*T)) {
|
|
||||||
needle := h.find(find)
|
|
||||||
if needle == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
decrease(needle.value)
|
|
||||||
|
|
||||||
if h.less(needle.value, h.least.value) {
|
|
||||||
h.least = needle
|
|
||||||
}
|
|
||||||
|
|
||||||
if parent := needle.parent; parent != nil {
|
|
||||||
if h.less(needle.value, parent.value) {
|
|
||||||
h.cut(needle)
|
|
||||||
h.cascadingCut(parent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) cut(x *fibTree[T]) {
|
|
||||||
parent := x.parent
|
|
||||||
for i := range parent.child {
|
|
||||||
pos := parent.child[i]
|
|
||||||
if pos == x {
|
|
||||||
parent.child[i] = parent.child[len(parent.child)-1]
|
|
||||||
parent.child = parent.child[:len(parent.child)-1]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
x.parent = nil
|
|
||||||
x.mark = false
|
|
||||||
h.trees = append(h.trees, x)
|
|
||||||
|
|
||||||
if h.less(x.value, h.least.value) {
|
|
||||||
h.least = x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *fibHeap[T]) cascadingCut(y *fibTree[T]) {
|
|
||||||
if y.parent != nil {
|
|
||||||
if !y.mark {
|
|
||||||
y.mark = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
h.cut(y)
|
|
||||||
h.cascadingCut(y.parent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindPath uses the A* path finding algorithem.
|
|
||||||
// g is the graph source that implements the pather interface.
|
|
||||||
//
|
|
||||||
// C is an numeric type for calculating cost/potential
|
|
||||||
// N is the node values. is comparable for storing in visited table for pruning.
|
|
||||||
//
|
|
||||||
// start, end are nodes that dileniate the start and end of the search path.
|
|
||||||
// The returned values are the calculated cost and the path taken from start to end.
|
|
||||||
func FindPaths[C integer, N comparable](g pather[C, N], start, end N) ([]C, [][]N) {
|
|
||||||
var zero C
|
|
||||||
// closed := make(map[N]bool)
|
|
||||||
|
|
||||||
var potentialFn = func(N) C { var zero C; return zero }
|
|
||||||
if p, ok := g.(interface{ Potential(N) C }); ok {
|
|
||||||
potentialFn = p.Potential
|
|
||||||
}
|
|
||||||
|
|
||||||
type node struct {
|
|
||||||
cost C
|
|
||||||
potential C
|
|
||||||
parent *node
|
|
||||||
position N
|
|
||||||
closed map[N]bool
|
|
||||||
}
|
|
||||||
|
|
||||||
NewPath := func(n *node) []N {
|
|
||||||
var path []N
|
|
||||||
for n.parent != nil {
|
|
||||||
path = append(path, n.position)
|
|
||||||
n = n.parent
|
|
||||||
}
|
|
||||||
path = append(path, n.position)
|
|
||||||
|
|
||||||
Reverse(path)
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
|
|
||||||
less := func(b, a *node) bool {
|
|
||||||
return b.cost+b.potential < a.cost+a.potential
|
|
||||||
}
|
|
||||||
|
|
||||||
pq := PriorityQueue(less)
|
|
||||||
pq.Insert(&node{position: start, closed: make(map[N]bool)})
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
Log("queue max depth = ", pq.maxDepth, "total enqueue = ", pq.totalEnqueue, "total dequeue = ", pq.totalDequeue)
|
|
||||||
}()
|
|
||||||
|
|
||||||
var seenFn = func(a N) N { return a }
|
|
||||||
if s, ok := g.(interface{ Seen(N) N }); ok {
|
|
||||||
seenFn = s.Seen
|
|
||||||
}
|
|
||||||
|
|
||||||
var targetFn = func(n N, c C) bool { return true }
|
|
||||||
if s, ok := g.(interface{ Target(N, C) bool }); ok {
|
|
||||||
targetFn = s.Target
|
|
||||||
}
|
|
||||||
|
|
||||||
var paths [][]N
|
|
||||||
var costs []C
|
|
||||||
|
|
||||||
for !pq.IsEmpty() {
|
|
||||||
current := pq.ExtractMin()
|
|
||||||
cost, potential, n := current.cost, current.potential, current.position
|
|
||||||
|
|
||||||
seen := seenFn(n)
|
|
||||||
if current.closed[seen] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
current.closed[seen] = true
|
|
||||||
|
|
||||||
if cost > 0 && potential == zero && cost > Max(0, costs...) && targetFn(current.position, cost) {
|
|
||||||
paths = append([][]N(nil), NewPath(current))
|
|
||||||
costs = append([]C(nil), cost)
|
|
||||||
Log("new record = ", cost)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, nb := range g.Neighbors(n) {
|
|
||||||
seen := seenFn(nb)
|
|
||||||
if current.closed[seen] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
cost := g.Cost(n, nb) + current.cost
|
|
||||||
next := &node{
|
|
||||||
position: nb,
|
|
||||||
parent: current,
|
|
||||||
cost: cost,
|
|
||||||
potential: potentialFn(nb),
|
|
||||||
closed: maps.Clone(current.closed),
|
|
||||||
}
|
|
||||||
// check if path is in open list
|
|
||||||
if _, open := current.closed[seen]; !open {
|
|
||||||
next.closed[seen] = false // add to open list
|
|
||||||
pq.Insert(next)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return costs, paths
|
|
||||||
}
|
}
|
||||||
|
|||||||
9
set.go
9
set.go
@@ -57,12 +57,3 @@ func (m *defaultMap[K, V]) Items() []pair[K, V] {
|
|||||||
}
|
}
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
func In[C comparable](n C, haystack ...C) bool {
|
|
||||||
for _, h := range haystack {
|
|
||||||
if n == h {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|||||||
27
set_test.go
27
set_test.go
@@ -1,27 +0,0 @@
|
|||||||
package aoc_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sort"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/matryer/is"
|
|
||||||
aoc "go.sour.is/advent-of-code"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSet(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
s := aoc.Set(1, 2, 3)
|
|
||||||
is.True(!s.Has(0))
|
|
||||||
is.True(s.Has(1))
|
|
||||||
is.True(s.Has(2))
|
|
||||||
is.True(s.Has(3))
|
|
||||||
is.True(!s.Has(4))
|
|
||||||
|
|
||||||
s.Add(4)
|
|
||||||
is.True(s.Has(4))
|
|
||||||
|
|
||||||
items := s.Items()
|
|
||||||
sort.Ints(items)
|
|
||||||
is.Equal(items, []int{1, 2, 3, 4})
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user