ralig

Advent Of Code 2020 Day 3 Part 2

Dec 3rd, 2020 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. from pathlib import Path
  2.  
  3. def TraverseSlope(xAmt, yAmt, input):
  4.     xLoc = 0
  5.     cntTrees = 0
  6.     for yLoc in range(yAmt,len(input),yAmt):
  7.         line = input[yLoc].replace("\n","")
  8.         xLoc = xLoc+xAmt
  9.         #determine if past end of input line, if yes, go back to beginning of line
  10.         if (xLoc) > len(line)-1:
  11.             xLoc = xLoc-len(line)
  12.        
  13.         #determine if tree in spot
  14.         if (line[xLoc] == "#"):
  15.             cntTrees += 1
  16.     return cntTrees
  17.  
  18. path = Path(__file__).parent / "../../input.txt"
  19.  
  20. with path.open() as f:
  21.     lines = f.readlines()
  22.  
  23. multTrees = 1
  24. multTrees *= TraverseSlope(1,1,lines)
  25. multTrees *= TraverseSlope(3,1,lines)
  26. multTrees *= TraverseSlope(5,1,lines)
  27. multTrees *= TraverseSlope(7,1,lines)
  28. multTrees *= TraverseSlope(1,2,lines)
  29. print(multTrees)
  30.  
Add Comment
Please, Sign In to add comment