From 9bc536f6ec2d0bd7c6eea32b80ac1605a4be62ce Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 10 Dec 2020 07:29:12 +0100 Subject: [PATCH] 2020: d10: ex1: add solution --- 2020/d10/ex1/ex1.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 2020/d10/ex1/ex1.py diff --git a/2020/d10/ex1/ex1.py b/2020/d10/ex1/ex1.py new file mode 100755 index 0000000..e75dd5c --- /dev/null +++ b/2020/d10/ex1/ex1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +import sys +from collections import defaultdict +from typing import Dict, List + + +def make_chain(adapters: List[int]) -> int: + adapters = sorted(adapters) + output = 0 + device = adapters[-1] + 3 + jolts: Dict[int, int] = defaultdict(int) + for adapter in adapters: + delt = adapter - output + jolts[delt] += 1 + output = adapter + jolts[device - output] += 1 + + return jolts[1] * jolts[3] + + +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()