did 2023 day1 p1 and then say p2
This commit is contained in:
parent
92131a8449
commit
2cd5a8f9fd
1000
2023/day12/p1/input.text
Normal file
1000
2023/day12/p1/input.text
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,25 +1,50 @@
|
|||||||
INPUT_FILE = "test.text"
|
import re
|
||||||
|
|
||||||
|
with open('input.text', 'r') as file:
|
||||||
def main():
|
|
||||||
with open(INPUT_FILE, 'r') as file:
|
|
||||||
data: list[str] = file.readlines()
|
data: list[str] = file.readlines()
|
||||||
|
|
||||||
for line in data:
|
# Parse Input
|
||||||
record, groups = line.split()
|
|
||||||
|
|
||||||
groups: list[int] = [int(g) for g in groups.split(',')]
|
|
||||||
|
|
||||||
# for group in groups:
|
|
||||||
|
|
||||||
# # if '#' * group in record:
|
|
||||||
# # groups.remove(group)
|
|
||||||
# # record = record.replace('#' * group, '')
|
|
||||||
|
|
||||||
print(f'{record} {groups}')
|
|
||||||
|
|
||||||
|
|
||||||
|
records = []
|
||||||
|
groups = []
|
||||||
|
for line in data:
|
||||||
|
|
||||||
|
record, group = line.split(' ')
|
||||||
|
|
||||||
|
group = [int(n) for n in group.split(',')]
|
||||||
|
records.append(record)
|
||||||
|
groups.append(group)
|
||||||
|
|
||||||
|
# Part 1
|
||||||
|
|
||||||
|
# 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()
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user