-
Notifications
You must be signed in to change notification settings - Fork 0
/
p79.go
103 lines (83 loc) · 1.41 KB
/
p79.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
package main
import (
"bufio"
"fmt"
"os"
"time"
)
func IsValid(x, y []rune) bool {
indexX, indexY := 0, 0
for indexX < len(x) && indexY < len(y) {
if x[indexX] == y[indexY] {
indexY++
}
indexX++
}
return indexY == len(y)
}
func main() {
start := time.Now()
f, _ := os.Open("./data/d79")
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
var code [][]rune
for i := 0; i < 50; i++ {
scanner.Scan()
code = append(code, []rune(scanner.Text()))
}
var link [10][10]bool
for i := 0; i < 50; i++ {
link[code[i][0]-'0'][code[i][1]-'0'] = true
link[code[i][1]-'0'][code[i][2]-'0'] = true
}
var table [10]bool
var inDeg, outDeg [10]int
for i := 0; i < 10; i++ {
table[i] = false
inDeg[i] = 0
outDeg[i] = 0
}
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
if link[i][j] {
table[i] = true
table[j] = true
outDeg[i]++
inDeg[j]++
}
}
}
left := 0
for _, x := range table {
if x {
left++
}
}
for left > 0 {
curr := 0
for curr < 10 {
if table[curr] && inDeg[curr] == 0 {
fmt.Print(curr)
table[curr] = false
left--
for i := 0; i < 10; i++ {
if link[i][curr] {
outDeg[i]--
inDeg[curr]--
}
if link[curr][i] {
outDeg[curr]--
inDeg[i]--
}
}
break
} else {
curr++
}
}
}
fmt.Println()
f.Close()
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}