aoc2023/day-5/util.go

69 lines
1.5 KiB
Go
Raw Normal View History

2023-12-06 04:01:17 +00:00
package main
import (
"bufio"
"os"
"regexp"
"strconv"
"strings"
"unicode"
2024-01-01 11:32:43 +00:00
log "github.com/sirupsen/logrus"
2023-12-06 04:01:17 +00:00
)
// 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
}
2024-01-01 11:32:43 +00:00
var r = regexp.MustCompile(`([\w-]+) map[:]`)
2023-12-06 04:01:17 +00:00
// parseLines parses input to a data map
func parseLines(lines []string) {
f := func(c rune) bool {
return !unicode.IsDigit(c)
}
2024-01-01 11:32:43 +00:00
seeds = convertToInts(strings.FieldsFunc(lines[0], f))
2023-12-06 04:01:17 +00:00
2024-01-01 11:32:43 +00:00
for i := 2; i < len(lines); i++ {
m := r.FindStringSubmatch(lines[i])
2023-12-06 04:01:17 +00:00
if len(m) == 2 {
2024-01-01 11:32:43 +00:00
identifiers = append(identifiers, m[1])
2023-12-06 04:01:17 +00:00
for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ {
2024-01-01 11:32:43 +00:00
d := newData(convertToInts(strings.FieldsFunc(lines[i], f))...)
dataMap[m[1]] = append(dataMap[m[1]], d)
2023-12-06 04:01:17 +00:00
}
}
}
2024-01-01 11:32:43 +00:00
log.Debug(identifiers)
2023-12-06 04:01:17 +00:00
}
// 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
}
2024-01-01 11:32:43 +00:00
// 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
2023-12-06 04:01:17 +00:00
}