From 2136bc59e4ef49bc51848aeb976c5db156585aa6 Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Mon, 6 Dec 2021 10:17:06 +0100 Subject: [PATCH] 3rd: remove redundancy --- pkg/three/three.go | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/pkg/three/three.go b/pkg/three/three.go index 785a619..bffc68f 100644 --- a/pkg/three/three.go +++ b/pkg/three/three.go @@ -17,32 +17,18 @@ func Run() { } func calcOxygen(values [][]int) int { - valueStore := make([][][]int, 0) - valueStore = append(valueStore, values) - for i := 0; i < len(values[0]); i++ { - commonValues := getCommonValues(valueStore[i], false) - newValues := make([][]int, 0) - for _, v := range valueStore[i] { - if v[i] == commonValues[i] { - newValues = append(newValues, v) - } - } - - valueStore = append(valueStore, newValues) - - if len(newValues) == 1 { - break - } - } - - return convertToDecimal(valueStore[len(valueStore)-1][0]) + return calcOxygenOrCO2(values, false) } func calcCO2(values [][]int) int { + return calcOxygenOrCO2(values, true) +} + +func calcOxygenOrCO2(values [][]int, getCO2 bool) int { valueStore := make([][][]int, 0) valueStore = append(valueStore, values) for i := 0; i < len(values[0]); i++ { - commonValues := getCommonValues(valueStore[i], true) + commonValues := getCommonValues(valueStore[i], getCO2) newValues := make([][]int, 0) for _, v := range valueStore[i] { if v[i] == commonValues[i] {