From 078c98ee1d886f4f290362d813843567cfd7b441 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Mon, 21 Dec 2020 22:21:07 +0100 Subject: [PATCH] Day 10 of 2020 solved in Python --- 2020/Day 10/Solution.py | 68 +++++++++++++++++++++++++ 2020/Day 10/input.txt | 110 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 2020/Day 10/Solution.py create mode 100644 2020/Day 10/input.txt diff --git a/2020/Day 10/Solution.py b/2020/Day 10/Solution.py new file mode 100644 index 0000000..bb0a10e --- /dev/null +++ b/2020/Day 10/Solution.py @@ -0,0 +1,68 @@ +input_file = "input.txt" + +adapters = [] + +with open(input_file) as adapter_ratings: + line = adapter_ratings.readline() + + while line and line != "\n": + adapters.append(int(line)) + line = adapter_ratings.readline() + +adapters.sort() +computer_adapter = max(adapters) + 3 + +# List of couples separated by three, which must be present to have a working chain +fixed_points = [] + +one_diffs = 0 +three_diffs = 1 # As the computer adapter is always the highest one we got plus three + +# The charging outlet is 0, compute the difference now +if adapters[0] == 1: + one_diffs += 1 +elif adapters[0] == 3: + fixed_points.append((adapters[0], 0)) + three_diffs += 1 + +for index in range(len(adapters)-1): + current_difference = adapters[index+1] - adapters[index] + if current_difference == 1: + one_diffs += 1 + elif current_difference == 3: + if (adapters[index], index) not in fixed_points: + fixed_points.append((adapters[index], index)) + fixed_points.append((adapters[index+1], index+1)) + three_diffs += 1 + +print(f"The product of one and three differences is : {one_diffs}*{three_diffs} = {one_diffs*three_diffs}") + +# The last adapter is always needed as it is the only link possible to the computer adapter +if (adapters[-1], len(adapters)-1) not in fixed_points: + fixed_points.append((adapters[-1], len(adapters)-1)) + +# Compute the distance separating each fixed point. If they are not successive, it means that there are possible +# permutations between those two fixed points. Store the count of numbers we can choose from + +separation_distance = [] +for index in range(len(fixed_points) - 1): + if fixed_points[index + 1][1] - fixed_points[index][1] > 1: + separation_distance.append(fixed_points[index + 1][1] - fixed_points[index][1] - 1) + +# Distance between 0 and the first fixed point +separation_distance.insert(0, fixed_points[0][1]) + +total_combinations = 1 + +# This would probably not work in a general case other than this puzzle, as I only take into account small possible +# separations, which is all I have got. +for separation in separation_distance: + # If we have three numbers, it means that we have at least a separation of 4 jolts between the two fixed points, + # which is not possible to cross if we do not choose any number between those two. So, do not count this choice. + if separation == 3: + # Number of subsets of a set is 2^n where n is the number of elements in the set. + total_combinations *= 2**3 - 1 + else: + total_combinations *= 2**separation + +print(f"The number of combinations is : {total_combinations}") diff --git a/2020/Day 10/input.txt b/2020/Day 10/input.txt new file mode 100644 index 0000000..46ad475 --- /dev/null +++ b/2020/Day 10/input.txt @@ -0,0 +1,110 @@ +160 +34 +123 +159 +148 +93 +165 +56 +179 +103 +171 +44 +110 +170 +147 +98 +25 +37 +137 +71 +5 +6 +121 +28 +19 +134 +18 +7 +66 +90 +88 +181 +89 +41 +156 +46 +8 +61 +124 +9 +161 +72 +13 +172 +111 +59 +105 +51 +109 +27 +152 +117 +52 +68 +95 +164 +116 +75 +78 +180 +81 +47 +104 +12 +133 +175 +16 +149 +135 +99 +112 +38 +67 +53 +153 +2 +136 +113 +17 +145 +106 +31 +45 +169 +146 +168 +26 +36 +118 +62 +65 +142 +130 +1 +140 +84 +94 +141 +122 +22 +48 +102 +60 +178 +127 +73 +74 +87 +182 +35