155 lines
3.1 KiB
Go
155 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"AOC2022/helper"
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
//args := os.Args[1:]
|
|
lines := helper.ReadTextFile("day13/input")
|
|
result := 0
|
|
index := 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)
|
|
|
|
}
|
|
|
|
// 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, 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 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)
|
|
if currentElemLeft > currentElemRight {
|
|
return -1
|
|
}
|
|
if currentElemLeft < currentElemRight {
|
|
return 0
|
|
}
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// 0 Element
|
|
//-1 End Packet
|
|
//-2 Start Array
|
|
//-3 End Array
|
|
func getNextElement(packet string, currentIndex int) (int, int) {
|
|
index := currentIndex + 1
|
|
for index < len(packet) {
|
|
switch packet[index] {
|
|
case ',':
|
|
index++
|
|
break
|
|
case '[':
|
|
return index, -2
|
|
break
|
|
case ']':
|
|
if index < len(packet)-1 {
|
|
return index, -3
|
|
} else {
|
|
index++
|
|
}
|
|
break
|
|
default:
|
|
return index, 0
|
|
}
|
|
}
|
|
return index, -1
|
|
}
|
|
|
|
func getEndofCurrentArray(packet string, currentIndex int) int {
|
|
index := currentIndex
|
|
openedArrays := 1
|
|
for openedArrays > 0 && index < len(packet) {
|
|
switch packet[index] {
|
|
case ']':
|
|
openedArrays--
|
|
case '[':
|
|
openedArrays++
|
|
}
|
|
index++
|
|
}
|
|
if openedArrays == 0 {
|
|
return index
|
|
} else {
|
|
return -1
|
|
}
|
|
}
|
|
|
|
func readElement(packet string, currentIndex int) (element, newIndex int) {
|
|
index := currentIndex
|
|
newIndex = 0
|
|
for newIndex == 0 {
|
|
switch packet[index] {
|
|
case ',':
|
|
newIndex = index
|
|
break
|
|
case ']':
|
|
newIndex = index
|
|
break
|
|
default:
|
|
index++
|
|
}
|
|
}
|
|
return helper.RemoveError(strconv.Atoi(packet[currentIndex:newIndex])), newIndex - 1
|
|
}
|