aoc2024/day-11/internal/one/util.go

56 lines
815 B
Go

package one
import (
"bufio"
"io"
"math"
"strconv"
"strings"
)
func parseLines(r io.Reader) ([]int, error) {
var numbers []int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
numbers = numbersFromString(scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return numbers, nil
}
func numbersFromString(s string) []int {
splitted := strings.Fields(s)
var nums []int
for _, part := range splitted {
nums = append(nums, mustConv(part))
}
return nums
}
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
func splitNumber(n int) (int, int) {
factor := int(math.Pow10(lenItoa(n) / 2))
right := n % factor
left := int(n / factor)
return left, right
}
func lenItoa(n int) int {
return len(strconv.Itoa(n))
}