advent_of_code/2024/day19/main.py

59 lines
1.2 KiB
Python
Raw Permalink Normal View History

from re import sub
import sys
import os
def check_matchp1(p, t):
if p=='':
return False
for ts in t:
if p == ts:
return True
else:
if p[:len(ts)] == ts[:len(p)]:
if check_matchp1(p[len(ts):], t):
return True
return False
2024-12-19 11:37:27 -05:00
G = {}
def match_count(p, t):
count = 0
2024-12-19 11:37:27 -05:00
if p in G:
return G[p]
else:
if p=='':
return count
for ts in t:
if p == ts:
count += 1
else:
if p[:len(ts)] == ts[:len(p)]:
sub_count = match_count(p[len(ts):], t)
count += sub_count
2024-12-19 11:37:27 -05:00
G[p] = count
return count
def part_one(p, t):
2024-12-22 18:10:51 -05:00
p1 = 0
for ps in p:
if check_matchp1(ps, t):
2024-12-22 18:10:51 -05:00
p1 += 1
print("p1: ", p1)
def part_two(p, t):
2024-12-19 11:37:27 -05:00
p2 = 0
for ps in p:
p2 += match_count(ps, t)
print("p2: ", p2)
def main():
2024-12-19 11:37:27 -05:00
f = open('input.txt', 'r')
contents = [x.strip() for x in f.readlines()]
towels = contents[0].split(", ")
patterns = contents[2:]
part_one(patterns, towels)
part_two(patterns, towels)
if __name__ == "__main__":
main()