view the Advent of Code 2021 Day 3 challenge
# Setup - load any packages
import pandas as pd
# load data into Pandas DataFrame using fixed width
data_raw = pd.read_fwf('day3_data.txt', names=['a','b','c','d','e','f','g', 'h', 'i', 'j', 'k', 'l'], index_col=False, header=None, widths=[1,1,1,1,1,1,1,1,1,1,1,1])
# Check the data types of the columns
print(data_raw.dtypes)
# View the first few rows of the data
data_raw.head()
a int64 b int64 c int64 d int64 e int64 f int64 g int64 h int64 i int64 j int64 k int64 l int64 dtype: object
a | b | c | d | e | f | g | h | i | j | k | l | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 |
2 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
3 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
4 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 |
# copy the raw data
part1=data_raw.copy()
# declare variables as string
gamma =""
epsilon =""
# sum the columns
p1_totals = part1.sum()
#loop through each column sum
for column in p1_totals:
# if column sum is more than half the number of rows then there are more ones
if column >= (part1.shape[0]/2):
#more ones
gamma = gamma + "1"
epsilon = epsilon + "0"
else:
#more zeros
gamma = gamma + "0"
epsilon = epsilon + "1"
#view binary values
print(f'gamma = {gamma} ({int(gamma, 2)})')
print(f'epsilon = {epsilon} ({int(epsilon, 2)})')
# convert to integers and multiply
p1_answer = int(gamma, 2) * int(epsilon, 2)
print(f'answer = {p1_answer}')
gamma = 011101001101 (1869) epsilon = 100010110010 (2226) answer = 4160394
#find most common binary value in a column
def mostcommon(pd_col):
if sum(pd_col)>=(pd_col.shape[0]/2):
return "1"
else:
return "0"
# copy the raw data
part1=data_raw.copy()
# declare variables as string
gamma =""
epsilon =""
for i in range(part1.shape[1]):
# if column sum is more than half the number of rows then there are more ones
if mostcommon(part1.iloc[:,i])=="1":
#more ones
gamma = gamma + "1"
epsilon = epsilon + "0"
else:
#more zeros
gamma = gamma + "0"
epsilon = epsilon + "1"
#view binary values
print(f'gamma = {gamma} ({int(gamma, 2)})')
print(f'epsilon = {epsilon} ({int(epsilon, 2)})')
# convert to integers and multiply
p1_answer = int(gamma, 2) * int(epsilon, 2)
print(f'answer = {p1_answer}')
gamma = 011101001101 (1869) epsilon = 100010110010 (2226) answer = 4160394
# copy the raw data
part2=data_raw.copy()
def findReading(sensordata, leastcommon=False):
#copy the data as we will be filtering it
sensordata_temp=sensordata.copy()
#loop through each binary digit
for i in range(sensordata_temp.shape[1]):
# get most common value in column
most=int(mostcommon(sensordata_temp.iloc[:,i]))
if leastcommon==True:
#flip the most common value from 0->1 or 1->0
most = 1 - most
#subset the data by the most common value in the column
sensordata_temp=sensordata_temp[sensordata_temp.iloc[:, i]==most]
#if only one value then return it and exit
if sensordata_temp.shape[0]==1:
#Concatenate the columns together to get the binary value
sensordata_temp['reading'] = sensordata_temp[sensordata_temp.columns[:]].apply(
lambda x: ''.join(x.dropna().astype(str)),
axis=1)
return sensordata_temp.iloc[0,12]
oxygen = findReading(part2, False)
CO2=findReading(part2, True)
#view binary values
print(f'oxygen = {oxygen} ({int(oxygen, 2)})')
print(f'CO2 = {CO2} ({int(CO2, 2)})')
# convert to integers and multiply
p2_answer = int(oxygen, 2) * int(CO2, 2)
print(f'answer = {p2_answer}')
oxygen = 011010110111 (1719) CO2 = 100101100000 (2400) answer = 4125600