Created
December 5, 2014 21:36
-
-
Save metaperl/f620ead49b85c4baa9d2 to your computer and use it in GitHub Desktop.
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
import collections | |
import re | |
import sys | |
class Command: | |
""" | |
A Command is an object consisting of a start_floor | |
and a list of Transition instances | |
""" | |
def __init__(self, start_floor, transitions): | |
self.start_floor = start_floor | |
self.transitions = transitions | |
def __str__(self): | |
return "Command, \n\tstart_floor = {0}\n\ttransitions = {1}".format( | |
self.start_floor, self.transitions) | |
class Transition: | |
""" | |
A transition is an object consisting of an origin floor | |
and destination floor | |
""" | |
def __init__(self, origin, destination): | |
self.origin = origin | |
self.destination = destination | |
def __str__(self): | |
return "Transition {0} -> {1}".format(self.origin, self.destination) | |
def parse_command_string(s): | |
""" | |
Given a COMMAND_STRING, return a 2-tuple of START_FLOOR and | |
list of ORIGINS_AND_DESTINATIONS, where ORIGINS_AND_DESTINATIONS | |
is a namedtuple of origin and destination. | |
""" | |
start_floor, origins_and_destinations = s.split(':') | |
start_floor = int(start_floor) | |
command = Command(start_floor, []) | |
for o_d in origins_and_destinations.split(','): | |
o,d = [ int(i) for i in o_d.split('-') ] | |
print start_floor, o,d | |
command.transitions.append(Transition(o,d)) | |
return command | |
def commands(filename): | |
""" | |
Given a FILENAME, open the file and return each line as a command. | |
""" | |
f = file(filename).read() | |
for command_string in f.split('\n'): | |
print command_string | |
yield parse_command_string(command_string) | |
def main(document_name): | |
""" | |
Given a FILENAME return a concordance of it's data. | |
""" | |
for i, command in enumerate(commands(document_name)): | |
print command | |
if __name__ == '__main__': | |
input_file='input.dat' | |
main(input_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment