advent_of_code/2021/3/3.py
kleph 9598e8f526
All checks were successful
continuous-integration/drone/push Build is passing
archive AoC 2021
2022-11-30 02:08:02 +01:00

40 lines
881 B
Python
Executable file

#!/usr/bin/env python
# 2021 - Advent Of Code - 3
import re
line_count = 0
count = [0] * 12
result = [0] * 12
re_parse = re.compile(r'(\d)'*12)
with open('input.txt', encoding="utf-8") 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}')