mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-11-17 02:23:23 +01:00
day 3
This commit is contained in:
parent
33ea3df0ef
commit
2eeba61c6a
11
2021/03/README.md
Normal file
11
2021/03/README.md
Normal 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
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
90
2021/03/python/main.py
Normal 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
12
2021/03/test.txt
Normal file
@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
Loading…
Reference in New Issue
Block a user