2022-12-13 20:45:45 +01:00
package main
import (
"AOC2022/helper"
"fmt"
"strconv"
)
func main ( ) {
//args := os.Args[1:]
2022-12-13 21:38:24 +01:00
lines := helper . ReadTextFile ( "day13/input" )
2022-12-13 20:45:45 +01:00
result := 0
index := 1
2022-12-13 21:38:24 +01:00
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 {
2022-12-13 20:45:45 +01:00
result += index
}
2022-12-13 21:38:24 +01:00
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 ( )
}
2022-12-13 20:45:45 +01:00
index ++
}
fmt . Println ( result )
}
2022-12-13 21:38:24 +01:00
// 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
2022-12-13 20:45:45 +01:00
currentElemLeft := 0
currentElemRight := 0
2022-12-13 21:38:24 +01:00
leftElementType := 0
rightElementType := 0
2022-12-13 20:45:45 +01:00
for lefIndex < len ( left ) - 1 {
2022-12-13 21:38:24 +01:00
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
}
}
2022-12-13 20:45:45 +01:00
}
2022-12-13 21:38:24 +01:00
}
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
}
2022-12-13 20:45:45 +01:00
}
currentElemLeft , lefIndex = readElement ( left , lefIndex )
currentElemRight , rightIndex = readElement ( right , rightIndex )
if currentElemLeft > currentElemRight {
2022-12-13 21:38:24 +01:00
return - 1
2022-12-13 20:45:45 +01:00
}
if currentElemLeft < currentElemRight {
2022-12-13 21:38:24 +01:00
return 0
2022-12-13 20:45:45 +01:00
}
}
}
2022-12-13 21:38:24 +01:00
return 0
2022-12-13 20:45:45 +01:00
}
// 0 Element
//-1 End Packet
//-2 Start Array
//-3 End Array
func getNextElement ( packet string , currentIndex int ) ( int , int ) {
2022-12-13 21:38:24 +01:00
index := currentIndex + 1
2022-12-13 20:45:45 +01:00
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
2022-12-13 21:38:24 +01:00
openedArrays := 1
for openedArrays > 0 && index < len ( packet ) {
switch packet [ index ] {
case ']' :
openedArrays --
case '[' :
openedArrays ++
2022-12-13 20:45:45 +01:00
}
index ++
}
2022-12-13 21:38:24 +01:00
if openedArrays == 0 {
return index
} else {
return - 1
}
2022-12-13 20:45:45 +01:00
}
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 ++
}
}
2022-12-13 21:38:24 +01:00
return helper . RemoveError ( strconv . Atoi ( packet [ currentIndex : newIndex ] ) ) , newIndex - 1
2022-12-13 20:45:45 +01:00
}