aoc2023/day-4/util.go
onyx-and-iris f3985498ce use strings.FieldFunc
remove regex and parsing functions

upd compare test
2023-12-04 18:45:30 +00:00

52 lines
885 B
Go

package main
import (
"bufio"
"log"
"math"
"os"
)
// readlines reads lines from stdin.
// Then it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// compare returns the number of matching elements
func compare(a []string, b []string) (int, error) {
n := 0
for _, elem := range a {
if contains(b, elem) {
n += 1
}
}
return n, nil
}
// contains returns true if a slice of elements contains a given element
func contains[T comparable](elems []T, v T) bool {
for _, s := range elems {
if v == s {
return true
}
}
return false
}
// pow returns the value of x to the n
func pow(x, n int) int {
return int(math.Pow(float64(x), float64(n)))
}