-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.go
49 lines (47 loc) · 1.09 KB
/
24.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
package main
import "bufio"
import "os"
import "strings"
import "fmt"
func main() {
type gate struct {t string; in1, in2 string}
var scanner = bufio.NewScanner(os.Stdin)
var inputs = make(map[string]bool)
var gates = make(map[string]gate)
for scanner.Scan() {
var l = scanner.Text()
if l == "" { break }
var s = strings.Split(l, ": ")
inputs[s[0]] = s[1] == "1"
}
var zd = make(map[string]bool)
for scanner.Scan() {
var s = strings.Split(scanner.Text(), " ")
gates[s[4]] = gate{s[1], s[0], s[2]}
if s[4][0] == 'z' { zd[s[4]] = true }
}
var zv = make(map[string]bool)
for len(zv) < len(zd) {
for g, val := range gates {
var in1, found1 = inputs[val.in1]
var in2, found2 = inputs[val.in2]
if found1 && found2 {
switch (val.t) {
case "AND": inputs[g] = in1 && in2
case "OR": inputs[g] = in1 || in2
case "XOR": inputs[g] = (in1 && !in2) || (!in1 && in2)
}
if g[0] == 'z' { zv[g] = true }
}
}
}
var val int64
for i := 64; i >= 0; i-- {
if inputs["z" + fmt.Sprintf("%02d", i)] {
val = (val * 2) | 1
} else {
val *= 2
}
}
fmt.Println(val)
}