 NicholeMattera
/
AOC-2021-Solutions

### ✨Added Day 3 Part 2 Solution

main
parent 453527c66b
commit 093de781e4
2 changed files with 60 additions and 17 deletions
1. 2
 `@ -48,7 +48,7 @@ Both the oxygen generator rating and the CO2 scrubber rating are values that can` `The **bit criteria** depends on which type of rating value you want to find:` ``` ``` `- To find **oxygen generator rating**, determine the **most common** value (`0` or `1`) in the current bit position, and keep only numbers with that bit in that position. If `0` and `1` are equally common, keep values with a `1` in the position being considered.` `- To find **CO2 scrubber rating**, determine the least common value (`0` or `1`) in the current bit position, and keep only numbers with that bit in that position. If `0` and `1` are equally common, keep values with a `0` in the position being considered.` `- To find **CO2 scrubber rating**, determine the **least common** value (`0` or `1`) in the current bit position, and keep only numbers with that bit in that position. If `0` and `1` are equally common, keep values with a `0` in the position being considered.` ``` ``` `For example, to determine the **oxygen generator rating** value using the same example diagnostic report from above:` ``` ```
 `@ -12,9 +12,38 @@ func powInt(x, y int) int {` ` return int(math.Pow(float64(x), float64(y)))` `}` ``` ``` `func filter(slice []int, test func(int, int) bool, bitPosition int) (results []int) {` `func getCommonBitValues(reports []int, bitPosition int) (int, int) {` ` bitCounts := 0` ``` for _, report := range reports { ``` ` power := powInt(2, bitPosition)` ` if power & report == power {` ` bitCounts += 1` ` }` ` }` ``` ``` ` mcv := 0` ` lcv := 0` ``` ``` ``` // Most Common Value ``` ` if bitCounts >= len(reports) / 2 {` ` mcv = 1` ` } else {` ` mcv = 0` ` }` ``` ``` ``` // Least Common Value ``` ` if bitCounts >= len(reports) / 2 {` ` lcv = 0` ` } else {` ` lcv = 1` ` }` ``` ``` ` return mcv, lcv` `}` ``` ``` `func filter(slice []int, test func(int, int, int) bool, bitPosition int, bitValue int) (results []int) {` ` for _, element := range slice {` ` if test(element, bitPosition) {` ` if test(element, bitPosition, bitValue) {` ` results = append(results, element)` ` }` ` }` `@ -22,23 +51,11 @@ func filter(slice []int, test func(int, int) bool, bitPosition int) (results []i` `}` ``` ``` `func partOne(reports []int, numberOfBits int) {` ` bitCounts := []int {}` ` gammaRate := 0` ` epsilonRate := 0` ``` ``` ` for i := 0; i < numberOfBits; i++ {` ``` for _, report := range reports { ``` ` if len(bitCounts) <= i {` ` bitCounts = append(bitCounts, 0)` ` }` ``` ``` ` power := powInt(2, i)` ` if power & report == power {` ` bitCounts[i] += 1` ` }` ` }` ``` ``` ` if bitCounts[i] > len(reports) / 2 {` ` if mcv, _ := getCommonBitValues(reports, i); mcv == 1 {` ` gammaRate += powInt(2, i)` ` } else {` ` epsilonRate += powInt(2, i)` `@ -49,7 +66,33 @@ func partOne(reports []int, numberOfBits int) {` `}` ``` ``` `func partTwo(reports []int, numberOfBits int) {` ` fmt.Printf("Part Two Answer: %d\n", 0)` ` ogrFilteredReports := reports` ` co2srFilteredReports := reports` ``` ``` ` filterTest := func (report, bitPosition, bitValue int) bool {` ` power := powInt(2, bitPosition)` ` return (bitValue == 1 && power & report == power) || (bitValue == 0 && power & report != power)` ` }` ``` ``` ` for i := numberOfBits - 1; i >= 0; i-- {` ` mcv, _ := getCommonBitValues(ogrFilteredReports, i)` ``` ``` ` if len(ogrFilteredReports) > 1 {` ` ogrFilteredReports = filter(ogrFilteredReports, filterTest, i, mcv)` ` }` ``` ``` ` _, lcv := getCommonBitValues(co2srFilteredReports, i)` ``` ``` ` if len(co2srFilteredReports) > 1 {` ` co2srFilteredReports = filter(co2srFilteredReports, filterTest, i, lcv)` ` }` ``` ``` ` if len(ogrFilteredReports) <= 1 && len(co2srFilteredReports) <= 1 {` ` break` ` }` ` }` ``` ``` ` fmt.Printf("Part Two Answer: %d\n", ogrFilteredReports * co2srFilteredReports)` `}` ``` ``` `func main() {`