func getCommon(data []string, col int) (int, int) {
zero, one := 0, 0
for _, line := range data {
switch line[col] {
case '0':
zero++
case '1':
one++
}
}
return zero, one
}
func bitCriteria(zero, one int, t string) byte {
switch t {
case "oxigen":
if zero > one {
return '0'
} else {
return '1'
}
case "CO2":
if zero > one {
return '1'
} else {
return '0'
}
default:
panic("unreachable")
}
}
func OGR(data []string) string {
for k := 0; k < len(data[0]); k++ {
next := getOxCO2(data, k, "oxigen")
if len(next) == 1 {
return next[0]
}
data = next
}
return "OGR SZOPÓ"
}
func CGR(data []string) string {
for k := 0; k < len(data[0]); k++ {
next := getOxCO2(data, k, "CO2")
if len(next) == 1 {
return next[0]
}
data = next
}
return "CGR SZOPÓ"
}
func getOxCO2(data []string, k int, kind string) []string {
var nextData []string
zero, one := getCommon(data, k)
for _, line := range data {
if line[k] == bitCriteria(zero, one, kind) {
nextData = append(nextData, line)
}
}
return nextData
}