Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Alright, now I'm making up ground on the ones I got behind on. I actually really enjoyed this one! It was fun tuning things to be readable and easy to follow.
"""Day 13: Mine Cart Madness
Figure out when carts on tracks will crash
"""fromtypingimportListclassVector:"""A 2D vector. Right is +X, Down is +Y"""def__init__(self,x,y):self.x=xself.y=ydef__add__(self,other):returnVector(self.x+other.x,self.y+other.y)def__eq__(self,other):returnself.x==other.xandself.y==other.ydef__repr__(self):returnf"Vector({self.x}, {self.y})"def__str__(self):returnf"<{self.x}, {self.y}>"def__hash__(self):returnhash((self.x,self.y))NORTH=Vector(0,-1)SOUTH=Vector(0,1)EAST=Vector(1,0)WEST=Vector(-1,0)DIRECTIONS=[NORTH,EAST,SOUTH,WEST]classCart:"""A cart that drives around the mine"""def__init__(self,position:Vector,direction:Vector):self.position=positionself.direction=directionself.next_turn="left"defmove(self):"""Updates position based on direction"""self.position+=self.directiondefturn_right(self):self.direction=DIRECTIONS[(DIRECTIONS.index(self.direction)+1)%len(DIRECTIONS)]defturn_left(self):self.direction=DIRECTIONS[(DIRECTIONS.index(self.direction)-1)%len(DIRECTIONS)]defhandle_next_turn(self):"""Update own direction based on the next turn value specified in the prompt"""ifself.next_turn=="left":self.turn_left()self.next_turn="straight"elifself.next_turn=="straight":self.next_turn="right"elifself.next_turn=="right":self.turn_right()self.next_turn="left"classMine:"""A mine that has a bunch of different carts on tracks"""def__init__(self,map:str):self.map=[]self.carts:List[Cart]=[]fory,lineinenumerate(map.splitlines()):self.map.append([])forx,cinenumerate(line):ifc=='>':self.carts.append(Cart(Vector(x,y),EAST))self.map[-1].append('-')elifc=='<':self.carts.append(Cart(Vector(x,y),WEST))self.map[-1].append('-')elifc=='^':self.carts.append(Cart(Vector(x,y),NORTH))self.map[-1].append('|')elifc=='v':self.carts.append(Cart(Vector(x,y),SOUTH))self.map[-1].append('|')else:self.map[-1].append(c)deffind_first_collision(self)->Vector:"""Simulates the mine until two carts collide. Returns that x, y pair"""whileTrue:self.sort_carts()forcartinself.carts:cart.move()self.turn_cart(cart)ifself.is_collision(cart):returncart.positiondeflast_cart_location(self)->Vector:"""Simulates the mine, removing carts when they collide.
Returns the x, y pair of the last cart standing
"""whileTrue:self.sort_carts()crashed=set()forcartinself.carts:ifcart.positionincrashed:continuecart.move()self.turn_cart(cart)ifself.is_collision(cart):crashed.add(cart.position)continueself.carts=[cartforcartinself.cartsifcart.positionnotincrashed]iflen(self.carts)==1:returnself.carts[0].positiondefsort_carts(self):"""Carts are always run in reading order, top to bottom, left to right"""self.carts.sort(key=lambdacart:(cart.position.y,cart.position.x))defis_collision(self,cart:Cart)->bool:"""Checks whether or not a cart is running into any other carts"""returnsum(other.position==cart.positionforotherinself.carts)>1defturn_cart(self,cart:Cart):"""Depending on what kind of track the cart is on, turn it appropriately"""track_type=self.map[cart.position.y][cart.position.x]iftrack_type=='+':cart.handle_next_turn()eliftrack_type=='/':ifcart.direction==NORTHorcart.direction==SOUTH:cart.turn_right()else:cart.turn_left()eliftrack_type=='\\':ifcart.direction==NORTHorcart.direction==SOUTH:cart.turn_left()else:cart.turn_right()if__name__=="__main__":withopen("python/data/day13.txt","r")asf:mine_map=f.read()# Part 1
mine=Mine(mine_map)print(mine.find_first_collision())# Part 2
mine=Mine(mine_map)print(mine.last_cart_location())
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Alright, now I'm making up ground on the ones I got behind on. I actually really enjoyed this one! It was fun tuning things to be readable and easy to follow.