mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
52 lines
885 B
Go
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)))
|
|
}
|