2023-12-27 14:07:32 -07:00
|
|
|
package aoc
|
|
|
|
|
|
|
|
import (
|
2024-12-01 11:37:27 -07:00
|
|
|
"iter"
|
2023-12-27 14:07:32 -07:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Atoi(s string) int {
|
|
|
|
i, _ := strconv.Atoi(s)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func Repeat[T any](s T, i int) []T {
|
|
|
|
lis := make([]T, i)
|
|
|
|
for i := range lis {
|
|
|
|
lis[i] = s
|
|
|
|
}
|
|
|
|
return lis
|
|
|
|
}
|
|
|
|
|
2024-12-01 11:37:27 -07:00
|
|
|
func Reduce[T, U any](fn func(int, T, U) U, u U, list iter.Seq[T]) U {
|
|
|
|
for i, t := range Enumerate(list) {
|
2023-12-27 14:07:32 -07:00
|
|
|
u = fn(i, t, u)
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func Reverse[T any](arr []T) []T {
|
|
|
|
for i := 0; i < len(arr)/2; i++ {
|
|
|
|
arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i]
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
func SliceMap[T, U any](fn func(T) U, in ...T) []U {
|
|
|
|
lis := make([]U, len(in))
|
|
|
|
for i := range lis {
|
|
|
|
lis[i] = fn(in[i])
|
|
|
|
}
|
|
|
|
return lis
|
|
|
|
}
|
|
|
|
func SliceIMap[T, U any](fn func(int, T) U, in ...T) []U {
|
|
|
|
lis := make([]U, len(in))
|
|
|
|
for i := range lis {
|
|
|
|
lis[i] = fn(i, in[i])
|
|
|
|
}
|
|
|
|
return lis
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pairwise iterates over a list pairing i, i+1
|
|
|
|
func Pairwise[T any](arr []T) [][2]T {
|
|
|
|
var pairs [][2]T
|
|
|
|
for i := range arr[:len(arr)-1] {
|
|
|
|
pairs = append(pairs, [2]T{arr[i], arr[i+1]})
|
|
|
|
}
|
|
|
|
return pairs
|
2023-12-28 18:57:22 -07:00
|
|
|
}
|
2024-12-01 11:37:27 -07:00
|
|
|
|
|
|
|
func Enumerate[T any](arr iter.Seq[T]) iter.Seq2[int, T] {
|
|
|
|
return func(yield func(int, T) bool) {
|
|
|
|
i := 0
|
|
|
|
for v := range arr {
|
|
|
|
if !yield(i, v) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|