diff --git a/2020-python/inputs/day_10 b/2020-python/inputs/day_10 new file mode 100644 index 0000000..a68e236 --- /dev/null +++ b/2020-python/inputs/day_10 @@ -0,0 +1,108 @@ +115 +134 +121 +184 +78 +84 +77 +159 +133 +90 +71 +185 +152 +165 +39 +64 +85 +50 +20 +75 +2 +120 +137 +164 +101 +56 +153 +63 +70 +10 +72 +37 +86 +27 +166 +186 +154 +131 +1 +122 +95 +14 +119 +3 +99 +172 +111 +142 +26 +82 +8 +31 +53 +28 +139 +110 +138 +175 +108 +145 +58 +76 +7 +23 +83 +49 +132 +57 +40 +48 +102 +11 +105 +146 +149 +66 +38 +155 +109 +128 +181 +43 +44 +94 +4 +169 +89 +96 +60 +69 +9 +163 +116 +45 +59 +15 +178 +34 +114 +17 +16 +79 +91 +100 +162 +125 +156 +65 diff --git a/2020-python/solutions/day_10.py b/2020-python/solutions/day_10.py new file mode 100644 index 0000000..18d27e7 --- /dev/null +++ b/2020-python/solutions/day_10.py @@ -0,0 +1,111 @@ +"""AOC 2020 Day 10""" + +import pathlib +import time + +TEST_INPUT = """16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4""" + +TEST_INPUT_2 = """28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3""" + +def read_input(input_path: str) -> str: + """take input file path and return a str with the file's content""" + with open(input_path, 'r') as input_file: + input_data = input_file.read().strip() + return input_data + +def extract(input_data: str) -> list: + """take input data and return the appropriate data structure""" + entries = [0] + for entry in input_data.split('\n'): + entries.append(int(entry)) + entries = sorted(entries) + entries.append(entries[-1]+3) + return entries + +def part1(entries: list) -> int: + """part1 solver""" + jolt_diff_1, jolt_diff_3 = 0, 0 + for index in range(len(entries)-1): + diff = entries[index+1] - entries[index] + if diff == 1: + jolt_diff_1 += 1 + elif diff == 3: + jolt_diff_3 += 1 + return jolt_diff_1*jolt_diff_3 + +def part2(entries: list) -> int: + """part2 solver""" + distinct_paths = [0]*(entries[-1]+1) + distinct_paths[0] = 1 + + for adapter in entries: + distinct_paths[adapter] += distinct_paths[adapter-1] + distinct_paths[adapter-2] + distinct_paths[adapter-3] + + return distinct_paths[len(distinct_paths)-1] + +def test_input_day_10(): + """pytest testing function""" + entries = extract(TEST_INPUT) + assert part1(entries) == 35 + assert part2(entries) == 8 + entries = extract(TEST_INPUT_2) + assert part1(entries) == 220 + assert part2(entries) == 19208 + +def test_bench_day_10(benchmark): + """pytest-benchmark function""" + benchmark(main) + +def main(): + """main function""" + input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem) + start_time = time.time() + input_data = read_input(input_path) + entries = extract(input_data) + print("Part 1: %d" % part1(entries)) + print("Part 2: %d" % part2(entries)) + end_time = time.time() + print("Execution time: %f" % (end_time-start_time)) + +if __name__ == "__main__": + main() diff --git a/README.md b/README.md index 1927ccb..01d9669 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,17 @@ ### Year [2020](https://adventofcode.com/2020/) - Python -| Day | Execution time | -| -------------------------------------------- | -------------------------------------------- | -| [Day 1](https://adventofcode.com/2020/day/1) | [0.04ms](./2020-python/solutions/day_1.py) | -| [Day 2](https://adventofcode.com/2020/day/2) | [0.702ms](./2020-python/solutions/day_2.py) | -| [Day 3](https://adventofcode.com/2020/day/3) | [0.343ms](./2020-python/solutions/day_3.py) | -| [Day 4](https://adventofcode.com/2020/day/4) | [21.806ms](./2020-python/solutions/day_4.py) | -| [Day 5](https://adventofcode.com/2020/day/5) | [5.993ms](./2020-python/solutions/day_5.py) | -| [Day 6](https://adventofcode.com/2020/day/6) | [2.587ms](./2020-python/solutions/day_6.py) | -| [Day 7](https://adventofcode.com/2020/day/7) | [1.689ms](./2020-python/solutions/day_7.py) | -| [Day 8](https://adventofcode.com/2020/day/8) | [6.313ms](./2020-python/solutions/day_8.py) | -| [Day 9](https://adventofcode.com/2020/day/9) | [7.2207ms](./2020-python/solutions/day_9.py) | +| Day | Execution time | +| ---------------------------------------------- | ---------------------------------------------- | +| [Day 1](https://adventofcode.com/2020/day/1) | [0.04ms](./2020-python/solutions/day_1.py) | +| [Day 2](https://adventofcode.com/2020/day/2) | [0.702ms](./2020-python/solutions/day_2.py) | +| [Day 3](https://adventofcode.com/2020/day/3) | [0.343ms](./2020-python/solutions/day_3.py) | +| [Day 4](https://adventofcode.com/2020/day/4) | [21.806ms](./2020-python/solutions/day_4.py) | +| [Day 5](https://adventofcode.com/2020/day/5) | [5.993ms](./2020-python/solutions/day_5.py) | +| [Day 6](https://adventofcode.com/2020/day/6) | [2.587ms](./2020-python/solutions/day_6.py) | +| [Day 7](https://adventofcode.com/2020/day/7) | [1.689ms](./2020-python/solutions/day_7.py) | +| [Day 8](https://adventofcode.com/2020/day/8) | [6.313ms](./2020-python/solutions/day_8.py) | +| [Day 9](https://adventofcode.com/2020/day/9) | [7.220ms](./2020-python/solutions/day_9.py) | +| [Day 10](https://adventofcode.com/2020/day/10) | [139.110ms](./2020-python/solutions/day_10.py) |