Improve day 13

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

View File

@@ -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