Example #1
0
def test_modular():
    assert solve_congruence(*list(zip([3, 4, 2], [12, 35, 17]))) == (1719, 7140)
    assert solve_congruence(*list(zip([3, 4, 2], [12, 6, 17]))) is None
    assert solve_congruence(*list(zip([3, 4, 2], [13, 7, 17]))) == (172, 1547)
    assert solve_congruence(*list(zip([-10, -3, -15], [13, 7, 17]))) == (172, 1547)
    assert solve_congruence(*list(zip([-10, -3, 1, -15], [13, 7, 7, 17]))) is None
    assert solve_congruence(*list(zip([-10, -5, 2, -15], [13, 7, 7, 17]))) == (835, 1547)
    assert solve_congruence(*list(zip([-10, -5, 2, -15], [13, 7, 14, 17]))) == (2382, 3094)
    assert solve_congruence(*list(zip([-10, 2, 2, -15], [13, 7, 14, 17]))) == (2382, 3094)
    assert solve_congruence(*list(zip((1, 1, 2), (3, 2, 4)))) is None
    raises(ValueError, lambda: solve_congruence(*list(zip([3, 4, 2], [12.1, 35, 17]))))
Example #2
0
def tool_solution(bus_list):

    from sympy.ntheory.modular import solve_congruence

    bus_list = [int(bus) for bus in bus_list.replace("x", "0").split(",")]
    check_list = [(-offset, value) for offset, value in enumerate(bus_list)
                  if value]

    solution = solve_congruence(*check_list)
    return solution[0]
Example #3
0
def main():
    with open('input.txt') as f:
        ls = [line.strip() for line in f.readlines()]

    print(ls)
    earliest = int(ls[0])
    bus_times = [(-i, int(x)) for i, x in enumerate(ls[1].split(','))
                 if x != 'x']
    _, busses = zip(*bus_times)

    print(bus_times)

    # Part one
    print(
        next((time - earliest) * bus for time in count(earliest)
             for bus in busses if time % bus == 0))

    # Part two
    print(solve_congruence(*bus_times)[0])
Example #4
0
def ans2(bus_times):
    return (solve_congruence(*bus_times)[0])
Example #5
0
def main():
    Schedule = ReadInFile()
    ActiveBuses = DetermineActiveBusesSchedule(Schedule)
    TimeStamp = solve_congruence(*ActiveBuses)[0]
    print("Part 2: ", TimeStamp)
Example #6
0
    offset_dict = {}
    while True:
        group = [
            bus for offset, bus in nums.items() if (start + offset) % bus == 0
        ]
        if group:
            group.sort()
            group = tuple(group)
            if group in offset_dict:
                diff = start - offset_dict[group]
                if len(group) > old_len:
                    add_offset = diff
                    old_len = len(group)
            offset_dict[group] = start

        if len(group) == len(nums):
            return start % lcm
        start += add_offset


first = min([(x, get_wait(n, x)) for x in nums.values()], key=lambda x: x[1])
print(f'Part 1: {first[0] * first[1]}')

start = find_start(nums)
print(f'Part 2: {start}')

# part 2 alternative method
# import solve_congruence from sympy lul
x = solve_congruence(*((x[1] - x[0], x[1]) for x in nums.items()))
print(x[0])
Example #7
0
1068792    .       .       .       .       .
1068793    .       .       .       .       .
1068794    .       .       .       .       .
1068795    D       D       .       .       .
1068796    .       .       .       .       .
1068797    .       .       .       .       .
In the above example, bus ID 7 departs at timestamp 1068788 (seven minutes after t). This is fine; the only requirement on that minute is that bus ID 19 departs then, and it does.

Here are some other examples:

The earliest timestamp that matches the list 17,x,13,19 is 3417.
67,7,59,61 first occurs at timestamp 754018.
67,x,7,59,61 first occurs at timestamp 779210.
67,7,x,59,61 first occurs at timestamp 1261476.
1789,37,47,1889 first occurs at timestamp 1202161486.
However, with so many bus IDs in your list, surely the actual earliest timestamp will be larger than 100000000000000!

What is the earliest timestamp such that all of the listed bus IDs depart at offsets matching their positions in the list?
"""
#07:12:31   8873

from sympy.ntheory.modular import solve_congruence

file = open('input.txt', 'r')
data = [line.strip('\n') for line in file]

bus_times = [(-i, int(x)) for i, x in enumerate(data[1].split(','))
             if x != 'x']

print('Earliest timestamp:', solve_congruence(bus_times))
Example #8
0
    bus_routes_tmp = bus_routes_tmp.partition(',')[2]

print('time =',t)
print('Bus Routes = ',bus_routes)
earliest_route = ''

t = t_initial
while earliest_route == '':
    for i in range(0, len(bus_routes)):
        if bus_routes[i] != 'x':
            test_div = divmod(t, int(bus_routes[i]))[1]
            if test_div == 0:
                earliest_route = bus_routes[i]
                earliest_time = t
        i = i + 1
    t = t + 1
wait_time = earliest_time-t_initial
print('Earliest Route is:',earliest_route,'at time',earliest_time)
print('Waiting time:',wait_time)
print('Solution:',wait_time*int(earliest_route))

route_sequence_correct = 0
bus_routes_w_pos = []
for i in range(0, len(bus_routes)):
    if bus_routes[i] != 'x':
        bus_routes_w_pos.append((-1*i,int(bus_routes[i])))
    i = i+1

# Using sympy Number Theory package (https://docs.sympy.org/latest/modules/ntheory.html):
print('Part 2 Solution:', solve_congruence(*bus_routes_w_pos)[0])
Example #9
0
def solve(L, R, v):
    if R <= 11**16:
        pool = mp.Pool(12)
        cnt = 0
        while True:
            params = [(L, R, v) for _ in range(12)]
            for result in pool.imap_unordered(find_sol, params):
                if result != None:
                    return result
            cnt += 1
            print(3600 * cnt)
    elif R <= 11**45:
        lst = [i for i in range(3, 100) if isPrime(i)]
        ass = [0] * len(lst)
        ass[0] = 1
        RG = [i for i in range(2, 2 * v, 2)]
        mark = [0 for i in range(2, 2 * v, 2)]
        CNT = [0 for i in range(2, 2 * v, 2)]
        cur = (0, 1)
        for i in range(len(mark)):
            if RG[i] % 3 == 0:
                mark[i] = 3
                CNT[i] += 1
        for i in range(len(mark)):
            if mark[i] == 0:
                p = 0
                for j in range(len(lst)):
                    if ass[j] != 1 and RG[i] % lst[j] != 0:
                        ass[j] = 1
                        p = lst[j]
                        break
                if p != 0:
                    for j in range(len(mark)):
                        if RG[j] % p == RG[i] % p:
                            mark[j] = p
                            CNT[j] += 1
                    cur = solve_congruence(cur, ((p - RG[i]) % p, p))
            if cur[1] > R // (10**11):
                break
        for i in range(len(mark)):
            if cur[1] > R // (10**11):
                break
            if CNT[i] == 1:
                p = 0
                for j in range(len(lst)):
                    if ass[j] != 1 and RG[i] % lst[j] != 0:
                        ass[j] = 1
                        p = lst[j]
                        break
                if p != 0:
                    for j in range(len(mark)):
                        if RG[j] % p == RG[i] % p:
                            mark[j] = p
                            CNT[j] += 1
                    cur = solve_congruence(cur, ((p - RG[i]) % p, p))
        A, B = cur
        cc = A * inverse(3, B)
        cc %= B
        print(B)
        print(mark)
        print(CNT)
        return find_sol1((L, R, cc, B, v, mark, CNT))
Example #10
0
def CRT(a, m, b, n):
    (u, v) = solve_congruence((a, m), (b, n))
    return u, v