Move copy of array in filterNumbersPart2

This commit is contained in:
Karl Spickermann 2021-12-03 23:16:30 +01:00
parent 6491807ab8
commit 2d2a5e7bd6

@ -27,8 +27,8 @@ func main() {
} }
func part2(intArray [][]int) { func part2(intArray [][]int) {
oxygenGeneratorRating, _ := filterNumbersPart2(append([][]int(nil), intArray...), 0) oxygenGeneratorRating, _ := filterNumbersPart2(intArray, 0)
co2ScrubberRating, _ := filterNumbersPart2(append([][]int(nil), intArray...), 1) co2ScrubberRating, _ := filterNumbersPart2(intArray, 1)
fmt.Println(co2ScrubberRating * oxygenGeneratorRating) fmt.Println(co2ScrubberRating * oxygenGeneratorRating)
} }
@ -42,21 +42,23 @@ func part1(intArray [][]int) {
} }
func filterNumbersPart2(intArray [][]int, invert int) (int64, error){ func filterNumbersPart2(intArray [][]int, invert int) (int64, error){
gammaArray := getGammaArray(intArray, invert) intArrayCopy := make([][]int, len(intArray))
copy(intArrayCopy, intArray)
gammaArray := getGammaArray(intArrayCopy, invert)
i := 0 i := 0
for len(intArray) > 1 { for len(intArrayCopy) > 1 {
n := 0 n := 0
for _, number := range intArray { for _, number := range intArrayCopy {
if number[i] == gammaArray[i] { if number[i] == gammaArray[i] {
intArray[n] = number intArrayCopy[n] = number
n++ n++
} }
} }
intArray = intArray[:n] intArrayCopy = intArrayCopy[:n]
gammaArray = getGammaArray(intArray, invert) gammaArray = getGammaArray(intArrayCopy, invert)
i++ i++
} }
s, _ := json.Marshal(intArray[0]) s, _ := json.Marshal(intArrayCopy[0])
intString := strings.Replace(strings.Trim(string(s), "[]"),",","",-1) intString := strings.Replace(strings.Trim(string(s), "[]"),",","",-1)
return strconv.ParseInt(intString, 2, 64) return strconv.ParseInt(intString, 2, 64)
} }