Compare commits
4 Commits
graphs
...
hackerrank
| Author | SHA1 | Date | |
|---|---|---|---|
|
a5ebbe25d0
|
|||
|
50af2114d4
|
|||
|
f8fa61672f
|
|||
|
951c2c298a
|
6
README.md.sig
Normal file
6
README.md.sig
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
-----BEGIN SSH SIGNATURE-----
|
||||||
|
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgZ+OuJYdd3UiUbyBuO1RlsQR20a
|
||||||
|
Qm5mKneuMxRjGo3zkAAAAEZmlsZQAAAAAAAAAGc2hhNTEyAAAAUwAAAAtzc2gtZWQyNTUx
|
||||||
|
OQAAAED8T4C6WILXYZ1KxqDIlVhlrAEjr1Vc+tn8ypcVM3bN7iOexVvuUuvm90nr8eEwKU
|
||||||
|
acrdDxmq2S+oysQbK+pMUE
|
||||||
|
-----END SSH SIGNATURE-----
|
||||||
97
aoc_test.go
97
aoc_test.go
@@ -9,66 +9,6 @@ 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)
|
||||||
@@ -116,23 +56,6 @@ func TestPriorityQueue(t *testing.T) {
|
|||||||
is.True(v == nil)
|
is.True(v == nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 ExamplePriorityQueue() {
|
func ExamplePriorityQueue() {
|
||||||
type memo struct {
|
type memo struct {
|
||||||
@@ -194,26 +117,6 @@ 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 TestStack(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
|
|
||||||
s := aoc.Stack(1, 2, 3, 4)
|
|
||||||
is.True(!s.IsEmpty())
|
|
||||||
is.Equal(s.Pop(), 4)
|
|
||||||
is.Equal(s.Pop(), 3)
|
|
||||||
is.Equal(s.Pop(), 2)
|
|
||||||
is.Equal(s.Pop(), 1)
|
|
||||||
is.True(s.IsEmpty())
|
|
||||||
s.Push(4, 3, 2, 1)
|
|
||||||
is.True(!s.IsEmpty())
|
|
||||||
is.Equal(s.Pop(), 1)
|
|
||||||
is.Equal(s.Pop(), 2)
|
|
||||||
is.Equal(s.Pop(), 3)
|
|
||||||
is.Equal(s.Pop(), 4)
|
|
||||||
is.True(s.IsEmpty())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGraph(t *testing.T) {
|
func TestGraph(t *testing.T) {
|
||||||
is := is.New(t)
|
is := is.New(t)
|
||||||
|
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -1,8 +1,6 @@
|
|||||||
module go.sour.is/advent-of-code
|
module go.sour.is/advent-of-code
|
||||||
|
|
||||||
go 1.21.4
|
go 1.22.0
|
||||||
|
|
||||||
toolchain go1.21.5
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/matryer/is v1.4.1
|
github.com/matryer/is v1.4.1
|
||||||
|
|||||||
80
grids.go
80
grids.go
@@ -2,7 +2,11 @@ package aoc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Vector struct {
|
type Vector struct {
|
||||||
@@ -82,6 +86,76 @@ func (m *Map[I, T]) Valid(p Point[I]) bool {
|
|||||||
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, c := range nbs {
|
||||||
|
fmt.Fprintln(b, " ", to, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
} 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 adjacencyList[V any, C comparable] map[C][]V
|
||||||
type graph[V any, W cmp.Ordered, C comparable] map[C]*vertex[V, W]
|
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 graphOption[V any, W cmp.Ordered, C comparable] func(g *graph[V, W, C])
|
||||||
@@ -117,7 +191,7 @@ func Graph[V any, W cmp.Ordered, C comparable](opts ...graphOption[V, W, C]) *gr
|
|||||||
return &g
|
return &g
|
||||||
}
|
}
|
||||||
func (g *graph[V, W, C]) AddVertex(id C, value V) {
|
func (g *graph[V, W, C]) AddVertex(id C, value V) {
|
||||||
(*g)[id] = &vertex[V,W]{Value: value}
|
(*g)[id] = &vertex[V, W]{Value: value}
|
||||||
}
|
}
|
||||||
func (g *graph[V, W, C]) AddEdge(from, to C, w W) {
|
func (g *graph[V, W, C]) AddEdge(from, to C, w W) {
|
||||||
if g == nil {
|
if g == nil {
|
||||||
@@ -130,7 +204,7 @@ func (g *graph[V, W, C]) AddEdge(from, to C, w W) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
(*g)[from].Edges = append((*g)[from].Edges, edge[V,W]{(*g)[to], w})
|
(*g)[from].Edges = append((*g)[from].Edges, edge[V, W]{(*g)[to], w})
|
||||||
}
|
}
|
||||||
func (g *graph[V, W, C]) Neighbors(v C) []V {
|
func (g *graph[V, W, C]) Neighbors(v C) []V {
|
||||||
if g == nil {
|
if g == nil {
|
||||||
@@ -170,5 +244,3 @@ func WithAdjacencyList[W cmp.Ordered, C comparable](list adjacencyList[C, C]) gr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func GraphFromMap()
|
|
||||||
|
|||||||
69
hacker01/hacker.go
Normal file
69
hacker01/hacker.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "os"
|
||||||
|
import "bufio"
|
||||||
|
import "strconv"
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
|
||||||
|
var input1 = strings.NewReader(`
|
||||||
|
6
|
||||||
|
1 2 5 3 6 4
|
||||||
|
`)
|
||||||
|
|
||||||
|
var input2 = strings.NewReader(`
|
||||||
|
15
|
||||||
|
1 14 3 7 4 5 15 6 13 10 11 2 12 8 9
|
||||||
|
`)
|
||||||
|
|
||||||
|
var input3 = os.Stdin
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var length int
|
||||||
|
var tree *node
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(input1)
|
||||||
|
for scanner.Scan() {
|
||||||
|
if length == 0 {
|
||||||
|
length, _ = strconv.Atoi(scanner.Text())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, txt := range strings.Fields(scanner.Text()) {
|
||||||
|
if v, err := strconv.Atoi(txt); err == nil {
|
||||||
|
tree = insert(tree, &node{value: v})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foo(input{1, "hi"})
|
||||||
|
|
||||||
|
fmt.Println(tree)
|
||||||
|
}
|
||||||
|
|
||||||
|
type node struct {
|
||||||
|
value int
|
||||||
|
left *node
|
||||||
|
right *node
|
||||||
|
}
|
||||||
|
|
||||||
|
func insert(root, n *node) *node {
|
||||||
|
if root == nil {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
if root.value > n.value {
|
||||||
|
root.left = insert(root.left, n)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
root.right = insert(root.right, n)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) String() string {
|
||||||
|
if n == nil { return "" }
|
||||||
|
return fmt.Sprintf("%v %v%v", n.value, n.left.String(), n.right.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
type input struct{a int; b string}
|
||||||
|
func foo (in input) {}
|
||||||
109
hacker02/hacker.go
Normal file
109
hacker02/hacker.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
aoc "go.sour.is/advent-of-code"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Complete the 'cookies' function below.
|
||||||
|
*
|
||||||
|
* The function is expected to return an INTEGER.
|
||||||
|
* The function accepts following parameters:
|
||||||
|
* 1. INTEGER k
|
||||||
|
* 2. INTEGER_ARRAY A
|
||||||
|
*/
|
||||||
|
|
||||||
|
func cookies(k int32, A []int32) int32 {
|
||||||
|
var i int32
|
||||||
|
// fmt.Println(" ", A)
|
||||||
|
// sort.Slice(A, func(i, j int) bool { return A[j] < A[i]})
|
||||||
|
// for A[len(A)-1] < k {
|
||||||
|
// if len(A) < 2 {
|
||||||
|
// return -1
|
||||||
|
// }
|
||||||
|
// // fmt.Println(" ", A[len(A)-1], " + 2x", A[len(A)-2], 2*A[len(A)-2] + A[len(A)-1])
|
||||||
|
// A[len(A)-2] = 2*A[len(A)-2] + A[len(A)-1]
|
||||||
|
// A = A[:len(A)-1]
|
||||||
|
|
||||||
|
// for j:=len(A)-1; j>0 && A[j] > A[j-1]; j-- {
|
||||||
|
// A[j], A[j-1] = A[j-1], A[j]
|
||||||
|
// }
|
||||||
|
// i++
|
||||||
|
// fmt.Println("A", len(A), i)
|
||||||
|
// }
|
||||||
|
|
||||||
|
pq := aoc.FibHeap(func(a, b *int32) bool { return *a < *b })
|
||||||
|
for i := range A {
|
||||||
|
pq.Insert(&A[i])
|
||||||
|
}
|
||||||
|
for !pq.IsEmpty() && *pq.GetMin() < k {
|
||||||
|
first := *pq.ExtractMin()
|
||||||
|
second := *pq.ExtractMin()
|
||||||
|
third := first + 2*second
|
||||||
|
|
||||||
|
pq.Insert(&third)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024)
|
||||||
|
|
||||||
|
stdout, err := os.Create("/dev/stderr")
|
||||||
|
checkError(err)
|
||||||
|
|
||||||
|
defer stdout.Close()
|
||||||
|
|
||||||
|
writer := bufio.NewWriterSize(stdout, 16*1024*1024)
|
||||||
|
|
||||||
|
firstMultipleInput := strings.Split(strings.TrimSpace(readLine(reader)), " ")
|
||||||
|
|
||||||
|
nTemp, err := strconv.ParseInt(firstMultipleInput[0], 10, 64)
|
||||||
|
checkError(err)
|
||||||
|
n := int32(nTemp)
|
||||||
|
|
||||||
|
kTemp, err := strconv.ParseInt(firstMultipleInput[1], 10, 64)
|
||||||
|
checkError(err)
|
||||||
|
k := int32(kTemp)
|
||||||
|
|
||||||
|
ATemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
|
||||||
|
|
||||||
|
var A []int32
|
||||||
|
|
||||||
|
for i := 0; i < int(n); i++ {
|
||||||
|
AItemTemp, err := strconv.ParseInt(ATemp[i], 10, 64)
|
||||||
|
checkError(err)
|
||||||
|
AItem := int32(AItemTemp)
|
||||||
|
A = append(A, AItem)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := cookies(k, A)
|
||||||
|
|
||||||
|
fmt.Fprintf(writer, "%d\n", result)
|
||||||
|
|
||||||
|
writer.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
func readLine(reader *bufio.Reader) string {
|
||||||
|
str, _, err := reader.ReadLine()
|
||||||
|
if err == io.EOF {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimRight(string(str), "\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkError(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
2
hacker02/input.txt
Normal file
2
hacker02/input.txt
Normal file
File diff suppressed because one or more lines are too long
2
hacker02/other.txt
Normal file
2
hacker02/other.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
8 90
|
||||||
|
13 47 74 12 89 74 18 38
|
||||||
0
hacker02/output.txt
Normal file
0
hacker02/output.txt
Normal file
44
itertools_test.go
Normal file
44
itertools_test.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
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},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
33
math_test.go
Normal file
33
math_test.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
15
ranger/channel.go
Normal file
15
ranger/channel.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
|
||||||
|
func countChan(n int) <-chan int {
|
||||||
|
ch := make(chan int)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for i := range n {
|
||||||
|
ch <- i
|
||||||
|
}
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ch
|
||||||
|
}
|
||||||
84
ranger/range.go
Normal file
84
ranger/range.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// export GOEXPERIMENT=rangefunc
|
||||||
|
import "iter"
|
||||||
|
|
||||||
|
func Step(start, stop, step int) iter.Seq[int] {
|
||||||
|
if step < 0 {
|
||||||
|
return func(yield func(int) bool) {
|
||||||
|
for i := start; i <= start && i > stop; i += step {
|
||||||
|
next := yield(i)
|
||||||
|
if !next {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if step > 0 {
|
||||||
|
return func(yield func(int) bool) {
|
||||||
|
for i := start; i >= start && i < stop; i += step {
|
||||||
|
next := yield(i)
|
||||||
|
if !next {
|
||||||
|
println("stopped!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(yield func(int) bool) { yield(start) }
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fib() iter.Seq[uint] {
|
||||||
|
return func(yield func(uint) bool) {
|
||||||
|
previous, value := uint(1), uint(1)
|
||||||
|
next := yield(value)
|
||||||
|
for next {
|
||||||
|
next = yield(value)
|
||||||
|
value, previous = value+previous, value
|
||||||
|
if value < previous {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fizzbuzzed[T int | uint](from iter.Seq[T]) iter.Seq2[T, string] {
|
||||||
|
pull, stop := iter.Pull(from)
|
||||||
|
return func(yield func(T, string) bool) {
|
||||||
|
defer stop()
|
||||||
|
for {
|
||||||
|
var fizzbuzz string
|
||||||
|
p, ok := pull()
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if p%3 == 0 {
|
||||||
|
fizzbuzz = "fizz"
|
||||||
|
}
|
||||||
|
if p%5 == 0 {
|
||||||
|
fizzbuzz += "buzz"
|
||||||
|
}
|
||||||
|
if !yield(p, fizzbuzz) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// func main() {
|
||||||
|
// for i, v := range Fizzbuzzed(Fib()) {
|
||||||
|
// println(i, v)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
func countScan(n int) (func() bool, func() int) {
|
||||||
|
pos := -1
|
||||||
|
return func() bool {
|
||||||
|
pos++
|
||||||
|
return pos < n
|
||||||
|
}, func() int {
|
||||||
|
return pos
|
||||||
|
}
|
||||||
|
}
|
||||||
193
ranger/range_test.go
Normal file
193
ranger/range_test.go
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/matryer/is"
|
||||||
|
)
|
||||||
|
|
||||||
|
// func BenchmarkChan(b *testing.B) {
|
||||||
|
// sum := 0
|
||||||
|
|
||||||
|
// for n := range countChan(b.N) {
|
||||||
|
// sum += n
|
||||||
|
// }
|
||||||
|
// b.StopTimer()
|
||||||
|
// b.Log(sum)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func BenchmarkScan(b *testing.B) {
|
||||||
|
// sum := 0
|
||||||
|
// scan, value := countScan(b.N)
|
||||||
|
// for scan() {
|
||||||
|
// sum += value()
|
||||||
|
// }
|
||||||
|
// b.StopTimer()
|
||||||
|
// b.Log(sum)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func BenchmarkRange(b *testing.B) {
|
||||||
|
// sum := 0
|
||||||
|
// for n := range countRF(b.N) {
|
||||||
|
// sum += n
|
||||||
|
// }
|
||||||
|
// b.StopTimer()
|
||||||
|
// b.Log(sum)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func TestRangeS(t *testing.T) {
|
||||||
|
// for i := range Step(0, 10, 3) {
|
||||||
|
// t.Log(i)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for i := range Step(10, 0, -3) {
|
||||||
|
// t.Log(i)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
func TestSelect(t *testing.T) {
|
||||||
|
done1 := make(chan struct{})
|
||||||
|
done2 := make(chan struct{})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
close(done1)
|
||||||
|
close(done2)
|
||||||
|
}()
|
||||||
|
|
||||||
|
for i, ch := range []chan struct{}{done1, done2} {
|
||||||
|
<-ch
|
||||||
|
println("closed", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGCD(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
is.Equal(gcdOfStrings("ABAB", "ABABAB"), "AB")
|
||||||
|
is.Equal(gcdOfStrings("ABAB", "ABABABAB"), "ABAB")
|
||||||
|
is.Equal(gcdOfStrings("ABC", "ABCABC"), "ABC")
|
||||||
|
is.Equal(gcdOfStrings("ABC", "ABCDEF"), "")
|
||||||
|
is.Equal(gcdOfStrings("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
||||||
|
}
|
||||||
|
func gcdOfStrings(str1 string, str2 string) string {
|
||||||
|
if len(str1) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
runes1 := []rune(str1)
|
||||||
|
runes2 := []rune(str2)
|
||||||
|
|
||||||
|
n := max(len(runes1), len(runes2))
|
||||||
|
m := min(len(runes1), len(runes2))
|
||||||
|
|
||||||
|
if len(runes2) == m {
|
||||||
|
runes1, runes2 = runes2, runes1
|
||||||
|
}
|
||||||
|
|
||||||
|
if isRepeating(runes1, runes2) {
|
||||||
|
return string(runes1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := n; i > 0; i-- {
|
||||||
|
if runes1[i%len(runes1)] != runes2[i%len(runes2)] {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
gcd := runes2[:i]
|
||||||
|
if isRepeating(gcd, runes1) && isRepeating(gcd, runes2) {
|
||||||
|
return string(gcd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
func isRepeating(str1, str2 []rune) bool {
|
||||||
|
if len(str1) > len(str2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(str2)%len(str1) != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := range str2 {
|
||||||
|
if str1[i%len(str1)] != str2[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func canPlaceFlowers(flowerbed []int, n int) bool {
|
||||||
|
var c int
|
||||||
|
|
||||||
|
for i := 0; i < len(flowerbed); i++ {
|
||||||
|
if (i == 0 || flowerbed[i-1] == 0) &&
|
||||||
|
flowerbed[i] == 0 &&
|
||||||
|
(i == len(flowerbed)-1 || flowerbed[i+1] == 0) {
|
||||||
|
c++
|
||||||
|
flowerbed[i] = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c >= n
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFlowers(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
is.Equal(canPlaceFlowers([]int{1, 0, 0, 0, 1}, 1), true)
|
||||||
|
is.Equal(canPlaceFlowers([]int{0, 0, 1, 0, 1}, 1), true)
|
||||||
|
is.Equal(canPlaceFlowers([]int{1, 0, 1, 0, 0}, 1), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dailyTemperatures(temperatures []int) []int {
|
||||||
|
answers := make([]int, len(temperatures))
|
||||||
|
var stack [][2]int
|
||||||
|
push := func(v int) {
|
||||||
|
if len(stack) == 0 {
|
||||||
|
stack = append(stack, [2]int{v, 1})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
end := len(stack)-1
|
||||||
|
if v < stack[end][0]{
|
||||||
|
stack = append(stack, [2]int{v, 1})
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
if v == stack[end][0] {
|
||||||
|
stack[len(stack)-1][1]++
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var vs = [2]int{v, 1}
|
||||||
|
// for ; end>0; end-- {
|
||||||
|
// if stack[end][0]<vs[0] {
|
||||||
|
// vs[1]+=stack[end][1]
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
stack = append(stack, vs)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := len(temperatures) - 2; i >= 0; i-- {
|
||||||
|
push(temperatures[i+1])
|
||||||
|
currentTemp := temperatures[i]
|
||||||
|
days := 1
|
||||||
|
end := len(stack)-1
|
||||||
|
for j := range stack {
|
||||||
|
nextTemp := stack[end-j]
|
||||||
|
if currentTemp < nextTemp[0] {
|
||||||
|
answers[i] = days
|
||||||
|
break
|
||||||
|
}
|
||||||
|
days+=nextTemp[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return answers
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTemps(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
is.Equal(dailyTemperatures(
|
||||||
|
[]int{73, 74, 75, 71, 69, 72, 76, 73}),
|
||||||
|
[]int{1, 1, 4, 2, 1, 1, 0, 0})
|
||||||
|
is.Equal(dailyTemperatures(
|
||||||
|
[]int{99,99,99,99,100}), []int{4,3,2,1,0})
|
||||||
|
}
|
||||||
12
ranger/rangefunc.go
Normal file
12
ranger/rangefunc.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
|
||||||
|
func countRF(n int) func(yield func(int) bool) {
|
||||||
|
return func(yield func(int) bool) {
|
||||||
|
for i := range n {
|
||||||
|
if !yield(i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
search.go
24
search.go
@@ -42,30 +42,6 @@ func (pq *priorityQueue[T]) ExtractMin() *T {
|
|||||||
return elem
|
return elem
|
||||||
}
|
}
|
||||||
|
|
||||||
type stack[T any] []T
|
|
||||||
|
|
||||||
func Stack[T any](a ...T) *stack[T] {
|
|
||||||
var s stack[T] = a
|
|
||||||
return &s
|
|
||||||
}
|
|
||||||
func (s *stack[T]) Push(a ...T) {
|
|
||||||
if s == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
*s = append(*s, a...)
|
|
||||||
}
|
|
||||||
func (s *stack[T]) IsEmpty() bool {
|
|
||||||
return s == nil || len(*s) == 0
|
|
||||||
}
|
|
||||||
func (s *stack[T]) Pop() T {
|
|
||||||
var a T
|
|
||||||
if s.IsEmpty() {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
a, *s = (*s)[len(*s)-1], (*s)[:len(*s)-1]
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
// ManhattanDistance the distance between two points measured along axes at right angles.
|
// ManhattanDistance the distance between two points measured along axes at right angles.
|
||||||
func ManhattanDistance[T integer](a, b Point[T]) T {
|
func ManhattanDistance[T integer](a, b Point[T]) T {
|
||||||
return ABS(a[0]-b[0]) + ABS(a[1]-b[1])
|
return ABS(a[0]-b[0]) + ABS(a[1]-b[1])
|
||||||
|
|||||||
27
set_test.go
Normal file
27
set_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
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