aoc2023/day-5/util.go
2024-01-01 11:32:43 +00:00

69 lines
1.5 KiB
Go

package main
import (
"bufio"
"os"
"regexp"
"strconv"
"strings"
"unicode"
log "github.com/sirupsen/logrus"
)
// 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
}
var r = regexp.MustCompile(`([\w-]+) map[:]`)
// parseLines parses input to a data map
func parseLines(lines []string) {
f := func(c rune) bool {
return !unicode.IsDigit(c)
}
seeds = convertToInts(strings.FieldsFunc(lines[0], f))
for i := 2; i < len(lines); i++ {
m := r.FindStringSubmatch(lines[i])
if len(m) == 2 {
identifiers = append(identifiers, m[1])
for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ {
d := newData(convertToInts(strings.FieldsFunc(lines[i], f))...)
dataMap[m[1]] = append(dataMap[m[1]], d)
}
}
}
log.Debug(identifiers)
}
// 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
}
// isOverlapping returns true if two ranges overlap. see:
// https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap/325964#325964
func isOverlapping(maxStart, minEnd int) bool {
return maxStart < minEnd
}