Zwischenstand

This commit is contained in:
Karl Spickermann 2022-12-13 21:38:24 +01:00
parent 0b0f0b75dc
commit 233513758a

@ -8,53 +8,84 @@ import (
func main() { func main() {
//args := os.Args[1:] //args := os.Args[1:]
lines := helper.ReadTextFile("day13/testinput") lines := helper.ReadTextFile("day13/input")
result := 0 result := 0
index := 1 index := 1
for i := 18; i < len(lines); i += 3 { for i := 0; i < len(lines); i += 3 {
fmt.Println(i) //fmt.Println(lines[i])
if comparePair(lines[i], lines[i+1]) { //fmt.Println(lines[i+1])
//fmt.Println(comparePair(lines[i], lines[i+1]))
//fmt.Println()
if comparePair(lines[i], lines[i+1]) == 0 {
result += index result += index
} }
if comparePair(lines[i], lines[i+1]) == -2 {
fmt.Println(lines[i])
fmt.Println(lines[i+1])
fmt.Println(comparePair(lines[i], lines[i+1]))
fmt.Println()
}
index++ index++
} }
fmt.Println(result) fmt.Println(result)
} }
func comparePair(left string, right string) bool { // 0 Right Order
lefIndex := 0 //-1 Left Value to high
rightIndex := 0 //-2 Right had no values left
func comparePair(left string, right string) int {
lefIndex := -1
rightIndex := -1
currentElemLeft := 0 currentElemLeft := 0
currentElemRight := 0 currentElemRight := 0
leftElementType := 0
rightElementType := 0
for lefIndex < len(left)-1 { for lefIndex < len(left)-1 {
lefIndex = getNextElement(left, lefIndex) lefIndex, leftElementType = getNextElement(left, lefIndex)
if left[lefIndex] == ']' { rightIndex, rightElementType = getNextElement(right, rightIndex)
lefIndex = getNextElement(left, lefIndex+1) if leftElementType == -1 {
rightIndex = getEndofCurrentArray(right, rightIndex) return 0
rightIndex = getNextElement(right, rightIndex+1) }
} else { if leftElementType == -2 {
rightIndex = getNextElement(right, rightIndex) for rightElementType != -2 && rightElementType != 0 {
if rightIndex == -1 { rightIndex, rightElementType = getNextElement(right, rightIndex)
return false if rightElementType == -1 {
return -2
}
}
if rightElementType == 0 {
for leftElementType != 0 {
lefIndex, leftElementType = getNextElement(left, lefIndex)
if leftElementType == -1 {
return -2
}
}
}
}
if leftElementType == -3 {
rightIndex = getEndofCurrentArray(right, rightIndex)
rightElementType = -3
}
if leftElementType == 0 {
for rightElementType != 0 {
rightIndex, rightElementType = getNextElement(right, rightIndex)
if rightElementType == -1 || rightElementType == -3 {
return -2
} }
if lefIndex == -1 {
return true
} }
currentElemLeft, lefIndex = readElement(left, lefIndex) currentElemLeft, lefIndex = readElement(left, lefIndex)
currentElemRight, rightIndex = readElement(right, rightIndex) currentElemRight, rightIndex = readElement(right, rightIndex)
fmt.Println(currentElemLeft)
fmt.Println(currentElemRight)
fmt.Println()
if currentElemLeft > currentElemRight { if currentElemLeft > currentElemRight {
return false return -1
} }
if currentElemLeft < currentElemRight { if currentElemLeft < currentElemRight {
return true return 0
} }
} }
} }
return true return 0
} }
// 0 Element // 0 Element
@ -62,7 +93,7 @@ func comparePair(left string, right string) bool {
//-2 Start Array //-2 Start Array
//-3 End Array //-3 End Array
func getNextElement(packet string, currentIndex int) (int, int) { func getNextElement(packet string, currentIndex int) (int, int) {
index := currentIndex index := currentIndex + 1
for index < len(packet) { for index < len(packet) {
switch packet[index] { switch packet[index] {
case ',': case ',':
@ -87,13 +118,21 @@ func getNextElement(packet string, currentIndex int) (int, int) {
func getEndofCurrentArray(packet string, currentIndex int) int { func getEndofCurrentArray(packet string, currentIndex int) int {
index := currentIndex index := currentIndex
for index < len(packet) { openedArrays := 1
if packet[index] == ']' { for openedArrays > 0 && index < len(packet) {
return index switch packet[index] {
case ']':
openedArrays--
case '[':
openedArrays++
} }
index++ index++
} }
if openedArrays == 0 {
return index
} else {
return -1 return -1
}
} }
func readElement(packet string, currentIndex int) (element, newIndex int) { func readElement(packet string, currentIndex int) (element, newIndex int) {
@ -111,5 +150,5 @@ func readElement(packet string, currentIndex int) (element, newIndex int) {
index++ index++
} }
} }
return helper.RemoveError(strconv.Atoi(packet[currentIndex:newIndex])), newIndex return helper.RemoveError(strconv.Atoi(packet[currentIndex:newIndex])), newIndex - 1
} }