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