-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (68 loc) · 1.7 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"math/big"
"time"
"github.com/ErikThorsell/advent-of-code-go/util"
"github.com/deanveloper/modmath/v1/bigmod"
)
func part1(earliestTime int, busTable []string) int {
validBusTable := getOperationalBuses(busTable)
ct := earliestTime
for {
for _, busID := range validBusTable {
if util.RemIsZero(ct, busID) {
return util.Abs(ct-earliestTime) * busID
}
}
ct++
}
}
func getOperationalBuses(bustable []string) []int {
operationalBuses := []int{}
for _, b := range bustable {
if util.IsInt(b) {
operationalBuses = append(operationalBuses, util.ToInt(b))
}
}
return operationalBuses
}
func part2(table []string) int64 {
constraints := findDepartureConstrains(table)
var opBusses []bigmod.CrtEntry
for b, t := range constraints {
opBusses = append(opBusses, bigmod.CrtEntry{A: big.NewInt(int64(b - t)), N: big.NewInt(int64(b))})
}
return bigmod.SolveCrtMany(opBusses).Int64()
}
func findDepartureConstrains(busTable []string) map[int]int {
constraints := make(map[int]int)
t := 0
for _, b := range busTable {
if util.IsInt(b) {
constraints[util.ToInt(b)] = t
}
t++
}
return constraints
}
func main() {
rawInput := util.FetchInputForDay("2020", "13")
earliestTime, busTable := util.ParseBusTableInput(rawInput)
fmt.Println("Done parsing input.")
fmt.Println()
// PART 1
s := time.Now()
ans1 := part1(earliestTime, busTable)
t := time.Now()
e := t.Sub(s)
fmt.Println("Answer for first question: ", ans1)
fmt.Println("First answer retrieved in: ", e)
fmt.Println()
s = time.Now()
ans2 := part2(busTable)
t = time.Now()
e = t.Sub(s)
fmt.Println("Answer for second question: ", ans2)
fmt.Println("Second answer retrieved in: ", e)
}