2020/13.py
def part2_3():
    """
    Third attempt. This one I tried to implement the Chinese Remainder Theorem by hand.
    I failed.
    """
    print(lines[1].split(","))
    busses = []
    gaps = [0]
    for b in lines[1].split(","):
        if b == "x":
            gaps[-1] += 1
            continue
        else:
            gaps.append(1)
            busses.append(int(b))

    N = reduce(lambda a, b: a * b, busses, 1)
    print(N)
    x = 0
    for i, b in enumerate(busses):
        y_i = N // b
        # (1/y_i) % sum(gaps[:i+1])
        z_i = extended_gcd(gaps[i - 1], gaps[i])
        print(z_i)
        x += sum(gaps[: i + 1]) * y_i * z_i
        print(x)
    return x

    Third attempt. This one I tried to implement the Chinese Remainder Theorem by hand.
    I failed.
    
Third attempt. This one I tried to implement the Chinese Remainder Theorem by hand.
I failed.
Third attempt . This one I tried to implement the Chinese Remainder Theorem by hand . I failed .
def part2_3():
    print(lines[1].split(","))
    busses = []
    gaps = [0]
    for b in lines[1].split(","):
        if b == "x":
            gaps[-1] += 1
            continue
        else:
            gaps.append(1)
            busses.append(int(b))
    N = reduce(lambda a, b: a * b, busses, 1)
    print(N)
    x = 0
    for i, b in enumerate(busses):
        y_i = N // b
        z_i = extended_gcd(gaps[i - 1], gaps[i])
        print(z_i)
        x += sum(gaps[: i + 1]) * y_i * z_i
        print(x)
    return x
def part2_3 ( ) : print ( lines [ 1 ] . split ( "," ) ) busses = [ ] gaps = [ 0 ] for b in lines [ 1 ] . split ( "," ) : if b == "x" : gaps [ - 1 ] += 1 continue else : gaps . append ( 1 ) busses . append ( int ( b ) ) N = reduce ( lambda a , b : a * b , busses , 1 ) print ( N ) x = 0 for i , b in enumerate ( busses ) : y_i = N // b z_i = extended_gcd ( gaps [ i - 1 ] , gaps [ i ] ) print ( z_i ) x += sum ( gaps [ : i + 1 ] ) * y_i * z_i print ( x ) return x
Third attempt.
Third attempt .
"""
    Third attempt. This one I tried to implement the Chinese Remainder Theorem by hand.
    I failed.
    """ # (1/y_i) % sum(gaps[:i+1])