mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
// 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 parseLines(lines []string) {
|
|
f := func(c rune) bool {
|
|
return !unicode.IsDigit(c)
|
|
}
|
|
|
|
times := []int{}
|
|
distances := []int{}
|
|
for _, line := range lines {
|
|
if strings.HasPrefix(line, "Time") {
|
|
times = convertToInts(strings.FieldsFunc(line, f))
|
|
} else {
|
|
distances = convertToInts(strings.FieldsFunc(line, f))
|
|
}
|
|
}
|
|
|
|
for i := range times {
|
|
datas = append(datas, data{time: times[i], distance: distances[i]})
|
|
}
|
|
}
|
|
|
|
// convertToInts converts a string representing ints to an array of ints
|
|
func convertToInts(data []string) []int {
|
|
nums := []int{}
|
|
for _, elem := range data {
|
|
n, _ := strconv.Atoi(elem)
|
|
nums = append(nums, n)
|
|
}
|
|
return nums
|
|
}
|