-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (44 loc) · 1.04 KB
/
main.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
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
estimate, schedule := parseInput(os.Stdin)
busID, waitTime := earliestBus(estimate, schedule)
fmt.Printf("Part 1: %d\n", busID*waitTime)
fmt.Printf("Part 2: %d\n", earliestTimestampForSubsequentDepartures(schedule))
}
func parseInput(r io.Reader) (estimate int, schedule []string) {
var scheduleStr string
fmt.Fscanln(r, &estimate)
fmt.Fscanln(r, &scheduleStr)
return estimate, strings.Split(scheduleStr, ",")
}
func earliestBus(notBefore int, schedule []string) (id int, waitTime int) {
waitTime = notBefore
for _, bStr := range schedule {
if b, err := strconv.Atoi(bStr); err == nil {
wt := b*(notBefore/b+1) - notBefore
if wt < waitTime {
id, waitTime = b, wt
}
}
}
return id, waitTime
}
func earliestTimestampForSubsequentDepartures(schedule []string) int {
t, step := 0, 1
for offset, idStr := range schedule {
if id, err := strconv.Atoi(idStr); err == nil {
for (t+offset)%id != 0 {
t += step
}
step *= id
}
}
return t
}