This commit is contained in:
Karl Spickermann 2022-12-04 23:09:37 +01:00
parent 35c13300cc
commit 615f33798f
4 changed files with 1067 additions and 0 deletions

42
day4/day4.go Normal file
View File

@ -0,0 +1,42 @@
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 == ','
}

1000
day4/input Normal file

File diff suppressed because it is too large Load Diff

6
day4/testInput Normal file
View File

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

View File

@ -22,6 +22,25 @@ func ReadTextFile(filePath string) []string {
return strings.Split(string(file), "\r\n")
}
func FindRepeatedItems[T int | int64](itemgroup1, itemgroup2 []T) []T {
elementsCompartment1 := map[T]bool{}
repeatedElemnts := map[T]bool{}
for _, item := range itemgroup1 {
elementsCompartment1[item] = true
}
for _, item := range itemgroup2 {
if elementsCompartment1[item] {
repeatedElemnts[item] = true
}
}
v := make([]T, 0, len(repeatedElemnts))
for key, _ := range repeatedElemnts {
v = append(v, key)
}
return v
}
func FindMax[T int | int64](slice []T) (m T) {
for i, e := range slice {
if i == 0 || e > m {