-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.go
61 lines (49 loc) · 918 Bytes
/
Day13.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
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
time, _ := strconv.Atoi(scanner.Text())
scanner.Scan()
idsString := strings.Split(scanner.Text(), ",")
min := math.MaxInt32
minId := -1
for _, idString := range idsString {
if idString == "x" {
continue
}
j, _ := strconv.Atoi(idString)
wait := j*(time/j+1) - time
if wait < min {
min = wait
minId = j
}
}
first := min * minId
step := int64(-1)
second := int64(0)
for index, idString := range idsString {
if idString == "x" {
continue
}
bus, _ := strconv.ParseInt(idString, 10, 64)
if step == -1 {
step = bus
continue
}
int64index := int64(index)
remainder := (bus*int64index - int64index) % bus
for second%bus != remainder {
second += step
}
step *= bus
}
fmt.Printf("%d %d\n", first, second)
}