Created
October 7, 2017 12:10
-
-
Save hristo-vrigazov/97d19f3c19a85473bcf97b9ad4ef9d1f to your computer and use it in GitHub Desktop.
FMI homework with frogs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
signs_map = { | |
'>': 1, | |
'<': -1, | |
'_': 0 | |
} | |
reverse_signs_map = { | |
0: '_', | |
1: '>', | |
-1: '<' | |
} | |
def get_goal_configuration(configuration): | |
return configuration[::-1] | |
def in_bounds(i, frog, current_configuration): | |
return (i + frog) >= 0 and (i + frog) < len(current_configuration) | |
def possible_actions(current_configuration, already_explanded_configurations): | |
actions_configurations = [] | |
for i in range(len(current_configuration)): | |
if current_configuration[i] == 0: | |
continue | |
frog = current_configuration[i] | |
if in_bounds(i, frog, current_configuration) and current_configuration[i + frog] == 0: | |
new_configuration = list(current_configuration[:]) | |
new_configuration[i] = 0 | |
new_configuration[i + frog] = frog | |
if new_configuration not in already_explanded_configurations: | |
actions_configurations.append((i, tuple(new_configuration))) | |
if in_bounds(i, frog * 2, current_configuration) and current_configuration[i + frog * 2] == 0 and current_configuration[i + frog] != 0: | |
new_configuration = list(current_configuration[:]) | |
new_configuration[i] = 0 | |
new_configuration[i + frog * 2] = frog | |
if new_configuration not in already_explanded_configurations: | |
actions_configurations.append((i, tuple(new_configuration))) | |
return actions_configurations | |
def serialize_configuration(tuple_configuration): | |
return ''.join(tuple(map(lambda x: reverse_signs_map[x], tuple_configuration))) | |
def search(configuration): | |
start_configuration = configuration | |
goal = get_goal_configuration(configuration) | |
white_configurations = [] | |
gray_configurations = [] | |
black_configurations = [] | |
frontier = [configuration] | |
path = [] | |
backtracking_map = {} | |
while goal not in black_configurations: | |
configuration = frontier.pop() | |
actions_configurations = possible_actions(configuration, black_configurations) | |
black_configurations.append(configuration) | |
for action, child_configuration in actions_configurations: | |
backtracking_map[child_configuration] = configuration | |
frontier.extend(list(map(lambda x: x[1], actions_configurations))) | |
tmp_node = goal | |
path = [] | |
while tmp_node != start_configuration: | |
path.append(serialize_configuration(tmp_node)) | |
tmp_node = backtracking_map[tmp_node] | |
return path[::-1] | |
def parse_configuration(string_configuration): | |
return tuple(map(lambda x: signs_map[x], string_configuration)) | |
def build_config(n): | |
configuration = '' | |
for i in range(n): | |
configuration += '>' | |
configuration += '_' | |
for i in range(n): | |
configuration += '<' | |
return configuration | |
if __name__ == "__main__": | |
n = int(sys.argv[1]) | |
configuration = build_config(n) | |
parsed_config = parse_configuration(configuration) | |
print(configuration) | |
for config in search(parsed_config): | |
print(config) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment