From 2a7a59b9491a0a34db40f02c987d4889ad346b2c Mon Sep 17 00:00:00 2001 From: JISAUAY Date: Thu, 5 Dec 2024 16:09:59 -0600 Subject: [PATCH] day 12 p2 still got me stumped --- 2023/day12/{p1 => }/input.text | 0 2023/day12/{p1 => }/main.py | 23 ++++++++++++++++++++--- 2023/day12/{p1 => }/test.text | 0 3 files changed, 20 insertions(+), 3 deletions(-) rename 2023/day12/{p1 => }/input.text (100%) rename 2023/day12/{p1 => }/main.py (69%) rename 2023/day12/{p1 => }/test.text (100%) diff --git a/2023/day12/p1/input.text b/2023/day12/input.text similarity index 100% rename from 2023/day12/p1/input.text rename to 2023/day12/input.text diff --git a/2023/day12/p1/main.py b/2023/day12/main.py similarity index 69% rename from 2023/day12/p1/main.py rename to 2023/day12/main.py index b34dcb2..9eae9fe 100644 --- a/2023/day12/p1/main.py +++ b/2023/day12/main.py @@ -1,11 +1,11 @@ import re -with open('input.text', 'r') as file: +with open('test.text', 'r') as file: data: list[str] = file.readlines() # Parse Input - +seen = {} records = [] groups = [] for line in data: @@ -33,11 +33,16 @@ def valid_group(record: str, groups: list[int]) -> bool: return True def check_combinations(record: str, group: list[int]) -> int: + if record in seen: + return seen[record] + if '?' in record: new_record1 = record.replace('?', '.', 1) new_record2 = record.replace('?', '#', 1) - return check_combinations(new_record1, group) + check_combinations(new_record2, group) + ans = check_combinations(new_record1, group) + check_combinations(new_record2, group) + seen[record] = ans + return ans else: return int(valid_group(record, group)) @@ -47,4 +52,16 @@ for record, group in zip(records, groups): print(total) +# Part 2 +total = 0 +for record, group in zip(records, groups): + record = ((record + '?')* 5)[:-1] + group = group * 5 + + total += check_combinations(record, group) + +print(f'Part 2: {total}') + + + diff --git a/2023/day12/p1/test.text b/2023/day12/test.text similarity index 100% rename from 2023/day12/p1/test.text rename to 2023/day12/test.text