-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayThreeSecondAssignment.py
More file actions
121 lines (105 loc) · 3.59 KB
/
Copy pathdayThreeSecondAssignment.py
File metadata and controls
121 lines (105 loc) · 3.59 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#There are 323 lines, each 31 characters across
#Set initial variables
rowNum = 1
colNum = 0
tree = '#'
free = '.'
treesEncountered = 0
#Right 1 Down 1
with open("DayThreeInput.txt") as pathways:
rowNum = 1
colNum = 0
treesEncountered = 0
for row in pathways:
print("\n \nYou're at position: ", rowNum, colNum)
treePositions = [i for i, a in enumerate(row) if a == tree]
print("There are trees at the following positions: ", treePositions)
#Check if a tree was encountered
if colNum in treePositions:
treesEncountered = treesEncountered + 1
print("You've hit ", treesEncountered, " trees.")
colNum = colNum + 1
rowNum = rowNum + 1
if colNum > 30:
colNum = colNum - 31
r1d1Trees = treesEncountered
#Right 3 down 1
with open("DayThreeInput.txt") as pathways:
rowNum = 1
colNum = 0
treesEncountered = 0
for row in pathways:
print("\n \nYou're at position: ", rowNum, colNum)
treePositions = [i for i, a in enumerate(row) if a == tree]
print("There are trees at the following positions: ", treePositions)
#Check if a tree was encountered
if colNum in treePositions:
treesEncountered = treesEncountered + 1
print("You've hit ", treesEncountered, " trees.")
colNum = colNum + 3
rowNum = rowNum + 1
if colNum > 30:
colNum = colNum - 31
r3d1Trees = treesEncountered
#Right 5 Down 1
with open("DayThreeInput.txt") as pathways:
rowNum = 1
colNum = 0
treesEncountered = 0
for row in pathways:
print("\n \nYou're at position: ", rowNum, colNum)
treePositions = [i for i, a in enumerate(row) if a == tree]
print("There are trees at the following positions: ", treePositions)
#Check if a tree was encountered
if colNum in treePositions:
treesEncountered = treesEncountered + 1
print("You've hit ", treesEncountered, " trees.")
colNum = colNum + 5
rowNum = rowNum + 1
if colNum > 30:
colNum = colNum - 31
r5d1Trees = treesEncountered
#Right 7 down 1
with open("DayThreeInput.txt") as pathways:
rowNum = 1
colNum = 0
treesEncountered = 0
for row in pathways:
print("\n \nYou're at position: ", rowNum, colNum)
treePositions = [i for i, a in enumerate(row) if a == tree]
print("There are trees at the following positions: ", treePositions)
#Check if a tree was encountered
if colNum in treePositions:
treesEncountered = treesEncountered + 1
print("You've hit ", treesEncountered, " trees.")
colNum = colNum + 7
rowNum = rowNum + 1
if colNum > 30:
colNum = colNum - 31
r7d1Trees = treesEncountered
#Hard one, first use of 'continue'
#Right 1 Down 2
with open("DayThreeInput.txt") as pathways:
rowNum = 1
colNum = 0
currentRow = 0
treesEncountered = 0
for row in pathways:
currentRow = currentRow + 1
if currentRow != rowNum:
continue
print("\n \nYou're at position: ", rowNum, colNum)
treePositions = [i for i, a in enumerate(row) if a == tree]
print("There are trees at the following positions: ", treePositions)
#Check if a tree was encountered
if colNum in treePositions:
treesEncountered = treesEncountered + 1
print("You've hit ", treesEncountered, " trees.")
colNum = colNum + 1
rowNum = rowNum + 2
if colNum > 30:
colNum = colNum - 31
r1d2Trees = treesEncountered
totalTrees = int(r1d1Trees) * int(r3d1Trees) * int(r5d1Trees) * int(r7d1Trees) * int(r1d2Trees)
print("\nThere were: ", r1d1Trees, " on the first, ", r3d1Trees, " on the second, ", r5d1Trees, " on the third, ", r7d1Trees, " on the fourth, and ", r1d2Trees, " on the last.")
print("\nYou hit ", totalTrees, " trees total.")