Compare commits
3
Commits
0dc039f032
...
hackerrank
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a5ebbe25d0
|
||
|
|
50af2114d4
|
||
|
|
f8fa61672f
|
No files matched your search
@@ -0,0 +1,6 @@
|
||||
-----BEGIN SSH SIGNATURE-----
|
||||
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgZ+OuJYdd3UiUbyBuO1RlsQR20a
|
||||
Qm5mKneuMxRjGo3zkAAAAEZmlsZQAAAAAAAAAGc2hhNTEyAAAAUwAAAAtzc2gtZWQyNTUx
|
||||
OQAAAED8T4C6WILXYZ1KxqDIlVhlrAEjr1Vc+tn8ypcVM3bN7iOexVvuUuvm90nr8eEwKU
|
||||
acrdDxmq2S+oysQbK+pMUE
|
||||
-----END SSH SIGNATURE-----
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,9 @@ package aoc
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
@@ -109,8 +111,8 @@ func (m *cmap[C, N]) String() string {
|
||||
|
||||
for k, nbs := range m.neighbors {
|
||||
fmt.Fprintln(b, k)
|
||||
for to, v := range nbs {
|
||||
fmt.Fprintln(b, " ", to, k)
|
||||
for to, c := range nbs {
|
||||
fmt.Fprintln(b, " ", to, c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +138,7 @@ func CompressMap[C number, N comparable](p pather[C, N], start N) pather[C, N] {
|
||||
to[b] = to[n] + p.Cost(n, b)
|
||||
delete(to, n)
|
||||
visited[a] = to
|
||||
} else if to, ok := visited[b]; ok {
|
||||
} else if to, ok := visited[b]; ok {
|
||||
to[a] = to[n] + p.Cost(n, a)
|
||||
delete(to, n)
|
||||
visited[b] = to
|
||||
|
||||
@@ -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) {}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
1000000 105823341
|
||||
10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 1Line truncated
|
||||
@@ -0,0 +1,2 @@
|
||||
8 90
|
||||
13 47 74 12 89 74 18 38
|
||||
Whitespace-only changes.
+1
-4
@@ -1,8 +1,6 @@
|
||||
package aoc_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/matryer/is"
|
||||
@@ -27,7 +25,6 @@ func TestRepeat(t *testing.T) {
|
||||
is.Equal(aoc.Repeat(5, 3), []int{5, 5, 5})
|
||||
}
|
||||
|
||||
|
||||
func TestTranspose(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
@@ -44,4 +41,4 @@ func TestTranspose(t *testing.T) {
|
||||
{1, 0, 1},
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -1,5 +1,11 @@
|
||||
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)
|
||||
@@ -24,4 +30,4 @@ func TestABS(t *testing.T) {
|
||||
is.Equal(aoc.ABS(1), 1)
|
||||
is.Equal(aoc.ABS(0), 0)
|
||||
is.Equal(aoc.ABS(-1), 1)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
-1
@@ -1,5 +1,12 @@
|
||||
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)
|
||||
@@ -17,4 +24,4 @@ func TestSet(t *testing.T) {
|
||||
items := s.Items()
|
||||
sort.Ints(items)
|
||||
is.Equal(items, []int{1, 2, 3, 4})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user