aoc2023/day-4/util.go

52 lines
885 B
Go
Raw Normal View History

2023-12-04 17:23:02 +00:00
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
2023-12-04 17:23:02 +00:00
}
}
return n, nil
2023-12-04 17:23:02 +00:00
}
// contains returns true if a slice of elements contains a given element
2023-12-04 17:23:02 +00:00
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)))
}