Beispiel #1
0

def count_locations(N, SX, SY, Xprefix, Yprefix):
    S = create_points(N, SX, SY, Xprefix, Yprefix)
    # print("len: {} ==> {}".format(S.size(), S))
    if S.size() < 3:
        return 0

    possible_points = Points()
    for p1 in range(len(S.points_list) - 1):
        # print(p1)
        for p2 in range(p1 + 1, len(S.points_list)):
            find_square(S, possible_points, S.points_list[p1],
                        S.points_list[p2])
    return possible_points.size()


def run_line(match):
    N = int(match.group(1))
    SX = int(match.group(2))
    SY = int(match.group(3))
    Xprefix = get_ints(match.group(4))
    Yprefix = get_ints(match.group(5))
    expected = int(match.group(6))
    actual = count_locations(N, SX, SY, Xprefix, Yprefix)
    assert_equals(actual, expected)


test_solution(__file__, '(\\d+), (\\d+), (\\d+), {(.*)}, {(.*)}\\s+(\\d+)',
              run_line)
Beispiel #2
0

def find_all():
    palindromes = []
    for digits in range(1, 15):
        palindromes.extend(find_by_digits(digits))
    return palindromes


def get_min(x):
    global all
    if len(all) == 0:
        all = find_all()
    for p in all:
        if p % x == 0:
            y = p / x
            if y > 1e9:
                return -1
            return p / x
    return -1


def run_line(match):
    x = int(match.group(1))
    expected = int(match.group(2))
    actual = get_min(x)
    assert_equals(actual, expected)


test_solution(__file__, '(\\S+)\\s+(\\S+)', run_line)
Beispiel #3
0

def get_primes(sums_input):
    global sums
    sums = sums_input
    find_primes_numbers()
    find_primes_digits()
    index_primes()

    possible_first = [i for i in range(len(primes_digits))]
    possible_second = [i for i in range(len(primes_digits))]
    possible_third = [i for i in range(len(primes_digits))]
    result = recursive_find(0, possible_first, possible_second, possible_third)
    print(result)
    if result is None:
        return []

    result = [primes_numbers[result[0][0]], primes_numbers[result[1][0]], primes_numbers[result[2][0]]]
    print(result)
    return result


def run_line(match):
    sums = get_ints(match.group(1))
    expected = get_ints(match.group(2))
    actual = get_primes(sums)
    assert_equals(len(actual), len(expected))


test_solution(__file__, '{(.*)}\\s+{(.*)}', run_line)
Beispiel #4
0
        cost = remove_one(lists, x, y, z)
        if cost is None:
            break
        # print(cost)
        total_cost += cost

    result = {
        'min_sum': sum(lists[0]) + sum(lists[1]) + sum(lists[2]),
        'cost': total_cost
    }

    # print(result)
    return result


def run_line(match):
    a = int(match.group(1))
    b = int(match.group(2))
    c = int(match.group(3))
    x = int(match.group(4))
    y = int(match.group(5))
    z = int(match.group(6))
    min_sum = int(match.group(7))
    cost = int(match.group(8))
    actual = do_delete(a, b, c, x, y, z)
    assert_equals(actual['min_sum'], min_sum)
    assert_equals(actual['cost'], cost)


test_solution(__file__, '(\\d+), (\\d+), (\\d+), (\\d+), (\\d+), (\\d+)\\s+{(\\d+), (\\d+)}', run_line)
Beispiel #5
0
                        updated = lamp.integrate(self.solvers[j])
                        if updated:
                            any_updated = True
                            print("update lamp {} related {}".format(
                                lamp.lamp, lamp.related))
                            if lamp.is_finite():
                                self.guess_alternatives -= 1
        return any_updated

    def learn_from_experiments(self):
        for lamp in range(self.n):
            lamp_solver = LampSolver(self, lamp)
            lamp_solver.check_lamp()
            self.solvers.append(lamp_solver)


def the_min(switches, lamps):
    s = Solver(switches, lamps)
    return s.solve()


def run_line(match):
    switches = get_strings(match.group(1))
    lamps = get_strings(match.group(2))
    expected = int(match.group(3))
    actual = the_min(switches, lamps)
    assert_equals(actual, expected)


test_solution(__file__, '{(.*)}, {(.*)}\\s+(\\S+)', run_line)
Beispiel #6
0
        print(self)

    def __repr__(self):
        return "{},{},{} = {:,}".format(
            self.product1, self.product2,
            "Parallel" if self.parallel else "Serial", self.value)


def construct(nanoOhms):
    print("{:,}".format(nanoOhms))
    products.append(Product(-1, -1, False))
    products.append(Product(-1, -1, False))

    while (products[-1].value != nanoOhms):
        if len(products) > 1000:
            raise Exception("Failed!")
        if products[-1].value < nanoOhms:
            last = len(products) - 1
            products.append(Product(last, last - 1, False))
        else:
            last = len(products) - 1
            products.append(Product(last, last - 1, True))


def run_line(match):
    nanoOhms = int(match.group(1))
    construct(nanoOhms)


test_solution(__file__, '(\\d+)', run_line)
Beispiel #7
0

def find_number_of_swaps(p, a0, x, y, n, k):
    s = generate(p, a0, n, x, y)

    score = 0
    for i in range(k):
        part = ""
        while i < len(s):
            part += s[i]
            i += k
        score += replace_count(part)
    return score


def run_line(match):
    p = match.group(1)
    a0 = int(match.group(2))
    x = int(match.group(3))
    y = int(match.group(4))
    n = int(match.group(5))
    k = int(match.group(6))
    expected = int(match.group(7))
    actual = find_number_of_swaps(p, a0, x, y, n, k)
    assert_equals(actual, expected)


test_solution(__file__,
              '"(.*)", (\\d+), (\\d+), (\\d+), (\\d+), (\\d+)\\s+(\\d+)',
              run_line)
Beispiel #8
0
from python.test_utils import test_solution, assert_equals, get_ints


def possible(candy, bag_size, bags):
    add = list(map(lambda x: x // bags, candy))
    return sum(add) >= bag_size


def make_bags(candy, bag_size):
    low = 0
    high = pow(10, 19)
    while low + 1 < high:
        mid = (low + high) // 2
        if possible(candy, bag_size, mid):
            low = mid
        else:
            high = mid
    return low


def run_line(match):
    candy = get_ints(match.group(1))
    bag_size = int(match.group(2))
    expected = int(match.group(3))
    actual = make_bags(candy, bag_size)
    assert_equals(actual, expected)


test_solution(__file__, '{(.*)}, (\\d+)\\s+(\\d+)', run_line)
Beispiel #9
0
    c['z'] = reduce_multiple(c['z'])
    print("after reduce multiple z: " + periodic_string(c))

    truncate_zeros(c)
    print("after zeros truncate: " + periodic_string(c))

    pattern_reduce(c)
    print("after pattern reduce: " + periodic_string(c))

    return periodic_string(c)


def periodic_string(c):
    y = c['y']
    if y == 0:
        y = ''

    return '{}.{}({})'.format(c['x'], y, c['z'])


def run_line(match):
    a = match.group(1)
    b = match.group(2)
    expected = match.group(3)
    actual = add(a, b)
    assert_equals(actual, expected)


test_solution(__file__, '"(.*)", "(.*)"\\s+"(.*)"', run_line)
Beispiel #10
0
    a = match.group(1)
    b = match.group(2)
    c = match.group(3)

    valid = []
    for i in range(10):
        a_num = replace_digit(a, i)
        b_num = replace_digit(b, i)
        c_num = replace_digit(c, i)

        if a_num is None or b_num is None or c_num is None:
            continue

        if a_num * b_num == c_num:
            valid.append(i)

    if len(valid) != 1:
        return -1

    return valid[0]


def run_line(match):
    equation = match.group(1)
    expected = int(match.group(2))
    actual = find_digit(equation)
    assert_equals(actual, expected)


test_solution(__file__, '"(.*)"\\s+(-?\\d+)', run_line)
Beispiel #11
0
    else:
        indexes[distance] = [position]


def find_value(default_value, query):
    if query == 0:
        return 0
    indexes = {}
    last = 0
    position = 0
    while query not in indexes:
        position += 1
        distance = find_distance(last, indexes, default_value)
        last = distance
        add_location(distance, indexes, position)
        if position > 10000000:
            return -1

    return indexes[query][0]


def run_line(match):
    default_value = int(match.group(1))
    query = int(match.group(2))
    expected = int(match.group(3))
    actual = find_value(default_value, query)
    assert_equals(actual, expected)


test_solution(__file__, '(\\d+), (\\d+)\\s+(-?\\d+)', run_line)
Beispiel #12
0
    return s, e


def maximize(n, e):
    p = generate_prime_alphabet()

    final_len = n - e
    if n > 17000:
        s = ['z'] * final_len
    else:
        s = p[0:n]

        start = 0
        while e > 0:
            s, e = lexical_delete(start, s, e)
            start += 1

    return ''.join(s)


def run_line(match):
    n = int(match.group(1))
    e = int(match.group(2))
    expected = match.group(3)
    actual = maximize(n, e)
    assert_equals(actual, expected)


test_solution(__file__, '(\\d+), (\\d+)\\s+\"(.*)\"', run_line)
Beispiel #13
0
        if blocks[try_height] > 0:
            blocks[try_height] -= 1
            if recurse_possible(0, land_width - 1, blocks, 0, current_height + 1):
                return True
            blocks[try_height] += 1

    else:
        return True


def is_it_even_possible(water_width, land_width, block_height, depth):
    blocks = [0] * 202
    for h in block_height:
        blocks[h] += 1
    if recurse_possible(water_width, land_width, blocks, depth, 0):
        return "POSSIBLE"
    return "IMPOSSIBLE"


def run_line(match):
    water_width = int(match.group(1))
    land_width = int(match.group(2))
    block_height = get_ints(match.group(3))
    depth = int(match.group(4))
    expected = match.group(5)
    actual = is_it_even_possible(water_width, land_width, block_height, depth)
    assert_equals(actual, expected)


test_solution(__file__, '(\\d+), (\\d+), {(.*)}, (\\d+)\\s+"(.*)"', run_line)