From d2abd31795bb7abfd455ccac4dd16de80d5faf19 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 10 Dec 2020 07:29:28 +0100 Subject: [PATCH] 2020: d10: ex2: add solution --- 2020/d10/ex2/ex2.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 2020/d10/ex2/ex2.py diff --git a/2020/d10/ex2/ex2.py b/2020/d10/ex2/ex2.py new file mode 100755 index 0000000..6af091e --- /dev/null +++ b/2020/d10/ex2/ex2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +import sys +from typing import Dict, List + + +def make_chain(adapters: List[int]) -> int: + adapters += [0, max(adapters) + 3] + adapters = sorted(adapters) + jolts: Dict[int, int] = {} + N = len(adapters) + + def rec(index: int) -> int: + if (res := jolts.get(index)) is not None: + return res + if index == N - 1: + return 1 + total = sum( + rec(i) + for i in range(index + 1, min(N, index + 4)) + if (adapters[i] - adapters[index]) <= 3 + ) + jolts[index] = total + return total + + return rec(0) + + +def solve(raw: List[str]) -> int: + return make_chain([int(line) for line in raw]) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()