| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package main
- import "strconv"
- import "strings"
- import "bufio"
- import "fmt"
- import "io"
- import "os"
- var (
- ex1 = true
- )
- type Compressed struct {
- BSize int
- Count int
- }
- func ReadCompressed(line string) *Compressed {
- if (line[len(line) -1] != ')') {
- return nil
- }
- line = line[0:len(line)-1]
- parts := strings.Split(line, "x")
- if (len(parts) != 2) {
- return nil
- }
- this := new(Compressed)
- var err error
- this.BSize, err = strconv.Atoi(parts[0])
- if (err != nil) {
- return nil
- }
- this.Count, err = strconv.Atoi(parts[1])
- if (err != nil) {
- return nil
- }
- return this
- }
- func readString(reader io.Reader, sep byte) (string, error) {
- buf := ""
- for {
- byteBuffer := make([]byte, 1)
- n, err := reader.Read(byteBuffer)
- buf += string(byteBuffer[:n])
- if (byteBuffer[0] == sep || err != nil) {
- return buf, err
- }
- }
- return "", nil
- }
- func doRead(reader io.Reader) int {
- length := 0
- for {
- line, err := readString(reader, '(')
- if (len(line) > 0 && line[len(line) -1] == '(') {
- length += len(line) -1
- line, err = readString(reader, ')')
- compressed := ReadCompressed(line)
- if (compressed == nil) {
- length += len(line)
- } else {
- var num int
- var buf string
- read := 0
- for ; compressed.BSize > read; {
- bytes := make([]byte, compressed.BSize - read)
- num, err = reader.Read(bytes)
- read += num
- if (!ex1) {
- buf += string(bytes[:num])
- }
- }
- if (ex1) {
- length += read * compressed.Count
- } else {
- length += doRead(strings.NewReader(buf)) * compressed.Count
- }
- }
- } else {
- length += len(line)
- }
- if err != nil {
- break
- }
- }
- return length
- }
- func main() {
- reader := bufio.NewReader(os.Stdin)
- length := doRead(reader)
- fmt.Println(length -1)
- }
|