aoc2023/day-5/util.go

74 lines
1.4 KiB
Go
Raw Normal View History

2023-12-06 04:01:17 +00:00
package main
import (
"bufio"
"log"
"os"
"regexp"
"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
}
// parseLines parses input to a data map
func parseLines(lines []string) {
var _regex_identifier = regexp.MustCompile(`(?P<identifier>[\w-]+) map[:]`)
f := func(c rune) bool {
return !unicode.IsDigit(c)
}
for i := 0; i < len(lines); i++ {
if i == 0 {
seeds = convertToInts(strings.FieldsFunc(lines[i], f))
continue
}
m := _regex_identifier.FindStringSubmatch(lines[i])
if len(m) == 2 {
for i = i + 1; i < len(lines) && len(lines[i]) != 0; i++ {
nums := convertToInts(strings.FieldsFunc(lines[i], f))
dataMap[m[1]] = append(dataMap[m[1]], Data{dest: nums[0], source: nums[1], offset: nums[2]})
}
}
}
}
// 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
}
/*
func isChecked(i int) bool {
for _, bound := range checked {
if i > bound.start && i < bound.end {
return true
}
}
return false
}
*/