File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ split_data = True
5+ completed = False
6+ raw_data = None # Not To be touched
7+
8+ def part1 (data ):
9+ ts , ids = data
10+ ts = int (ts )
11+
12+ earliest = float ('inf' )
13+ early_id = None
14+ for id in ids .split (',' ):
15+ if id == 'x' : continue
16+ id = int (id )
17+ d_ts = ((ts // id ) + 1 ) * id
18+ if d_ts < earliest :
19+ earliest = d_ts
20+ early_id = id
21+
22+ return early_id * (earliest - ts )
23+
24+
25+ def extendedGCD (a , b ):
26+ if b == 0 :
27+ return a , 1 , 0
28+
29+ g , x1 , y1 = extendedGCD (b , a % b )
30+
31+ x = y1
32+ y = x1 - (a // b ) * y1
33+
34+ return g , x , y
35+
36+ def modInverse (a , m ):
37+ g , x , y = extendedGCD (a , m )
38+ if g != 1 :
39+ raise ValueError
40+
41+ return (x % m + m ) % m
42+ def crt (remainders , moduli ):
43+ prod = 1
44+ for mod in moduli :
45+ prod *= mod
46+
47+ ans = 0
48+ for rem , mod in zip (remainders , moduli ):
49+ pp = prod // mod
50+ inv = modInverse (pp , mod )
51+ ans += rem * inv * pp
52+ return ans % prod
53+
54+ def part2 (data ):
55+ # This part can be easily solved using the Chiense Remainder Theorem.
56+ # Though I wrote the code, I don't know how this theorem or algo works
57+
58+ rem = []
59+ mod = []
60+ _ , ids = data
61+ for i , a in enumerate (ids .split (',' )):
62+ if a == 'x' : continue
63+ mod .append (int (a ))
64+ rem .append (- i % int (a ))
65+
66+ return crt (rem , mod )
You can’t perform that action at this time.
0 commit comments