Add solution to problem 6
This commit is contained in:
66
Problem6_1.py
Normal file
66
Problem6_1.py
Normal file
@@ -0,0 +1,66 @@
|
||||
def printList(numbersList: list[list[int]], symbolsList: list[str]) :
|
||||
for line in numbersList :
|
||||
print(f"{line}")
|
||||
print(f"{symbolsList}")
|
||||
|
||||
def parseInput() :
|
||||
numbers: list[list[int]] = []
|
||||
for cnt in range(len(rawInput) - 1) :
|
||||
line = rawInput[cnt]
|
||||
numbers.append([])
|
||||
for num in line.split(" ") :
|
||||
if len(num.replace(" ", "")) == 0 :
|
||||
continue
|
||||
numbers[cnt].append(int(num))
|
||||
symbols: list[str] = []
|
||||
for sym in rawInput[-1].split(" ") :
|
||||
if len(sym.replace(" ", "")) == 0:
|
||||
continue
|
||||
symbols.append(sym.replace(" ", ""))
|
||||
return numbers, symbols
|
||||
|
||||
def getValue(col: list[int], symbol: str) :
|
||||
if symbol == "+" :
|
||||
return sum(col)
|
||||
else :
|
||||
return prod(col)
|
||||
|
||||
def prod(list: list[int]) :
|
||||
if len(list) == 0 :
|
||||
return 0
|
||||
value = 1
|
||||
for val in list :
|
||||
value *= val
|
||||
return value
|
||||
|
||||
|
||||
testInput = [
|
||||
"123 328 51 64 ",
|
||||
" 45 64 387 23 ",
|
||||
" 6 98 215 314",
|
||||
"* + * + "
|
||||
]
|
||||
|
||||
def readFile() :
|
||||
ary = []
|
||||
with open("files/Problem6.txt", "r") as file :
|
||||
for line in file :
|
||||
ary.append(line.replace("\n", ""))
|
||||
return ary
|
||||
|
||||
|
||||
#rawInput = testInput
|
||||
rawInput = readFile()
|
||||
numbers, symbols = parseInput()
|
||||
#printList(numbers, symbols)
|
||||
values: list[int] = []
|
||||
for colNum in range(len(numbers[0])) :
|
||||
col: list[int] = []
|
||||
for rowNum in range(len(numbers)) :
|
||||
col.append(numbers[rowNum][colNum])
|
||||
value = getValue(col, symbols[colNum])
|
||||
values.append(value)
|
||||
|
||||
print(f"Sum = {sum(values)}")
|
||||
|
||||
#Sum = 6891729672676
|
||||
Reference in New Issue
Block a user