예제 #1
0
    def feed_prng(self, previous_hash, nonce):
        hasher = hashlib.sha256()
        hasher.update("{0}{1}".format(previous_hash, nonce).encode("ascii"))
        seed_hash = hasher.hexdigest()

        seed = seed_from_hash(seed_hash)
        self.mt = MT64(seed)
    def generate_solution(self, previous_hash, nonce):
        solution_string = ""

        grid = Grid(self.parameters["grid_size"])
        seed_hash = self.generate_seed_hash(previous_hash, nonce)
        # seed is the last solution hash suffix, else it's 0
        prng = coinslib.MT64(coinslib.seed_from_hash(seed_hash))

        for i in range(self.parameters["grid_size"]):
            # placing extremity walls
            grid.walls.append((i, 0))
            grid.walls.append((i, self.parameters["grid_size"] - 1))

            if i > 0 and i < (self.parameters["grid_size"] - 1):
                grid.walls.append((0, i))
                grid.walls.append((self.parameters["grid_size"] - 1, i))

        start_pos = (prng.extract_number() % self.parameters["grid_size"],
                     prng.extract_number() % self.parameters["grid_size"])
        while start_pos in grid.walls:
            start_pos = (prng.extract_number() % self.parameters["grid_size"],
                         prng.extract_number() % self.parameters["grid_size"])

        end_pos = (prng.extract_number() % self.parameters["grid_size"],
                   prng.extract_number() % self.parameters["grid_size"])
        while end_pos in grid.walls or start_pos == end_pos:
            end_pos = (prng.extract_number() % self.parameters["grid_size"],
                       prng.extract_number() % self.parameters["grid_size"])

        # placing walls
        for i in range(self.parameters["nb_blockers"]):
            # wall pos (row, col)
            block_pos = (prng.extract_number() % self.parameters["grid_size"],
                         prng.extract_number() % self.parameters["grid_size"])
            if block_pos != start_pos and block_pos != end_pos and block_pos not in grid.walls:
                grid.walls.append(block_pos)

        path = []
        came_from, cost_so_far = dijkstra_search(grid, start_pos, end_pos)
        path = reconstruct_path(came_from, start_pos, end_pos)

        for coord in path:
            solution_string += "{0}{1}".format(coord[0], coord[1])

        if self.debug_output:
            self.save_grid(grid, start_pos, end_pos, nonce, path)

        hash = self.generate_hash(solution_string)
        return Challenge(self.problem_name, nonce, solution_string, hash,
                         self.parameters)
    def generate(self, previous_hash):
        # generate a nonce
        nonce = random.randint(1000,
                               10000)  # put that into a configuration file
        print("Generating {0} problem nonce = {1}".format(
            self.problem_name, nonce))

        grid = Grid(self.parameters["grid_size"])
        seed_hash = self.generate_seed_hash(previous_hash, nonce)
        # seed is the last solution hash suffix, else it's 0
        prng = coinslib.MT64(coinslib.seed_from_hash(seed_hash))

        start_pos = (prng.extract_number() % self.parameters["grid_size"],
                     prng.extract_number() % self.parameters["grid_size"])
        end_pos = (prng.extract_number() % self.parameters["grid_size"],
                   prng.extract_number() % self.parameters["grid_size"])

        # placing walls
        for i in range(self.parameters["nb_blockers"]):
            # wall pos (row, col)
            block_pos = (prng.extract_number() % self.parameters["grid_size"],
                         prng.extract_number() % self.parameters["grid_size"])
            if block_pos != start_pos and block_pos != end_pos:
                grid.walls.append(block_pos)

        # starting and ending position
        solution_string = ""

        try:
            came_from, cost_so_far = dijkstra_search(grid, start_pos, end_pos)
            path = reconstruct_path(came_from, start_pos, end_pos)
            # print(path)
            for coord in path:
                solution_string += "{0}{1}".format(coord[0], coord[1])
        except Exception as e:
            print("Shortest Path Challenge error: {0}".format(e))
            # No solution exists, so solution string should be empty

        hash = self.generate_hash(solution_string)
        return Challenge(self.problem_name, nonce, solution_string, hash,
                         self.parameters)
예제 #4
0
    def generate_solution(self, previous_hash, nonce):
        seed_hash = self.generate_seed_hash(previous_hash, nonce)
        prng = coinslib.MT64(coinslib.seed_from_hash(seed_hash))

        element_list = []

        # generate n elements
        for i in range(self.parameters["nb_elements"]):
            element_list.append(prng.extract_number())

        element_list.sort()

        solution_string = ""

        for i in element_list:
            solution_string += "{0}".format(i)

        hash = self.generate_hash(solution_string)

        return Challenge(self.problem_name, nonce, solution_string, hash,
                         self.parameters)