mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"log"
|
|
"math"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
func parseAllCards(line string) {
|
|
groups := M{}
|
|
res := _regex_winning_numbers.FindStringSubmatch(line)
|
|
names := _regex_winning_numbers.SubexpNames()
|
|
for i := range res {
|
|
groups[names[i]] = res[i]
|
|
}
|
|
allCards = append(allCards, groups)
|
|
}
|
|
|
|
// numStrToArr converts a string of numbers or an array of numbers
|
|
func numStrToArr(nums string, arr []int) ([]int, error) {
|
|
for _, num := range _regex_nums.FindAllString(nums, -1) {
|
|
n, err := strconv.Atoi(num)
|
|
if err != nil {
|
|
return []int{}, err
|
|
}
|
|
arr = append(arr, n)
|
|
}
|
|
return arr, nil
|
|
}
|
|
|
|
// contains returns true if an 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)))
|
|
}
|