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 2023/12

+57
+57
2023/day12.py
··· 1 + import fileinput 2 + from utils import memoize 3 + 4 + @memoize 5 + def ways(spring, groups): 6 + # Base cases 7 + if len(spring) == 0: 8 + if not groups: 9 + return 1 10 + else: 11 + return 0 12 + 13 + # trust_the_natural_recursion.jpg 14 + if spring[0] == '#': 15 + if not groups: 16 + return 0 17 + 18 + if any(c == '.' for c in spring[:groups[0]]): 19 + return 0 20 + 21 + if len(spring) == groups[0]: 22 + if len(groups) == 1: 23 + return 1 24 + else: 25 + return 0 26 + 27 + if len(spring) < groups[0]: 28 + return 0 29 + 30 + # If the character after this "group" is a `#`, we have a mismatch. 31 + if spring[groups[0]] == '#': 32 + return 0 33 + 34 + return ways(spring[groups[0]+1:], groups[1:]) 35 + 36 + 37 + elif spring[0] == '.': 38 + return ways(spring[1:], groups) 39 + 40 + elif spring[0] == '?': 41 + return ways('#' + spring[1:], groups) + ways('.' + spring[1:], groups) 42 + 43 + 44 + part_1 = 0 45 + part_2 = 0 46 + 47 + for line in fileinput.input(): 48 + spring, groups = line.strip().split() 49 + groups = tuple(int(n) for n in groups.split(',')) 50 + part_1 += ways(spring, groups) 51 + part_2 += ways('?'.join(spring for _ in range(5)), groups * 5) 52 + 53 + print("Part 1:", part_1) 54 + print("Part 2:", part_2) 55 + 56 + 57 +