Last active
May 16, 2019 23:59
-
-
Save alexandrehuat/dd240bea9af28c158b719c7ea026c9a5 to your computer and use it in GitHub Desktop.
Battle Dev RegionsJob - Mars 2019 : Exercice 5/6 : Couloir au trésor
This file contains 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
""" | |
Test the solution from here : https://www.isograd.com/FR/solutionconcours.php | |
""" | |
#******* | |
#* Read input from STDIN | |
#* Use: echo or print to output your result to STDOUT, use the /n constant at the end of each result line. | |
#* Use: sys.stderr.write() to display debugging information to STDERR | |
#* ***/ | |
import sys | |
lines = [] | |
for line in sys.stdin: | |
lines.append(line.rstrip('\n')) | |
corridor = lines[1] | |
X_pos = corridor.index("X") | |
left, right = ''.join(reversed(corridor[:X_pos])), corridor[X_pos+1:] # left and right objects in increasing order of proximity with X | |
nl, nr = len(left), len(right) # numbers of objects | |
print(corridor, file=sys.stderr) | |
def score(treasure): | |
""" | |
Scores a treasure objects sequence. | |
Example: score('o*oo**') == (1*2+1+1)*2*2 == 16. | |
""" | |
s = 0 | |
for obj in treasure: | |
if obj == 'o': | |
s += 1 | |
elif obj == '*': | |
s *= 2 | |
return s | |
def push_left(string, i): | |
""" | |
Moves the i-th character of the string to the left. | |
Example: push_left('abcd', 2) == 'acbd'. | |
""" | |
return string[:i-1] + string[i] + string[i-1] + string[i+1:] | |
def search(left, right): | |
""" | |
Generates all possible object moves in the corridor. | |
""" | |
treasure = left + right | |
moves = nl * 'l' + nr * 'r' | |
yield treasure, moves | |
for j in range(nr): | |
for i in range(nl): | |
pos = nl - i + j | |
treasure = push_left(treasure, pos) | |
moves = push_left(moves, pos) | |
yield treasure, moves | |
score_max = -1 | |
for treasure, moves in search(left, right): | |
score_ = score(treasure) | |
max_info = (score_ > score_max) * 'NEW ' + (score_ >= score_max) * 'MAX!' | |
print(moves, treasure, score_, max_info, file=sys.stderr) | |
if score_ >= score_max: | |
best_treasure = treasure | |
best_moves = moves | |
score_max = score_ | |
print(file=sys.stderr) | |
print(best_treasure) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment