mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
53 lines
784 B
Go
53 lines
784 B
Go
package two
|
|
|
|
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))
|
|
|
|
return int(n / factor), n % factor
|
|
}
|
|
|
|
func lenItoa(n int) int {
|
|
return len(strconv.Itoa(n))
|
|
}
|