-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (38 loc) · 767 Bytes
/
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
package main
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
)
func main() {
// read the puzzle input
b, err := ioutil.ReadFile("input.txt")
if err != nil {
log.Fatal("Cannot open the input file.")
}
str := string(b)
// extract the data
data := strings.Split(str, "\n")
first, _ := strconv.Atoi(data[0])
buses := []int{}
for _, v := range strings.Split(data[1], ",") {
if v != "x" {
cv, _ := strconv.Atoi(v)
buses = append(buses, cv)
}
}
// find the airport bus with minimum waiting time
minWaitTime := buses[0]
minBusID := buses[0]
for _, b := range buses {
wtime := b - first%b
if wtime < minWaitTime {
minWaitTime = wtime
minBusID = b
}
}
// print the results
fmt.Println("Part 1:", minWaitTime*minBusID)
}