Part 1

You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate. Each bit in the gamma rate can be determined by finding the most common bit in the corresponding position of all numbers in the diagnostic report. For example, given the following diagnostic report: 00100 11110 10110 10111 10101 01111 00111 11100 10000 11001 00010 01010 Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1. The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0. The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110. So, the gamma rate is the binary number 10110, or 22 in decimal. The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or 9 in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, 198. Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the submarine? (Be sure to represent your answer in decimal, not binary.)

## Reading the data

import pandas as pd df = pd.read_csv("./aoc_day_03_test_data.txt", dtype = str, header=None) df.columns = ["original"]

We need to define the input data as string via dtype = str. Otherwise it will be treated as integer and we loose the leading zeroes.

original | |
---|---|

0 | 00100 |

1 | 11110 |

2 | 10110 |

3 | 10111 |

4 | 10101 |

5 | 01111 |

6 | 00111 |

7 | 11100 |

8 | 10000 |

9 | 11001 |

10 | 00010 |

11 | 01010 |

Now we can split the strings into individual bits with the split() function.

To generate a new column for every bit we use the tolist() function. The first and the last column is empty, so let’s remove them.

df = pd.DataFrame(df["original"].str.split('').tolist()) del df[0] df = df.iloc[:,:-1] df

0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|

0 | 0 | 0 | 1 | 0 | 0 |

1 | 1 | 1 | 1 | 1 | 0 |

2 | 1 | 0 | 1 | 1 | 0 |

3 | 1 | 0 | 1 | 1 | 1 |

4 | 1 | 0 | 1 | 0 | 1 |

5 | 0 | 1 | 1 | 1 | 1 |

6 | 0 | 0 | 1 | 1 | 1 |

7 | 1 | 1 | 1 | 0 | 0 |

8 | 1 | 0 | 0 | 0 | 0 |

9 | 1 | 1 | 0 | 0 | 1 |

10 | 0 | 0 | 0 | 1 | 0 |

11 | 0 | 1 | 0 | 1 | 0 |

Now we have to look which value appears the most in each column.

df = pd.concat([df[column].value_counts() for column in df], axis = 1) df

1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|

1 | 7 | 5 | 8 | 7 | 5 |

0 | 5 | 7 | 4 | 5 | 7 |

The most common bits are

most_common_bits = df.idxmax() most_common_bits

1 1 2 0 3 1 4 1 5 0 dtype: object

Now we have to concatenate these bits to one string and convert it back into an integer

gamma_rate = ''.join(most_common_bits) gamma_rate = int(gamma_rate, 2) gamma_rate

That gives us the gamma rate of 22

The epsilon rate can be calculated by inverting all significant bits in the most_common_bits value. We do this by creating a bit mask

bit_mask = 2 ** len(most_common_bits) - 1

and applying it with an XOR to the gamma rate:

epsilon_rate = gamma_rate ^ bit_mask epsilon_rate

The solution is:

gamma_rate * epsilon_rate