did 2023 day1 p1 and then say p2

This commit is contained in:
JISAUAY 2024-12-03 16:48:03 -06:00
parent 92131a8449
commit 2cd5a8f9fd
2 changed files with 1041 additions and 16 deletions

1000
2023/day12/p1/input.text Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,50 @@
INPUT_FILE = "test.text"
import re
def main():
with open(INPUT_FILE, 'r') as file:
with open('input.text', 'r') as file:
data: list[str] = file.readlines()
# Parse Input
records = []
groups = []
for line in data:
record, groups = line.split()
groups: list[int] = [int(g) for g in groups.split(',')]
record, group = line.split(' ')
# for group in groups:
group = [int(n) for n in group.split(',')]
records.append(record)
groups.append(group)
# # if '#' * group in record:
# # groups.remove(group)
# # record = record.replace('#' * group, '')
# Part 1
print(f'{record} {groups}')
# Checks if a group of springs is valid based on the group info
def valid_group(record: str, groups: list[int]) -> bool:
damaged_groups = re.findall('#+', record)
if len(damaged_groups) != len(groups):
return False
for i, group in enumerate(damaged_groups):
if len(group) != groups[i]:
return False
return True
def check_combinations(record: str, group: list[int]) -> int:
if '?' in record:
new_record1 = record.replace('?', '.', 1)
new_record2 = record.replace('?', '#', 1)
return check_combinations(new_record1, group) + check_combinations(new_record2, group)
else:
return int(valid_group(record, group))
total = 0
for record, group in zip(records, groups):
total += check_combinations(record, group)
print(total)
if __name__ == '__main__':
main()