Ad
Code
Diff
  • def count_trees(trees):
        return sum([[check_tree(y, x, trees) for x in range(len(trees[y]))].count(True) for y in range(len(trees))])  # go through all trees and sum results
    
    def check_tree(y, x, trees) -> bool:  # called for each tree in grid
        for ymod, xmod in ((1, 0), (0, 1), (-1, 0), (0, -1)):  # directions
            ytmp, xtmp = y + ymod, x + xmod  # vars we will be moving and checking
            valid = True
            while 0 <= ytmp < len(trees) and 0 <= xtmp < len(trees[y]):  # move until you hit the edge
                if trees[ytmp][xtmp] >= trees[y][x]:  # another tree blocks view
                    valid = False
                    break  # look in another direction
                ytmp += ymod  # move to the next tree
                xtmp += xmod
            if valid:
                return True
        return False
    • def count_trees(trees):
    • return None
    • return sum([[check_tree(y, x, trees) for x in range(len(trees[y]))].count(True) for y in range(len(trees))]) # go through all trees and sum results
    • def check_tree(y, x, trees) -> bool: # called for each tree in grid
    • for ymod, xmod in ((1, 0), (0, 1), (-1, 0), (0, -1)): # directions
    • ytmp, xtmp = y + ymod, x + xmod # vars we will be moving and checking
    • valid = True
    • while 0 <= ytmp < len(trees) and 0 <= xtmp < len(trees[y]): # move until you hit the edge
    • if trees[ytmp][xtmp] >= trees[y][x]: # another tree blocks view
    • valid = False
    • break # look in another direction
    • ytmp += ymod # move to the next tree
    • xtmp += xmod
    • if valid:
    • return True
    • return False

Code using datetime lib

Code
Diff
  • from datetime import datetime
    def days(month,day):
        return (datetime(2022, month, day) - datetime(2022, 1, 1)).days + 1
    • from datetime import datetime
    • def days(month,day):
    • return sum([31,28,31,30,31,30,31,31,30,31,30,31][:month-1])+day
    • return (datetime(2022, month, day) - datetime(2022, 1, 1)).days + 1