From ad115c8f98b8faea9c95c8102b0dcf0e2e5cbf12 Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Wed, 15 Dec 2021 00:17:16 +0000 Subject: [PATCH 1/1] Fixup day18 and add second part --- day18/badmath.py | 3 -- day18/badmath2.py | 87 +++++++++++++++++++++++++++++++++++++++++++++ day18/p2_1445.txt | 1 + day18/p2_23340.txt | 1 + day18/p2_46.txt | 1 + day18/p2_51.txt | 1 + day18/p2_669060.txt | 1 + 7 files changed, 92 insertions(+), 3 deletions(-) create mode 100755 day18/badmath2.py create mode 100644 day18/p2_1445.txt create mode 100644 day18/p2_23340.txt create mode 100644 day18/p2_46.txt create mode 120000 day18/p2_51.txt create mode 100644 day18/p2_669060.txt 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)) -- 2.39.5