-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.go
153 lines (146 loc) · 2.73 KB
/
16.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"encoding/hex"
"fmt"
"math"
"os"
"strconv"
)
type packet struct {
version, id int
value int64
sub []packet
}
func parse(input string, pos int) (packet, int) {
start := pos
version, _ := strconv.ParseInt(input[pos:pos+3], 2, 0)
id, _ := strconv.ParseInt(input[pos+3:pos+6], 2, 0)
if id == 4 {
s := ""
for pos += 6; input[pos] == '1'; pos += 5 {
s = s + input[pos+1:pos+5]
}
value, _ := strconv.ParseInt(s+input[pos+1:pos+5], 2, 0)
return packet{int(version), int(id), value, make([]packet, 0)}, pos + 5 - start
} else if input[pos+6] == '0' {
pos += 7
total, _ := strconv.ParseInt(input[pos:pos+15], 2, 0)
pos += 15
packets := make([]packet, 0)
for total > 0 {
sp, len := parse(input, pos)
packets = append(packets, sp)
total -= int64(len)
pos += len
}
return packet{int(version), int(id), 0, packets}, pos - start
} else {
pos += 7
n, _ := strconv.ParseInt(input[pos:pos+11], 2, 0)
pos += 11
packets := make([]packet, 0)
for i := 0; i < int(n); i++ {
sp, len := parse(input, pos)
packets = append(packets, sp)
pos += len
}
return packet{int(version), int(id), 0, packets}, pos - start
}
}
func (p packet) versions() int {
result := p.version
for _, sp := range p.sub {
result += sp.versions()
}
return result
}
func (p packet) print(prefix string) {
fmt.Print(prefix, p.version, " ")
switch p.id {
case 0:
fmt.Println("+")
case 1:
fmt.Println("*")
case 2:
fmt.Println("min")
case 3:
fmt.Println("max")
case 4:
fmt.Println(p.value)
case 5:
fmt.Println(">")
case 6:
fmt.Println("<")
case 7:
fmt.Println("==")
}
for _, sp := range p.sub {
sp.print(prefix + ".")
}
}
func (p packet) result() int {
r := 0
switch p.id {
case 0:
for _, sp := range p.sub {
r += sp.result()
}
case 1:
r = 1
for _, sp := range p.sub {
r *= sp.result()
}
case 2:
r = math.MaxInt
for _, sp := range p.sub {
if sp.result() < r {
r = sp.result()
}
}
case 3:
r = 0
for _, sp := range p.sub {
if sp.result() > r {
r = sp.result()
}
}
case 4:
r = int(p.value)
case 5:
if p.sub[0].result() > p.sub[1].result() {
r = 1
} else {
r = 0
}
case 6:
if p.sub[0].result() < p.sub[1].result() {
r = 1
} else {
r = 0
}
case 7:
if p.sub[0].result() == p.sub[1].result() {
r = 1
} else {
r = 0
}
default:
panic(fmt.Sprint("invalid packet id", p.id))
}
return r
}
func main() {
input := ""
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
bytes, _ := hex.DecodeString(scanner.Text())
for _, b := range bytes {
input = input + fmt.Sprintf("%08b", b)
}
fmt.Println(input)
p, _ := parse(input, 0)
p.print("")
fmt.Println(p.versions())
fmt.Println(p.result())
}