-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain1.go
46 lines (37 loc) · 807 Bytes
/
main1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"unicode"
)
func main() {
str, _ := ioutil.ReadAll(os.Stdin)
res := decompress(str)
fmt.Println(len(res))
}
func decompress(str []byte) []byte {
res := make([]byte, 0, len(str)*10)
for i := 0; i < len(str); i++ {
// ignore spaces, newline, tabs, etc.
if unicode.IsSpace(rune(str[i])) {
continue
}
if str[i] != '(' {
res = append(res, str[i])
continue
}
closeIdx := strings.IndexByte(string(str[i+1:]), ')') + i + 1
nums := strings.Split(string(str[i+1:closeIdx]), "x")
howMany, _ := strconv.Atoi(nums[0])
times, _ := strconv.Atoi(nums[1])
chunk := str[closeIdx+1 : closeIdx+1+howMany]
for j := 0; j < times; j++ {
res = append(res, chunk...)
}
i = closeIdx + howMany
}
return res
}