-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.py
75 lines (64 loc) · 2.42 KB
/
3.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
filename = '3.txt'
file = open("inputs/"+filename, 'r')
schematic = []
totalSum = 0
for line in file.readlines():
schematic.append(list(line.rstrip()))
def checkSymbol(lineModifier, charModifier):
try:
if ((not schematic[line+lineModifier][char+charModifier].isdigit()) and schematic[line+lineModifier][char+charModifier] != "."):
return True
else:
return False
except:
return False
currInt = ""
foundSymbol = False
gearLocation = None
gearRatios = {}
for line in range(len(schematic)):
if (currInt != ""):
if (foundSymbol):
totalSum += int(currInt)
currInt = ""
foundSymbol = False
for char in range(len(schematic[line])):
if (schematic[line][char].isdigit()):
currInt += schematic[line][char]
#Check surrounding spaces for a symbol
if (not foundSymbol):
for (lineModifier, charModifier) in [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]:
if (checkSymbol(lineModifier, charModifier)):
foundSymbol = True
if (schematic[line+lineModifier][char+charModifier] == "*"):
gearLocation = (line+lineModifier, char+charModifier)
break
else:
if (currInt != ""):
if (foundSymbol):
#print(currInt)
totalSum += int(currInt)
if (gearLocation != None):
if (gearLocation in gearRatios):
gearRatios[gearLocation][1] *= int(currInt)
gearRatios[gearLocation][0] += 1
else:
gearRatios[gearLocation] = [1, int(currInt)]
gearLocation = None
print(currInt)
foundSymbol = False
currInt = ""
else:
currInt = ""
gearLocation = None
else:
foundSymbol = False
currInt = ""
gearLocation = None
print(totalSum)
ratioSum = 0
print(gearRatios)
for gear in gearRatios:
if (gearRatios[gear][0] == 2):
ratioSum += gearRatios[gear][1]
print(ratioSum)