Beispiel #1
0
# O(n) creation O(log n) pop and push
def lowest_risk_faster(Graph, expansion):
    rows, cols = map(lambda x: x * expansion, Graph.shape)
    p_queue = [(0, 0, 0)]
    heapq.heapify(p_queue)
    dist = {}
    visited = set()
    while p_queue:
        cost, r, c = heapq.heappop(p_queue)
        u = (r, c)
        if u == (rows - 1, cols - 1):
            return cost
        if u in visited:
            continue
        visited.add(u)
        dist[u] = cost
        for up, down in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            i = u[0] + up
            j = u[1] + down
            if (0 <= i <= rows - 1) and (0 <= j <= cols - 1):
                heapq.heappush(p_queue,
                               (dist[u] + find_cost(i, j, Graph), i, j))
    return None


if __name__ == '__main__':
    Graph = parseInput("input.txt")
    '''for j in range(0, 50):
        print(find_cost(0, j, Graph), end=" ")'''
    print(lowest_risk_faster(Graph, 5))
Beispiel #2
0
                break
            start += 5
        start += 5
        return start, int(l_value, 2)

    else:
        start += 6
        l_type_id = packet[start]
        values = []
        if l_type_id == "0":
            len_sub_pkts = int(packet[start + 1:start + 16], 2)
            start += 16
            end = start + len_sub_pkts
            while start < end:
                start, res = parse_subpackets(packet, start)
                values.append(res)

        else:
            num_sub_pkts = int(packet[start + 1:start + 12], 2)
            start += 12
            for _ in range(num_sub_pkts):
                start, res = parse_subpackets(packet, start)
                values.append(res)

    return start, operations[t_Id](values)


if __name__ == '__main__':
    packet = parseInput("input.txt")
    print(parse_subpackets(packet, start=0)[1])
Beispiel #3
0
def reflect(coord, instruction):
    k = instruction[1]
    if instruction[0] == "y":
        return (coord[0], 2 * k - coord[1])
    else:
        return (2 * k - coord[0], coord[1])


def valid_point(coord, instruction):
    if instruction[0] == "y":
        return coord[1] > instruction[1]
    else:
        return coord[0] > instruction[1]


def fold_paper_once(paper, instructions):
    new_paper = set()
    for coord in paper:
        if valid_point(coord, instructions[0]):
            new_paper.add(reflect(coord, instructions[0]))
        else:
            new_paper.add(coord)
    return len(new_paper)


if __name__ == '__main__':
    paper, instructions = parseInput("input.txt")
    #print(reflect((6, 0), ("x", 5)))
    print(fold_paper_once(paper, instructions))
Beispiel #4
0
            max_height = y_pos

        # check if within target area
        if x0 <= x_pos <= x1 and y0 <= y_pos <= y1:
            return max_height

        # check if out of boundaries
        if x_pos > x1 or y_pos < y0:
            return 0

        # update vels
        xvel -= 1 if xvel > 0 else (-1 if xvel < 0 else 0)
        yvel -= 1


def max_height(target_area):
    x_max = target_area[0][1]
    y_max = -target_area[1][0]
    max_height = -float("inf")
    for xvel in range(x_max + 1):
        for yvel in range(-y_max, y_max):
            height = launch_probe([xvel, yvel], target_area)
            if height > max_height:
                max_height = height
    return max_height


if __name__ == '__main__':
    target_area = parseInput("input.txt")
    print(max_height(target_area))
Beispiel #5
0
from parse_input import parseInput
from starcubing import starCubing
from generate_tree import StarReduction
import time
from pptree import *
import sys

start_time = time.time()

count_table, input_table = parseInput(sys.argv[0])

starReduce = StarReduction(sys.argv[1], input_table, count_table)

starReduce.createStarTable()
tree = starReduce.createStarTree()

# print(tree.value
# print_tree(tree)
# print(starReduce.compressed_table)

tree1 = starCubing(tree, tree, sys.argv[1])

# print(tree.value)

print(time.time() - start_time)

# print_tree(tree)
Beispiel #6
0
def step(pairs, rules):
    new_pairs = defaultdict(int)
    for k in pairs:
        new_pairs[k[0] + rules[k]] += pairs[k]
        new_pairs[rules[k] + k[1]] += pairs[k]
    return new_pairs


def pair_insertions(template, rules, steps):
    # generate pairs
    pairs = defaultdict(int)
    for i in range(len(template) - 1):
        pairs[template[i:i + 2]] += 1

    # polymerize stepwise updating the number of pairs
    for _ in range(steps):
        pairs = step(pairs, rules)

    # count elements
    elem_counts = defaultdict(int)
    elem_counts[template[-1]] = 1
    for k, v in pairs.items():
        elem_counts[k[0]] += v
    # return sum of max - min count
    return max(elem_counts.values()) - min(elem_counts.values())


if __name__ == '__main__':
    template, rules = parseInput("input.txt")
    print(pair_insertions(template, rules, 10))