Improve day 13

This commit is contained in:
2022-12-13 17:50:45 +01:00
parent d865a9a611
commit c40dee10cc

View File

@@ -81,30 +81,27 @@ func (pair Pair) CheckOrder() bool {
func ParseListPacket(listString string) ListPacket { func ParseListPacket(listString string) ListPacket {
var j int
packet := ListPacket{[]Packet{}, false} packet := ListPacket{[]Packet{}, false}
currI := 1 for i := 1; i < len(listString) - 1; i++ {
for i := 1; i < len(listString); i++ { if listString[i] >= '0' && listString[i] <= '9' {
if listString[i] == ',' || listString[i] == ']' { for j = i + 1; listString[j] != ',' && listString[j] != ']'; j++ {}
if listString[currI] >= '0' && listString[currI] <= '9' { convertedInt, _ := strconv.Atoi(listString[i:j])
convertedInt, _ := strconv.Atoi(listString[currI:i])
packet.content = append(packet.content, IntPacket(convertedInt)) packet.content = append(packet.content, IntPacket(convertedInt))
} else if listString[currI] == '[' { } else if listString[i] == '[' {
depth := 1 depth := 1
var j int for j = i + 1; depth != 0; j++ {
for j = currI + 1; depth != 0; j++ {
if listString[j] == '[' { if listString[j] == '[' {
depth++ depth++
} else if listString[j] == ']' { } else if listString[j] == ']' {
depth-- depth--
} }
} }
packet.content = append(packet.content, ParseListPacket(listString[currI:j])) packet.content = append(packet.content, ParseListPacket(listString[i:j]))
}
i = j i = j
} }
currI = i + 1
}
}
return packet return packet
} }