This commit is contained in:
2020-12-18 09:41:08 +01:00
parent e1df2a6d5a
commit 1ab4862804

View File

@@ -11,13 +11,52 @@ def pw(line):
return line.strip() return line.strip()
def p1(): def p1():
inp = get_input(pw)
tot = []
for sample in inp:
stack = []
operand = ''
operator = ''
for i in sample[0:]:
if i == ' ':
continue
if i in list(map(str, range(10))):
operand = i
if operator == '':
res = int(operand)
elif operator == '+':
res += int(operand)
elif operator == '*':
res *= int(operand)
if i == '(':
stack.append((res, operator))
operator = ''
res = 0
elif i == ')':
value, operator = stack.pop()
if operator == '':
res = value
elif operator == '+':
res += value
elif operator == '*':
res *= value
else:
operator = i
tot.append(res)
res = 0
print(sum(tot))
return inp
def p2():
inp = get_input(pw) inp = get_input(pw)
rr = [] rr = []
for sample in inp: for sample in inp:
stack = [] stack = []
operand = '' operand = ''
operator = '' operator = ''
print(sample) #print(sample)
for i in sample[0:]: for i in sample[0:]:
if i == ' ': if i == ' ':
continue continue
@@ -50,9 +89,9 @@ def p1():
stack.append((res, operator, '')) stack.append((res, operator, ''))
operator = '' operator = ''
res = 0 res = 0
print(i, res, stack) #print(i, res, stack)
while(len(stack)>0): while(len(stack)>0):
print(res, stack) #print(res, stack)
value, operator, brace = stack.pop() value, operator, brace = stack.pop()
if operator == '': if operator == '':
if value != 0: if value != 0:
@@ -63,17 +102,12 @@ def p1():
res *= value res *= value
print(res) #print(res)
rr.append(res) rr.append(res)
res = 0 res = 0
print(sum(rr)) print(sum(rr))
return inp
def p2():
inp = get_input(pw)
print(len(inp))
return 0 return 0
result1 = None result1 = None