-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol3.py
52 lines (34 loc) · 992 Bytes
/
sol3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import utils
def initialiseGrid():
global grid, rows, cols
grid = utils.loadInputFile("input_3.txt")
rows = len(grid)-1 # 31, so 30
cols = len(grid[0])-1 # 323, so 322
def computeTreeCollisionsWithSlope(deltaX, deltaY):
global grid, rows, cols
# initialise
currentX = 0
currentY = 0
treesEncountered = 0
while currentY < rows:
currentX += deltaX
currentY += deltaY
# wrap around when exceeded
if currentX > cols:
currentX -= 31
# safety check
if currentY > rows:
return treesEncountered
if grid[currentY][currentX] == "#":
treesEncountered += 1
return treesEncountered
def part1():
return computeTreeCollisionsWithSlope(3, 1)
def part2():
result = 1
for x, y in [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]]:
result *= computeTreeCollisionsWithSlope(x, y)
return result
initialiseGrid()
print(part1())
print(part2())