aoc2023/day-9/util.go
2023-12-09 17:25:53 +00:00

48 lines
833 B
Go

package main
import (
"bufio"
"log"
"os"
"strconv"
"strings"
)
// readlines reads lines from stdin.
// returns input 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
}
// convertToInts converts a string representing ints to an array of ints
func convertToInts(line string) []int {
numStr := strings.Split(line, " ")
nums := make([]int, 0)
for _, elem := range numStr {
n, _ := strconv.Atoi(elem)
nums = append(nums, n)
}
return nums
}
// allEqual returns true if all values in array are equal
func allEqual(a []int) bool {
for i := 1; i < len(a); i++ {
if a[i] != a[0] {
return false
}
}
return true
}