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!
Quick one today! Probably could improve efficiency in the Conway section, but it runs pretty quick so I'm good with it. Forgot for a minute that I needed to loop over the white tiles sharing boundaries with my black tiles too!
fromcollectionsimportdefaultdictimportsysBLACK=TrueWHITE=Falsetiles=defaultdict(bool)filename=sys.argv[1]withopen(filename,"r")asf:forlineinf:x=0y=0line_iter=iter(line)forcharinline_iter:ifchar=='e':x+=1elifchar=='w':x-=1elifchar=='n':y+=1ifnext(line_iter)=='e':x+=0.5else:x-=0.5elifchar=='s':y-=1ifnext(line_iter)=='e':x+=0.5else:x-=0.5tiles[(x,y)]=nottiles[(x,y)]print("Part 1:",sum(tiles.values()))foriinrange(100):new_tiles=defaultdict(bool)# Ensure white tiles around black tiles in input set
blacks=[coordforcoord,colorintiles.items()ifcolor]forx,yinblacks:tiles[(x+1,y)]tiles[(x-1,y)]tiles[(x+0.5,y+1)]tiles[(x-0.5,y+1)]tiles[(x+0.5,y-1)]tiles[(x-0.5,y-1)]# Evolve based on rules
for(x,y),colorintiles.items():total=sum([(x+1,y)intilesandtiles[(x+1,y)],(x-1,y)intilesandtiles[(x-1,y)],(x+0.5,y+1)intilesandtiles[(x+0.5,y+1)],(x-0.5,y+1)intilesandtiles[(x-0.5,y+1)],(x+0.5,y-1)intilesandtiles[(x+0.5,y-1)],(x-0.5,y-1)intilesandtiles[(x-0.5,y-1)],])ifcolor==BLACKand(total==0ortotal>2):new_tiles[(x,y)]=WHITEelifcolor==WHITEandtotal==2:new_tiles[(x,y)]=BLACKelse:new_tiles[(x,y)]=colortiles=new_tilesprint("Part 2:",sum(tiles.values()))
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.
Quick one today! Probably could improve efficiency in the Conway section, but it runs pretty quick so I'm good with it. Forgot for a minute that I needed to loop over the white tiles sharing boundaries with my black tiles too!