From 6b40338dd3ffb82a58e997e289bed14d3f6df396 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Sat, 4 Dec 2021 22:56:24 +0000 Subject: [PATCH] Finish day 3 challenge 1 --- 3_1.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 3_1.py diff --git a/3_1.py b/3_1.py new file mode 100644 index 0000000..7b0e1d8 --- /dev/null +++ b/3_1.py @@ -0,0 +1,35 @@ +""" Calculate power consumption""" + +def most_common(digits): + ones = 0 + for d in digits: + if d == '1': + ones += 1 + + if ones > len(digits)/2: + return 1 + else: + return 0 + + +with open("3_1_input.txt") as f: + lines = [l.strip() for l in f] + +digits = list(zip(*lines)) +gamma_rate = [] +epsilon_rate = [] + +for col in digits: + mc = most_common(col) + gamma_rate.append(str(mc)) + epsilon_rate.append(str((mc + 1) % 2)) + +gamma = int("".join(gamma_rate), base=2) +epsilon = int("".join(epsilon_rate), base=2) + + + +print(gamma_rate, gamma) +print(epsilon_rate, epsilon) + +print(gamma * epsilon) -- 2.46.2