--- /dev/null
+#!/usr/bin/python3
+
+import sys
+
+filename="p1_71.txt"
+if len(sys.argv) > 1:
+ filename=sys.argv[1]
+
+def parse_sum(oursum):
+ parts = oursum.split()
+ new_parts=[]
+
+ for part in parts:
+ if part[0]=="(":
+ for index,c in enumerate(part):
+ if c=="(":
+ new_parts.append("(")
+ else:
+ new_parts.extend(parse_sum(part[index:]))
+ break
+ elif part[-1]==")":
+ got_number=False
+ for index,c in enumerate(part):
+ if c==")":
+ if not got_number:
+ new_parts.extend(parse_sum(part[0:index]))
+ got_number=True
+ new_parts.append(c)
+ elif part in ['+','*']:
+ new_parts.append(part)
+ else:
+ new_parts.append(int(part))
+ return new_parts
+
+def do_sum(parts):
+ cur_total=0
+ next_oper="+"
+ bracketed=[]
+ in_brackets=0
+ new_parts=[]
+
+ # first do all bracketed parts
+ for part in parts:
+ if in_brackets > 0 and part != ")" and part != "(":
+ bracketed.append(part)
+ continue
+ if in_brackets > 0 and part == ")":
+ in_brackets-=1
+ bracketed.append(part)
+ if in_brackets == 0:
+ part=do_sum(bracketed[1:-1])
+ bracketed.clear()
+ else:
+ continue
+ if part == "(":
+ in_brackets+=1
+ bracketed.append(part)
+ continue
+ new_parts.append(part)
+
+ while "+" in new_parts:
+ plus_index = new_parts.index("+")
+ left=new_parts[plus_index-1]
+ right=new_parts[plus_index+1]
+ new_new_parts=new_parts[:plus_index-1]
+ new_new_parts.append(left+right)
+ new_new_parts.extend(new_parts[plus_index+2:])
+ new_parts=new_new_parts
+
+ cur_total=new_parts[0]
+
+ # now we should just be able to go through new_parts left to right
+ for part in new_parts[1:]:
+ if part == '*':
+ continue
+ else:
+ cur_total*=int(part)
+
+ return cur_total
+
+total=0
+total2=0
+for line in [line.rstrip() for line in open(filename, "r")]:
+ answer=do_sum(parse_sum(line))
+ total+=answer
+
+print("Part 2 Total:", total)