AOC2022/day4/day4.go
2022-12-04 23:09:37 +01:00

43 lines
744 B
Go

package main
import (
"AOC2022/helper"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
args := os.Args[1:]
lines := helper.ReadTextFile(args[0])
sum1 := 0
sum2 := 0
for _, line := range lines {
orders := getOrders(line)
if orders[0] <= orders[2] && orders[1] >= orders[3] {
sum1++
} else if orders[2] <= orders[0] && orders[3] >= orders[1] {
sum1++
}
if !(orders[2] > orders[1] || orders[3] < orders[0]) {
sum2++
}
}
fmt.Println(sum1)
fmt.Println(sum2)
}
func getOrders(line string) (orders [4]int) {
orderparts := strings.FieldsFunc(line, split)
for i, part := range orderparts {
orders[i] = helper.RemoveError(strconv.Atoi(part))
}
return
}
func split(r rune) bool {
return r == '-' || r == ','
}