-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.go
66 lines (61 loc) · 1.33 KB
/
21.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type monkey struct {
n float64
wait []string
op string
}
var monkeys = map[string]monkey{}
func (m monkey) yell() float64 {
if m.n > 0 {
return m.n
}
yell1, yell2 := monkeys[m.wait[0]].yell(), monkeys[m.wait[1]].yell()
switch m.op {
case "+":
return yell1 + yell2
case "-":
return yell1 - yell2
case "*":
return yell1 * yell2
case "/":
return yell1 / yell2
}
panic("unknown operation" + fmt.Sprintf("%v", m))
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
var name string
var n int64
var w1, w2, op string
if strings.ContainsAny(scanner.Text(), "0123456789") {
fmt.Sscanf(scanner.Text(), "%s %d", &name, &n)
} else {
fmt.Sscanf(scanner.Text(), "%s %s %s %s", &name, &w1, &op, &w2)
}
monkeys[name[:4]] = monkey{float64(n), []string{w1, w2}, op}
}
n1 := monkeys["root"].yell()
low, high := int64(1), int64(1000000000000000)
monkeys["humn"] = monkey{float64(low + (high-low)/2), []string{}, ""}
root := monkeys["root"]
root.op = "-"
for {
cmp := root.yell()
if cmp > 0 {
low += (high - low) / 2
} else if cmp < 0 {
high -= (high - low) / 2
} else if cmp == 0 {
fmt.Println(int64(n1), int64(monkeys["humn"].yell()))
return
}
monkeys["humn"] = monkey{float64(low + (high-low)/2), []string{}, ""}
}
}