You are reading a single comment by @wence and its replies. Click here to read the full conversation.
  • Very C-like, I ended up writing

    def trees(grid, r, d):
        return sum(row[(i*r) % len(row)] == "#"
                   for i, row in enumerate(grid[::d]))
    

    for the find-trees function.

  • were you saying my function was very C-like?

    i've actually just re-written it and it's almost bang on the same as yours with enumerate - had seen a few people talking about that so tried to get that working. still not entirely sure what the :: does in grid[::d], apart from take every d'th y.

    my function:

    def findTrees2(funcdata, dx, dy):
        numTrees = 0
        for i, line in enumerate(funcdata[::dy]):
            if line[(i*dx) % len(line)] == '#': numTrees += 1
        return numTrees
    

    edit: was also confused as to why you can't print an enumerate object until you put it into a list. but guess that's a conversation for data structures... i should really do a bit more formal learning

About

Avatar for wence @wence started