diff --git a/2020/day10-adapter-array.py b/2020/day10-adapter-array.py new file mode 100644 index 0000000..b140a53 --- /dev/null +++ b/2020/day10-adapter-array.py @@ -0,0 +1,42 @@ +import copy +import itertools +from typing import List + + +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + jolts = sorted(map(int, f.read().strip().split("\n"))) # type: List[int] + + jolts.append(jolts[-1] + 3) + + counter_part_1 = solve_part_1(jolts) + counter_part_2 = 0 + + print(f"1. Found {counter_part_1}") + if expected_part_1: + assert expected_part_1 == counter_part_1 + + print(f"2. Found {counter_part_2}") + if expected_part_2: + assert expected_part_2 == counter_part_2 + + +def solve_part_1(jolts): + diffs = count_diffs(jolts) + return diffs[1] * diffs[3] + + +def count_diffs(jolts): + diffs = {1: 0, 2: 0, 3: 0} + previous = 0 + for jolt in jolts: + diffs[jolt - previous] += 1 + previous = jolt + return diffs + + +if __name__ == "__main__": + main("inputs/day10-test1", 35, 8) + main("inputs/day10-test2", 220, 19208) + main("inputs/day10", 2450) diff --git a/2020/inputs/day10 b/2020/inputs/day10 new file mode 100644 index 0000000..c472cf1 --- /dev/null +++ b/2020/inputs/day10 @@ -0,0 +1,104 @@ +2 +49 +78 +116 +143 +42 +142 +87 +132 +86 +67 +44 +136 +82 +125 +1 +108 +123 +46 +37 +137 +148 +106 +121 +10 +64 +165 +17 +102 +156 +22 +117 +31 +38 +24 +69 +131 +144 +162 +63 +171 +153 +90 +9 +107 +79 +7 +55 +138 +34 +52 +77 +152 +3 +158 +100 +45 +129 +130 +135 +23 +93 +96 +103 +124 +95 +8 +62 +39 +118 +164 +172 +75 +122 +20 +145 +14 +112 +61 +43 +141 +30 +85 +101 +151 +29 +113 +94 +68 +58 +76 +97 +28 +111 +128 +21 +11 +163 +161 +4 +168 +157 +27 +72 diff --git a/2020/inputs/day10-test1 b/2020/inputs/day10-test1 new file mode 100644 index 0000000..cd1b40b --- /dev/null +++ b/2020/inputs/day10-test1 @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 \ No newline at end of file diff --git a/2020/inputs/day10-test2 b/2020/inputs/day10-test2 new file mode 100644 index 0000000..be5c492 --- /dev/null +++ b/2020/inputs/day10-test2 @@ -0,0 +1,31 @@ +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 \ No newline at end of file