Advertisement
rosien

Count_trees

Feb 16th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. file = open('Day3.txt')
  2. data = file.readlines()
  3. data = [x.strip() for x in data]
  4.  
  5.  
  6. #define the tree calculating method
  7. def count_trees(right, step, data):
  8.     position = 0
  9.     tree = 0
  10.     for ind in range(0, len(data),step):
  11.         line = data[ind]
  12.         if len(line)-1 < position:
  13.             line = line *(position//len(line) +1)
  14.         if line[position] == '#':
  15.             tree += 1
  16.         position += right
  17.     return tree
  18.  
  19. #part 1
  20. count_trees(3,1,data)
  21.  
  22. #part 2
  23. rights = [1,3,5,7,1]
  24. steps = [1,1,1,1,2]
  25. tree_found = 1
  26. for right, step in zip(rights, steps):
  27.     tree = count_trees(right, step, data)
  28.     tree_found *= tree
  29. tree_found
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement