-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path608.go
65 lines (57 loc) · 1.23 KB
/
608.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
// UVa 608 - Counterfeit Dollar
package main
import (
"fmt"
"os"
"strings"
)
type weighing struct {
left, right string
result int
}
var (
diffs = [2]int{-1, 1}
balanceMap = map[string]int{"up": -1, "down": 1, "even": 0}
weightMap = map[int]string{-1: "light", 1: "heavy"}
)
func verify(w weighing, c string, diff int) bool {
l, r := 0, 0
if strings.Contains(w.left, c) {
l = diff
}
if strings.Contains(w.right, c) {
r = diff
}
return r-l == w.result
}
func solve(weighings [3]weighing) (string, int) {
for c := 'A'; c <= 'L'; c++ {
here:
for _, diff := range diffs {
for _, w := range weighings {
if !verify(w, string(c), diff) {
continue here
}
}
return string(c), diff
}
}
return "", 0
}
func main() {
in, _ := os.Open("608.in")
defer in.Close()
out, _ := os.Create("608.out")
defer out.Close()
var kase int
var left, right, result string
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
var weighings [3]weighing
for i := range weighings {
fmt.Fscanf(in, "%s%s%s", &left, &right, &result)
weighings[i] = weighing{left, right, balanceMap[result]}
}
c, diff := solve(weighings)
fmt.Fprintf(out, "%s is the counterfeit coin and it is %s.\n", c, weightMap[diff])
}
}