Created
August 11, 2014 09:48
-
-
Save yuheiomori/5c09c36a68b41074dfd7 to your computer and use it in GitHub Desktop.
Racing Chars (CodeEval) in Python 3.x
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
# coding=utf-8 | |
import sys | |
PASSING_TYPE_STRAIGHT = '|' | |
PASSING_TYPE_LEFT_TURN = '\\' | |
PASSING_TYPE_RIGHT_TURN = '/' | |
ROAD_TYPE_GATE = '_' | |
ROAD_TYPE_CHECKPOINT = 'C' | |
class RacingChars(object): | |
def __init__(self, file_name): | |
self.file_name = file_name | |
self.last_pos = -1 | |
self.next_pos = -1 | |
self.next_type = '' | |
def run(self): | |
test_cases = open(self.file_name, 'r') | |
for line in test_cases: | |
if line.rstrip(): | |
self.pass_by(line) | |
test_cases.close() | |
def pass_by(self, line): | |
self.find_next(line) | |
way_of_passing = self.decide_way_of_passing() | |
self.last_pos = self.next_pos | |
line = line.replace(self.next_type, way_of_passing) | |
print(line.rstrip()) | |
def find_next(self, s): | |
gate_pos, checkpoint_pos = s.find("_"), s.find("C") | |
if checkpoint_pos != -1: | |
self.next_pos = checkpoint_pos | |
self.next_type = ROAD_TYPE_CHECKPOINT | |
else: | |
self.next_pos = gate_pos | |
self.next_type = ROAD_TYPE_GATE | |
def decide_way_of_passing(self): | |
way_of_passing = PASSING_TYPE_STRAIGHT | |
if self.last_pos != -1: | |
if self.next_pos > self.last_pos: | |
way_of_passing = PASSING_TYPE_LEFT_TURN | |
elif self.next_pos < self.last_pos: | |
way_of_passing = PASSING_TYPE_RIGHT_TURN | |
return way_of_passing | |
def main(): | |
RacingChars(sys.argv[1]).run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment