add ranger
Some checks failed
Go Bump / bump (push) Failing after 6s
Go Test / build (push) Failing after 21s

This commit is contained in:
xuu 2024-04-05 16:27:02 -06:00
parent f8fa61672f
commit 50af2114d4
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F
7 changed files with 380 additions and 3 deletions

6
README.md.sig Normal file
View File

@ -0,0 +1,6 @@
-----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgZ+OuJYdd3UiUbyBuO1RlsQR20a
Qm5mKneuMxRjGo3zkAAAAEZmlsZQAAAAAAAAAGc2hhNTEyAAAAUwAAAAtzc2gtZWQyNTUx
OQAAAED8T4C6WILXYZ1KxqDIlVhlrAEjr1Vc+tn8ypcVM3bN7iOexVvuUuvm90nr8eEwKU
acrdDxmq2S+oysQbK+pMUE
-----END SSH SIGNATURE-----

4
go.mod
View File

@ -1,8 +1,6 @@
module go.sour.is/advent-of-code
go 1.21.4
toolchain go1.21.5
go 1.22.0
require (
github.com/matryer/is v1.4.1

69
hacker01/hacker.go Normal file
View 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) {}

15
ranger/channel.go Normal file
View 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
View 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
View 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
View 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
}
}
}
}