Пример #1
0
            for rule in rules.values():
                if rule['a'][0] <= val <= rule['a'][1] or \
                        rule['b'][0] <= val <= rule['b'][1]:
                    valid = True
                    break
            if not valid:
                tser += val
    toc = time()
    print(tser)
    print(f'finished in {1000 * (toc - tic):.2f}ms') # 3.75ms
    """

    # Part 2
    tic = time()
    rules = dict()
    for line in utils.read_str_sequence(file='rules'):
        rule, ranges = line.split(': ')
        a, b = ranges.split(' or ')
        rules[rule] = [int(val) for val in a.split('-')
                       ] + [int(val) for val in b.split('-')]
    sequence = utils.read_str_sequence()
    my_ticket = [
        223, 139, 211, 131, 113, 197, 151, 193, 127, 53, 89, 167, 227, 79, 163,
        199, 191, 83, 137, 149
    ]
    sequence.append(','.join([str(x) for x in my_ticket]))
    valid_tickets = []
    for ticket in sequence:
        vals = [int(val) for val in ticket.split(',')]
        ok = [False] * len(vals)
        for i, x in enumerate(vals):
Пример #2
0
        gcd, r, s = egcd(b % a, a)
        return (gcd, s - (b // a) * r, r)


if __name__ == '__main__':

    # Part 1
    """
    tic = time()
    sequence = utils.read_str_sequence()
    earliest = int(sequence[0])
    buses = [int(line) for line in sequence[1].split(',') if line != 'x']
    departures = [line - (earliest % line) for line in buses]
    print(buses[departures.index(min(departures))] * min(departures))
    toc = time()
    print(f'finished in {1000 * (toc - tic):.2f}ms') # 0.11ms
    """

    # Part 2
    tic = time()
    sequence = utils.read_str_sequence()
    buses = [(int(line), -i) for i, line in enumerate(sequence[1].split(','))
             if line != 'x']
    LCM = prod([bus[0] for bus in buses])
    N = [LCM / bus[0] for bus in buses]
    M = [egcd(N[i], buses[i][0])[1] for i in range(len(buses))]
    t = sum([int(buses[i][1] * M[i] * N[i]) for i in range(len(buses))]) % LCM
    toc = time()
    print(t)
    print(f'finished in {1000 * (toc - tic):.2f}ms')  # 0.13ms