Files
aocpy/2020/d10.py

58 lines
945 B
Python

from lib import get_data
from collections import defaultdict
from functools import lru_cache
@lru_cache
def count(xs, current, target):
delta_target = target - current
if len(xs) == 0:
if delta_target <= 3:
return 1
else:
return 0
x = xs[0]
xs = xs[1:]
total = 0
delta = x - current
if delta > 3:
return 0
if delta <= 3:
total += count(xs, x, target)
total += count(xs, current, target)
return total
def part_1(data):
xs = list(map(int, data.strip().splitlines()))
out = max(xs) + 3
ds = defaultdict(int)
c = 0
for x in sorted(xs):
d = x - c
ds[d] += 1
c = x
d = out - c
ds[d] += 1
a, b = list(ds.values())
print(a * b)
xs = tuple(sorted(xs))
print(count(xs, 0, out))
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()