From c879dbd3394528a00a9c68d053719b1b9d9f88ea Mon Sep 17 00:00:00 2001 From: kleph Date: Mon, 6 Dec 2021 02:28:03 +0100 Subject: [PATCH] Day 3 part 1 --- 3/3.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 3/3.py diff --git a/3/3.py b/3/3.py new file mode 100755 index 0000000..4d69060 --- /dev/null +++ b/3/3.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# 2021 - Advent Of Code - 3 + +import re + +line_count = 0 +count = [0 for x in range(12)] +result = [0 for x in range(12)] + +re_parse = re.compile(r'(\d)'*12) + +with open('input.txt') as f: + for line in f: + m = re_parse.match(line) + line_count += 1 + + for i in range(12): + if m.group(i+1) == '1': + count[i] += 1 + +for i in range(12): + if count[i] > line_count / 2: + result[i] = 1 + else: + result[i] = 0 + + +print(count) +invert_result = [0 if x == 1 else 1 for x in result] + +gamma_bin = "".join([str(x) for x in result]) +gamma = int(gamma_bin, 2) + +epsilon_bin = "".join([str(x) for x in invert_result]) +epsilon = int(epsilon_bin, 2) + +print(f'gamma_bin {gamma_bin}, gamma: {gamma}') +print(f'epsilon_bin {epsilon_bin}, epsilon: {epsilon}') + +print(f'power consumption: ({gamma}, {epsilon}) => {gamma * epsilon}')