From: Brett Parker Date: Wed, 15 Dec 2021 00:17:16 +0000 (+0000) Subject: Fixup day18 and add second part X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/commitdiff_plain/HEAD?hp=74eb6cebd0b3532d96275e3e2aa8d41657f00cbf Fixup day18 and add second part --- diff --git a/day18/badmath.py b/day18/badmath.py index b7e6dda..9ebf20e 100755 --- a/day18/badmath.py +++ b/day18/badmath.py @@ -62,11 +62,8 @@ def do_sum(parts): 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 1 Total:", total) - - diff --git a/day18/badmath2.py b/day18/badmath2.py new file mode 100755 index 0000000..4100f45 --- /dev/null +++ b/day18/badmath2.py @@ -0,0 +1,87 @@ +#!/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) diff --git a/day18/p2_1445.txt b/day18/p2_1445.txt new file mode 100644 index 0000000..48e9a4e --- /dev/null +++ b/day18/p2_1445.txt @@ -0,0 +1 @@ +5 + (8 * 3 + 9 + 3 * 4 * 3) diff --git a/day18/p2_23340.txt b/day18/p2_23340.txt new file mode 100644 index 0000000..9a421d5 --- /dev/null +++ b/day18/p2_23340.txt @@ -0,0 +1 @@ +((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 diff --git a/day18/p2_46.txt b/day18/p2_46.txt new file mode 100644 index 0000000..ea858ad --- /dev/null +++ b/day18/p2_46.txt @@ -0,0 +1 @@ +2 * 3 + (4 * 5) diff --git a/day18/p2_51.txt b/day18/p2_51.txt new file mode 120000 index 0000000..c63a6eb --- /dev/null +++ b/day18/p2_51.txt @@ -0,0 +1 @@ +p1_51.txt \ No newline at end of file diff --git a/day18/p2_669060.txt b/day18/p2_669060.txt new file mode 100644 index 0000000..81fdf5d --- /dev/null +++ b/day18/p2_669060.txt @@ -0,0 +1 @@ +5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))