-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.py
More file actions
107 lines (95 loc) · 2.88 KB
/
18.py
File metadata and controls
107 lines (95 loc) · 2.88 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
import re
lines = [
'1 + 2 * 3 + 4 * 5 + 6',
'1 + (2 * 3) + (4 * (5 + 6))',
'2 * 3 + (4 * 5)',
'5 + (8 * 3 + 9 + 3 * 4 * 3)',
'5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))',
'((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2',
]
lines = [x.strip() for x in open('18.input', 'r').readlines() if x != '']
def end_paranthesis(expression):
count = 0
for i, ch in enumerate(expression):
if ch == '(':
count += 1
if ch == ')':
count -= 1
if count == 0:
break
return i
def math(expression, verbose=False):
if verbose:
print "Math Expression:", expression
# First calculate all paranthesis in a recursive call
i = 0
new_expression = ''
while i < len(expression):
ch = expression[i]
if ch == '(':
end = end_paranthesis(expression[i:])
new_expression += str(math(expression[i+1: end+i]))
i += end
else:
new_expression += ch
i += 1
expression = new_expression
# Evaluate all additions
i = 0
if verbose:
print 'after paranthesis', expression
match_found = True
while match_found:
match_found = False
for a,b in re.findall(r'(\d+)\+(\d+)', expression):
match_found = True
addition = int(a) + int(b)
expression = expression.replace('%s+%s'%(a,b), str(addition), 1)
break
if verbose:
print 'after sum', expression
# Evaluate all multiplications
match_found = True
while match_found:
match_found = False
for a,b in re.findall(r'(\d+)\*(\d+)', expression):
match_found = True
product = int(a) * int(b)
expression = expression.replace('%s*%s'%(a,b), str(product), 1)
break
if verbose:
print 'after mul', expression
return int(expression)
values = []
for line in lines:
expression = line.replace(' ', '')
value = math(expression)
values.append(value)
print "Solution part 2: %d" %sum(values)
def part1(lines):
def operate(a,b, op):
return a * b if op == '*' else a + b
def math(expression):
val = 0
i = 0
operator = '+'
while i < len(expression):
ch = expression[i]
if ch == '(':
end = end_paranthesis(expression[i:])
submath = expression[i+1: end+i]
subvalue = math(submath)
val = operate(val, subvalue, operator)
i += end
elif ch in ['+', '*']:
operator = ch
else:
digit = int(ch)
val = operate(val,digit, operator)
i += 1
return val
values = []
for line in lines:
values.append(math(line.replace(' ', '')))
print "Solution part 1: %d " % sum(values)
part1(lines)