-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path161.go
65 lines (59 loc) · 1.11 KB
/
161.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 161 - Traffic Lights
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func solve(cycles []int) int {
first := true
here:
for i := 1; i <= 18000; i++ {
for _, cycle := range cycles {
if i%(2*cycle) >= cycle-5 {
if first {
first = false
}
continue here
}
}
if !first {
return i
}
}
return 0
}
func main() {
in, _ := os.Open("161.in")
defer in.Close()
out, _ := os.Create("161.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line string
var cycle int
var cycles []int
for s.Scan() {
if line = s.Text(); line == "0 0 0" {
break
}
for r := strings.NewReader(line); ; {
if _, err := fmt.Fscanf(r, "%d", &cycle); err != nil || cycle == 0 {
break
}
cycles = append(cycles, cycle)
}
if cycle == 0 {
if seconds := solve(cycles); seconds == 0 {
fmt.Fprintln(out, "Signals fail to synchronise in 5 hours")
} else {
hours := seconds / 3600
minutes := seconds % 3600 / 60
seconds = seconds % 3600 % 60
fmt.Fprintf(out, "%02d:%02d:%02d\n", hours, minutes, seconds)
}
cycles = nil
}
}
}