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