mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-22 10:20:48 +00:00
48 lines
833 B
Go
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
|
||
|
}
|