-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.go
69 lines (65 loc) · 1.37 KB
/
3.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func zeros(values []string, bit int) int {
var zeros int
for _, v := range values {
if v[bit] == '0' {
zeros++
}
}
return zeros
}
func filter(values []string, bit int, bitval int) []string {
result := make([]string, 0, len(values))
for _, v := range values {
if v[bit] == byte(bitval)+48 {
result = append(result, v)
}
}
return result
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
values := make([]string, 0, 1000)
for scanner.Scan() {
values = append(values, scanner.Text())
}
var gamma, epsilon int
for i := 0; i < len(values[0]); i++ {
zeros := zeros(values, i)
if zeros > len(values)-zeros {
gamma = gamma*2 + 0
epsilon = epsilon*2 + 1
} else {
gamma = gamma*2 + 1
epsilon = epsilon*2 + 0
}
}
fmt.Println(gamma * epsilon)
left := values
for bit := 0; bit < len(values[0]) && len(left) > 1; bit++ {
zeros := zeros(left, bit)
if zeros > len(left)-zeros {
left = filter(left, bit, 0)
} else {
left = filter(left, bit, 1)
}
}
oxygen, _ := strconv.ParseInt(left[0], 2, 32)
left = values
for bit := 0; bit < len(values[0]) && len(left) > 1; bit++ {
zeros := zeros(left, bit)
if zeros > len(left)-zeros {
left = filter(left, bit, 1)
} else {
left = filter(left, bit, 0)
}
}
co2, _ := strconv.ParseInt(left[0], 2, 32)
fmt.Println(oxygen * co2)
}