-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (138 loc) · 3.12 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bufio"
"fmt"
"io"
"math/big"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "13", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type equation struct {
a, n *big.Int
}
// https://en.wikipedia.org/wiki/Chinese_remainder_theorem#Existence_(constructive_proof)
func findX2(eq1, eq2 equation) *big.Int {
//euclid := extendedEuclid(eq1.n, eq2.n)
m1 := big.NewInt(0)
m2 := big.NewInt(0)
big.NewInt(0).GCD(m1, m2, eq1.n, eq2.n)
m2.Mul(m2, eq1.a)
m2.Mul(m2, eq2.n)
m1.Mul(m1, eq2.a)
m1.Mul(m1, eq1.n)
return m2.Add(m2, m1)
//m1, m2 := euclid.x, euclid.y
//return eq1.a * m2 * eq2.n + eq2.a*m1 * eq1.n
}
func findX(eqs []equation) (*big.Int, *big.Int) {
currentN := big.NewInt(0).Mul(eqs[0].n, eqs[1].n)
sol := findX2(eqs[0], eqs[1])
for i := 2; i < len(eqs); i++ {
sol = findX2(equation{
a: sol,
n: currentN,
}, eqs[i])
currentN.Mul(currentN, eqs[i].n)
}
return sol, currentN
}
func toSmallestPositive(sol, currentN *big.Int) *big.Int {
if sol.Cmp(big.NewInt(0)) == -1 {
toSub := big.NewInt(0).Div(sol, currentN)
toSub.Mul(toSub, big.NewInt(-1))
//toSub.Add(toSub, big.NewInt(1))
fmt.Println(sol, "+", toSub, "*", currentN, " = ")
sol.Add(sol, toSub.Mul(toSub, currentN))
} else {
toSub := big.NewInt(0).Div(sol, currentN)
//toSub.Add(toSub, big.NewInt(1))
fmt.Println(sol, "-", toSub, "*", currentN, " = ")
sol.Sub(sol, toSub.Mul(toSub, currentN))
}
//if currentN - sol > 0 && currentN - sol < sol {
// return currentN - sol
//}
return sol
}
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
busses := []int{}
scanner.Scan()
scanner.Scan()
second := scanner.Text()
for _, item := range strings.Split(second, ",") {
if item == "x" {
busses = append(busses, 0)
continue
}
val, err := strconv.Atoi(item)
if err != nil {
panic(err)
}
busses = append(busses, val)
}
//num := busses[0]
//for _, val := range busses {
// if val == 0 {
// continue
// }
// num *= val
//}
/*
x=(a1 mod m1); x=(a2 mod m2);
// most obvious
0 = ? mod 7
0 = ? + 1 mod 13
0 = ? + 4 mod 59
0 = ? + 6 mod 31
0 = ? + 7 mod 19
// move ? to the left to be solved for
-? = 0 mod 7
-? = 1 mod 13
-? = 4 mod 59
-? = 6 mod 31
-? = 7 mod 19
// make positive
? = 0 mod 7
? = -1 mod 13
? = -4 mod 59
? = -6 mod 31
? = -7 mod 19
*/
var entries []equation
for i, bus := range busses {
if bus == 0 {
continue
}
entries = append(entries, equation{
a: big.NewInt(int64(-i)),
n: big.NewInt(int64(bus)),
})
}
negSol, n := findX(entries)
//fmt.Println(negSol)
actualSol := toSmallestPositive(negSol, n)
fmt.Printf("%d %+v\n", actualSol, busses)
if scanner.Err() != nil {
panic(scanner.Err())
}
}
func main() {
//solve(strings.NewReader("0\n3,x,x,4,5"))
solve(strings.NewReader("0\n7,13,x,x,59,x,31,19"))
solve(strings.NewReader("0\n17,x,13,19"))
solve(strings.NewReader("0\n67,7,59,61"))
solve(strings.NewReader("0\n67,x,7,59,61"))
solve(strings.NewReader("0\n67,7,x,59,61"))
solve(strings.NewReader("0\n1789,37,47,1889"))
solve(input())
}