From: Maximilian Friedersdorff Date: Sat, 4 Dec 2021 22:56:24 +0000 (+0000) Subject: Finish day 3 challenge 1 X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=6b40338dd3ffb82a58e997e289bed14d3f6df396;p=max%2Fadvent_of_code_2021.git Finish day 3 challenge 1 --- 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)