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!
I got part 1 done, but couldn't figure out how I could possibly do that big of a number of generations. So, I peeked (a little) here and, without seeing anyone explicitly giving it away -- but reading enough of the responses to get ideas -- I figured out the trick.
"""Day 12: Subterranean Sustainability
Conway's game of life with plants! See which plants will live or die
each generation.
"""fromcollectionsimportdequefromitertoolsimportislicefromtypingimportList,DictclassPlants(deque):"""A row of plants centered around zero"""def__init__(self,*args):deque.__init__(self,*args)self.zero=0defscore(self)->int:"""Sums up all the indices (including negative) that have a plant"""returnsum(i-self.zerofori,potinenumerate(self)ifpot=='#')def__getitem__(self,key)->islice:"""Allows slicing a deque"""ifisinstance(key,slice):returnislice(self,key.start,key.stop,key.step)else:returndeque.__getitem__(self,key)defcopy(self):result=Plants(self)result.zero=self.zeroreturnresultdefnext_generation(plants:Plants,rules:Dict[str,str])->Plants:"""Given a row of pots with/without plants and some rules, creates
the next generation of plants.
The only rules that could increase the length in either direction
require 3 dots on the end to check, so this makes sure there are 3
dots on each end just in case.
"""this_generation=plants.copy()ifany(c=='#'forcinthis_generation[:3]):this_generation.extendleft(['.']*3)this_generation.zero+=3ifany(c=='#'forcinthis_generation[len(this_generation)-3:]):this_generation.extend(['.']*3)next_generation=this_generation.copy()foriinrange(2,len(this_generation)-2):next_generation[i]=rules.get("".join(this_generation[i-2:i+3]),'.')returnnext_generationdefparse_rules(text:str)->Dict[str,str]:"""Parses the conversion rules from a block of text"""rules={}forlineintext.splitlines():pattern,_,output=line.partition(" => ")rules[pattern]=outputreturnrulesdefage(plants:Plants,generations:int,rules:Dict[str,str])->Plants:"""Ages a set of plants n generations"""foriinrange(generations):plants=next_generation(plants,rules)returnplantsif__name__=="__main__":withopen("python/data/day12.txt","r")asf:rules=parse_rules(f.read())initial_plants=Plants("##..#..##....#..#..#..##.#.###.######..#..###.#.#..##.###.#.##..###..#.#..#.##.##..###.#.#...#.##..")# Part 1
plants=age(initial_plants,20,rules)print(plants.score())# Part 2: Wherein it's #@%!# linear above about 300
plants=age(initial_plants,300,rules)print(plants.score()+(50000000000-300)*86)
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.
I got part 1 done, but couldn't figure out how I could possibly do that big of a number of generations. So, I peeked (a little) here and, without seeing anyone explicitly giving it away -- but reading enough of the responses to get ideas -- I figured out the trick.