-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-2.go
75 lines (65 loc) · 1.41 KB
/
13-2.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"strconv"
"strings"
)
type BusConstraint struct {
interval int64
when int64
}
func gcd(a, b, rem int64) int64 {
for b != rem {
t := b
b = a % b
a = t
}
return a
}
func lcm(a, b int64) int64 {
denom := gcd(a, b, 0)
return (a / denom) * b
}
func main() {
var fileName string
var version2 bool
flag.StringVar(&fileName, "file", "data/in13.txt", "Input file to use")
flag.BoolVar(&version2, "v2", false, "Use task2 version")
flag.Parse()
content, _ := ioutil.ReadFile(fileName)
rd := bytes.NewReader(content)
var target int64
var line string
fmt.Fscanln(rd, &target)
fmt.Fscanln(rd, &line)
busNames := strings.Split(line, ",")
var definiteBusses []BusConstraint
unknownBusses := 0
for i, busName := range busNames {
busNum, err := strconv.ParseInt(busName, 0, 64)
if err != nil {
unknownBusses++
} else {
definiteBusses = append(definiteBusses, BusConstraint{busNum, int64(i)})
}
}
toVal := definiteBusses[0].interval
toInt := toVal
for i := 1; i < len(definiteBusses); i++ {
targetDiff := definiteBusses[i].when
compInt := definiteBusses[i].interval
for compNum := (toVal / compInt) * compInt; compNum-toVal != targetDiff; {
if compNum-toVal > targetDiff {
toVal += toInt
compNum = (toVal / compInt) * compInt
} else {
compNum += compInt
}
}
toInt = lcm(toInt, compInt)
}
fmt.Println(toVal)
}