Last active
December 11, 2015 15:38
-
-
Save jakab922/2ea3125dbdf4e2270d16 to your computer and use it in GitHub Desktop.
Solution for https://community.topcoder.com/stat?c=problem_statement&pm=7587 . A pain in the ass...
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 re | |
PUT_PATTERN = re.compile(r'PUT (?P<what>[0-9]+) INSIDE (?P<to>[0-9]+)') | |
LOOSE_PATTERN = re.compile(r'SET (?P<which>[0-9]+) LOOSE') | |
SWAP_PATTERN = re.compile(r'SWAP (?P<one>[0-9]+) WITH (?P<other>[0-9]+)') | |
class Bag(object): | |
def __init__(self, n, parent=None): | |
self.n = n | |
self.contains = set() | |
self.parent = parent | |
@property | |
def correct(self): | |
return all([el.n < self.n for el in self.contains]) | |
def remove(self, bag): | |
self.contains.remove(bag) | |
def add(self, bag): | |
bag.parent = self | |
self.contains.add(bag) | |
def __str__(self): | |
return "%s - %s" % (self.n, list(self.contains)) | |
__repr__ = __str__ | |
class BagsQuiz(object): | |
def checkIfProper(self, n, actions): | |
self.good = True | |
self.floor = Bag(0) | |
self.bags = [Bag(i + 1, self.floor) for i in xrange(n)] | |
self.floor.contains = set(self.bags) | |
for action in actions: | |
m = PUT_PATTERN.match(action) | |
if m is not None: | |
what = int(m.group('what')) - 1 | |
to = int(m.group('to')) - 1 | |
self.put(what, to) | |
print "action: %s, result: %s" % (action, self.floor) | |
continue | |
m = LOOSE_PATTERN.match(action) | |
if m is not None: | |
which = int(m.group('which')) - 1 | |
self.loose(which) | |
print "action: %s, result: %s" % (action, self.floor) | |
continue | |
m = SWAP_PATTERN.match(action) | |
if m is not None: | |
one = int(m.group('one')) - 1 | |
other = int(m.group('other')) - 1 | |
self.swap(one, other) | |
print "action: %s, result: %s" % (action, self.floor) | |
if not self.good or not all([bag.correct for bag in self.bags]): | |
return -1 | |
else: | |
return len(self.floor.contains) | |
def put(self, what, to): | |
wbag = self.bags[what] | |
tbag = self.bags[to] | |
if wbag.parent == self.floor and tbag.parent == self.floor: | |
wbag.parent.remove(wbag) | |
tbag.add(wbag) | |
else: | |
self.good = False | |
def loose(self, which): | |
cbag = self.bags[which] | |
if cbag.parent == self.floor: | |
for el in cbag.contains: | |
self.floor.add(el) | |
cbag.contains = set() | |
else: | |
self.good = False | |
def swap(self, one, other): | |
bag1 = self.bags[one] | |
bag2 = self.bags[other] | |
if bag1.parent == self.floor and bag2.parent == self.floor: | |
bag_list1 = list(bag1.contains) | |
bag_list2 = list(bag2.contains) | |
for bag in bag_list1: | |
bag1.remove(bag) | |
bag2.add(bag) | |
for bag in bag_list2: | |
bag2.remove(bag) | |
bag1.add(bag) | |
else: | |
self.good = False | |
if __name__ == "__main__": | |
n = 3 | |
actions = ["PUT 1 INSIDE 2", "PUT 3 INSIDE 1"] | |
bq = BagsQuiz() | |
print bq.checkIfProper(n, actions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment