Created
May 15, 2017 21:37
-
-
Save saoili/2c98f3bea9574708df6d6bcf54a20607 to your computer and use it in GitHub Desktop.
attempt at a pile of bricks
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 sys | |
def split_vert(vert): | |
""" strips surrounding brackets, splits on comma, casts to int | |
""" | |
return [int(c) for c in vert[1:-1].split(',')] | |
class Hole(object): | |
def __init__(self, hole_details): | |
self.vert1, self.vert2 = hole_details.split(" ") | |
x1, y1 = split_vert(self.vert1) | |
x2, y2 = split_vert(self.vert2) | |
self.width = abs(x1 - x2) | |
self.height = abs(y1 - y2) | |
def __str__(self): | |
return "{} {}, width {}, height {}".format( | |
self.vert1, self.vert2, self.width, self.height) | |
def __repr__(self): | |
return self.__str__() | |
class Brick(object): | |
def __init__(self, brick_details): | |
self.index, self.vert1, self.vert2 = brick_details[1:-1].split(" ") | |
x1, y1, z1 = split_vert(self.vert1) | |
x2, y2, z2 = split_vert(self.vert2) | |
self.small_side = [abs(x1 - x2), abs(y1 - y2), abs(z1 - z2)] | |
self.small_side.remove(max(self.small_side)) | |
def __str__(self): | |
return "vert1 is {}, vert2 {}, small_side is {}".format( | |
self.vert1, self.vert2, self.small_side) | |
def __repr__(self): | |
return self.__str__() | |
def fits_through(self, hole): | |
return ( | |
self.small_side[0] <= hole.height and | |
self.small_side[1] <= hole.width | |
) or ( | |
self.small_side[0] <= hole.width and | |
self.small_side[1] <= hole.height | |
) | |
if __name__ == "__main__": | |
with open(sys.argv[1], 'r') as inputs: | |
for line in inputs: | |
line = line.strip() | |
if not line: | |
continue | |
hole, bricks = line.split("|") | |
hole = Hole(hole) | |
bricks = [Brick(b) for b in bricks.split(";")] | |
answers = [b.index for b in bricks if b.fits_through(hole)] | |
print ",".join(answers) or "-" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment