My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2024/19

+33
+33
2024/day19.py
··· 1 + import fileinput 2 + import functools 3 + 4 + 5 + @functools.lru_cache() 6 + def num_designs(towel, patterns): 7 + poss = 0 8 + 9 + for pattern in patterns: 10 + l = len(pattern) 11 + if towel == pattern: 12 + poss += 1 13 + elif towel[:l] == pattern: 14 + sub_poss = num_designs(towel[l:], patterns) 15 + if sub_poss: 16 + poss += sub_poss 17 + 18 + return poss 19 + 20 + 21 + # Read problem input. 22 + TOWELS = [] 23 + for line in fileinput.input(): 24 + if ',' in line: 25 + PATTERNS = frozenset(line.strip().split(', ')) 26 + elif line.strip(): 27 + TOWELS.append(line.strip()) 28 + 29 + 30 + # Solve problem. 31 + designs = [num_designs(towel, PATTERNS) for towel in TOWELS] 32 + print("Part 1:", sum(1 for d in designs if d)) 33 + print("Part 2:", sum(designs))