day 18
This commit is contained in:
parent
3a23406310
commit
c305a3bfca
2 changed files with 119 additions and 0 deletions
57
18/18-1.py
Normal file
57
18/18-1.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import sys, re
|
||||||
|
|
||||||
|
def find_paren(items):
|
||||||
|
if ')' not in items:
|
||||||
|
return None
|
||||||
|
close = items.index(')')
|
||||||
|
for x in range(close, -1, -1):
|
||||||
|
#print (x,items[x])
|
||||||
|
if items[x] == '(':
|
||||||
|
return(x, close)
|
||||||
|
|
||||||
|
def do(op, a, b):
|
||||||
|
if op == '+':
|
||||||
|
return a+b
|
||||||
|
elif op == '-':
|
||||||
|
return a-b
|
||||||
|
elif op == '*':
|
||||||
|
return a*b
|
||||||
|
elif op == '/':
|
||||||
|
return a/b
|
||||||
|
|
||||||
|
def solve(nums):
|
||||||
|
items = nums.split(' ')
|
||||||
|
total = None
|
||||||
|
op = ''
|
||||||
|
for n in items:
|
||||||
|
if n == '*' or n == '+' or n == '-' or n == '/':
|
||||||
|
op = n
|
||||||
|
else:
|
||||||
|
if total is None:
|
||||||
|
total = int(n)
|
||||||
|
else:
|
||||||
|
total = do(op, total, int(n))
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
def process(items):
|
||||||
|
|
||||||
|
p = find_paren(items)
|
||||||
|
while p is not None:
|
||||||
|
before = p[0]
|
||||||
|
after = p[1]
|
||||||
|
|
||||||
|
nitems = items[:before] + str(solve(items[before+1:after])) + items[after+1:]
|
||||||
|
items = nitems
|
||||||
|
p = find_paren(items)
|
||||||
|
return(solve(items))
|
||||||
|
|
||||||
|
all = []
|
||||||
|
for line in sys.stdin:
|
||||||
|
line = line.rstrip()
|
||||||
|
print(line)
|
||||||
|
all.append(process(line))
|
||||||
|
|
||||||
|
print(sum(all))
|
62
18/18-2.py
Normal file
62
18/18-2.py
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import sys, re
|
||||||
|
|
||||||
|
def find_paren(items):
|
||||||
|
if ')' not in items:
|
||||||
|
return None
|
||||||
|
close = items.index(')')
|
||||||
|
for x in range(close, -1, -1):
|
||||||
|
#print (x,items[x])
|
||||||
|
if items[x] == '(':
|
||||||
|
return(x, close)
|
||||||
|
|
||||||
|
def do(op, a, b):
|
||||||
|
if op == '+':
|
||||||
|
return a+b
|
||||||
|
elif op == '-':
|
||||||
|
return a-b
|
||||||
|
elif op == '*':
|
||||||
|
return a*b
|
||||||
|
elif op == '/':
|
||||||
|
return a/b
|
||||||
|
|
||||||
|
def solve(nums):
|
||||||
|
items = nums.split(' ')
|
||||||
|
total = None
|
||||||
|
op = ''
|
||||||
|
for n in items:
|
||||||
|
if n == '*' or n == '+' or n == '-' or n == '/':
|
||||||
|
op = n
|
||||||
|
else:
|
||||||
|
if total is None:
|
||||||
|
total = int(n)
|
||||||
|
else:
|
||||||
|
total = do(op, total, int(n))
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
def process(items):
|
||||||
|
items =re.sub('(\d+ \+ \d+)', '(\\1)', items)
|
||||||
|
print(items)
|
||||||
|
p = find_paren(items)
|
||||||
|
while p is not None:
|
||||||
|
before = p[0]
|
||||||
|
after = p[1]
|
||||||
|
nitems = items[:before] + str(solve(items[before+1:after])) + items[after+1:]
|
||||||
|
items = nitems
|
||||||
|
|
||||||
|
items =re.sub('(\d+ \+ \d+)', '(\\1)', items)
|
||||||
|
p = find_paren(items)
|
||||||
|
return(solve(items))
|
||||||
|
|
||||||
|
all = []
|
||||||
|
for line in sys.stdin:
|
||||||
|
line = line.rstrip()
|
||||||
|
print(line)
|
||||||
|
res = process(line)
|
||||||
|
print(res)
|
||||||
|
all.append(res)
|
||||||
|
|
||||||
|
print(sum(all))
|
Loading…
Add table
Add a link
Reference in a new issue