diff --git a/day18/module.py b/day18/module.py index d5288aa..221db99 100644 --- a/day18/module.py +++ b/day18/module.py @@ -11,13 +11,52 @@ def pw(line): return line.strip() 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) rr = [] for sample in inp: stack = [] operand = '' operator = '' - print(sample) + #print(sample) for i in sample[0:]: if i == ' ': continue @@ -50,9 +89,9 @@ def p1(): stack.append((res, operator, '')) operator = '' res = 0 - print(i, res, stack) + #print(i, res, stack) while(len(stack)>0): - print(res, stack) + #print(res, stack) value, operator, brace = stack.pop() if operator == '': if value != 0: @@ -63,17 +102,12 @@ def p1(): res *= value - print(res) + #print(res) rr.append(res) res = 0 print(sum(rr)) - return inp - -def p2(): - inp = get_input(pw) - print(len(inp)) return 0 result1 = None