Browse Source

Solved for sample

Piotr Czajkowski 1 year ago
parent
commit
d00ce92e3b
1 changed files with 11 additions and 3 deletions
  1. 11 3
      11/code.go

+ 11 - 3
11/code.go

@@ -64,8 +64,16 @@ func distances(universe Universe, expanse float64) int {
 				}
 			}
 
-			for x := universe.galaxies[i].x + 1; x <= universe.galaxies[j].x; x++ {
-				if universe.occupiedColumns[x] {
+			start := universe.galaxies[i].x + 1
+			end := universe.galaxies[j].x
+			if start > end {
+				start, end = end, start
+				start++
+				end--
+			}
+
+			for ; start <= end; start++ {
+				if universe.occupiedColumns[start] {
 					distance++
 				} else {
 					distance += multiple
@@ -92,5 +100,5 @@ func main() {
 	}
 
 	universe := readInput(file)
-	fmt.Println("Part1:", distances(universe, 2))
+	fmt.Println("Part1:", distances(universe, 1))
 }