diff --git a/day13/common/distress.go b/day13/common/distress.go index c1beaf2..a9d54be 100644 --- a/day13/common/distress.go +++ b/day13/common/distress.go @@ -81,29 +81,26 @@ func (pair Pair) CheckOrder() bool { func ParseListPacket(listString string) ListPacket { + var j int packet := ListPacket{[]Packet{}, false} - currI := 1 - for i := 1; i < len(listString); i++ { - if listString[i] == ',' || listString[i] == ']' { - if listString[currI] >= '0' && listString[currI] <= '9' { - convertedInt, _ := strconv.Atoi(listString[currI:i]) - packet.content = append(packet.content, IntPacket(convertedInt)) - } else if listString[currI] == '[' { - depth := 1 - var j int - for j = currI + 1; depth != 0; j++ { - if listString[j] == '[' { - depth++ - } else if listString[j] == ']' { - depth-- - } + for i := 1; i < len(listString) - 1; i++ { + if listString[i] >= '0' && listString[i] <= '9' { + for j = i + 1; listString[j] != ',' && listString[j] != ']'; j++ {} + convertedInt, _ := strconv.Atoi(listString[i:j]) + packet.content = append(packet.content, IntPacket(convertedInt)) + } else if listString[i] == '[' { + depth := 1 + for j = i + 1; depth != 0; j++ { + if listString[j] == '[' { + depth++ + } else if listString[j] == ']' { + depth-- } - packet.content = append(packet.content, ParseListPacket(listString[currI:j])) - i = j } - currI = i + 1 + packet.content = append(packet.content, ParseListPacket(listString[i:j])) } + i = j } return packet