This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
66 lines (52 loc) · 1.85 KB
/
day3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from aocd import get_data
from dotenv import load_dotenv
def get_session() -> str:
load_dotenv()
return os.getenv('SESSION_COOKIE')
def get_list(data=None, day=None, year=None):
if not data:
aoc_input = [line.rstrip() for line in get_data(get_session(), day=day, year=year).split('\n')]
else:
aoc_input = [line.rstrip() for line in data.splitlines()]
return aoc_input
def get_power_consumption(input_list):
gamma, epsilon = '', ''
for i in range(len(input_list[0])):
counter = {'0': 0, '1': 0}
for row in input_list:
counter[row[i]] += 1
if counter['0'] > counter['1']:
gamma += '0'
epsilon += '1'
else:
gamma += '1'
epsilon += '0'
return int(gamma, 2) * int(epsilon, 2)
def get_num(input_list, type_of_gas):
input_list_copy = input_list.copy()
for i in range(0, len(input_list[0])):
one, zero = (0, 0)
bit_positions = [row[i] for row in input_list_copy]
one = bit_positions.count('1')
zero = bit_positions.count('0')
if type_of_gas == 'o2':
if one >= zero:
bit = '1'
else:
bit = '0'
else:
if one >= zero:
bit = '0'
else:
bit = '1'
req_numbers = [row for row in input_list_copy if row[i] == bit]
if len(req_numbers) == 1: # Keep going until we have one
return int(req_numbers[0], 2)
else:
input_list_copy = req_numbers
if __name__ == '__main__':
print(f'Part 1: Power consumption: {get_power_consumption(get_list(data=None, day=3, year=2021))}')
print(
f'Part 2: Life support rating = \
{get_num(get_list(data=None, day=3, year=2021), "o2") * get_num(get_list(data=None, day=3, year=2021), "co2")}')