Piotr Czajkowski il y a 2 semaines
Parent
commit
9633a6944f
1 fichiers modifiés avec 13 ajouts et 19 suppressions
  1. 13 19
      03/code.go

+ 13 - 19
03/code.go

@@ -28,29 +28,23 @@ func readInput(file *os.File) [][]int {
 	return batteries
 }
 
+func findMaxIndex(slice []int, start, end int) int {
+	maxIndex := start
+	for i := start; i < end; i++ {
+		if slice[i] > slice[maxIndex] {
+			maxIndex = i
+		}
+	}
+
+	return maxIndex
+}
+
 func part1(batteries [][]int) int {
 	var sum int
 
 	for row := range batteries {
-		var maxLeft int
-		for col := 0; col < len(batteries[row])-1; col++ {
-			if batteries[row][col] > batteries[row][maxLeft] {
-				maxLeft = col
-				if batteries[row][maxLeft] == 9 {
-					break
-				}
-			}
-		}
-
-		maxRight := maxLeft + 1
-		for col := maxRight; col < len(batteries[row]); col++ {
-			if batteries[row][col] > batteries[row][maxRight] {
-				maxRight = col
-				if batteries[row][maxRight] == 9 {
-					break
-				}
-			}
-		}
+		maxLeft := findMaxIndex(batteries[row], 0, len(batteries[row])-1)
+		maxRight := findMaxIndex(batteries[row], maxLeft+1, len(batteries[row]))
 
 		sum += batteries[row][maxLeft]*10 + batteries[row][maxRight]
 	}