This commit is contained in:
cupcakearmy 2021-12-03 08:58:31 +01:00
parent 33ea3df0ef
commit 2eeba61c6a
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
4 changed files with 1114 additions and 0 deletions

11
2021/03/README.md Normal file
View File

@ -0,0 +1,11 @@
# 03
Description
<details>
<summary>Solutions</summary>
<ol>
<li>Gamma: 3903, Epsilon: 192, Power Consumption: 749376</li>
<li>Oxygen: 3871, CO2: 613, Life Support: 2372923</li>
</ol>
</details>

1001
2021/03/input.txt Normal file

File diff suppressed because it is too large Load Diff

90
2021/03/python/main.py Normal file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env python
from os import O_EXCL
from os.path import join, dirname
from typing import Tuple
# Day 03
# Common
def read_input(filename):
data = join(dirname(__file__), '..', filename)
with open(data) as f:
return f.read().strip()
test = read_input('test.txt')
data = read_input('input.txt')
# 1
def count_bits(input: str) -> Tuple[int, int]:
gamma = 0
epsilon = 0
lines = input.split('\n')
size = len(lines[0])
for x in range(size):
zeros = 0
ones = 0
for line in lines:
if line[x] == '0':
zeros += 1
else:
ones += 1
incr = 2**(size - x - 1)
if zeros < ones:
gamma += incr
else:
epsilon += incr
return gamma, epsilon
print('1.')
gamma, epsilon = count_bits(test)
print(
f"Test: Gamma: {gamma}, Epsilon: {epsilon}, Power Consumption: {gamma * epsilon}")
gamma, epsilon = count_bits(data)
print(
f"Data: Gamma: {gamma}, Epsilon: {epsilon}, Power Consumption: {gamma * epsilon}")
# 2
def extract(input: str, bigger=True) -> int:
lines = input.split('\n')
size = len(lines[0])
for x in range(size):
zeros = 0
ones = 0
for line in lines:
if line[x] == '0':
zeros += 1
else:
ones += 1
if zeros == ones:
filter = '1' if bigger else '0'
elif bigger:
filter = '0' if zeros > ones else '1'
else:
filter = '0' if zeros < ones else '1'
lines = [
line for line in lines
if line[x] == filter
]
if len(lines) == 1:
return int(lines[0], 2)
raise Exception('No solution found')
print('\n2.')
oxygen = extract(test, True)
co2 = extract(test, False)
life_support = oxygen * co2
print(f"Test: Oxygen: {oxygen}, CO2: {co2}, Life Support: {life_support}")
oxygen = extract(data, True)
co2 = extract(data, False)
life_support = oxygen * co2
print(f"Data: Oxygen: {oxygen}, CO2: {co2}, Life Support: {life_support}")

12
2021/03/test.txt Normal file
View File

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010