Parcourir la source

Able to tilt north

Piotr Czajkowski il y a 1 an
Parent
commit
2fafb69fcc
1 fichiers modifiés avec 25 ajouts et 1 suppressions
  1. 25 1
      14/code.go

+ 25 - 1
14/code.go

@@ -28,6 +28,30 @@ func readInput(file *os.File) [][]byte {
 	return platform
 }
 
+func tiltNorth(platform [][]byte) [][]byte {
+	newPlatform := [][]byte{platform[0]}
+	height := len(platform)
+	for i := 1; i < height; i++ {
+		newPlatform = append(newPlatform, platform[i])
+		for x := range newPlatform[i] {
+			if newPlatform[i][x] == 'O' {
+				y := i - 1
+				for {
+					if y < 0 || newPlatform[y][x] == '#' || newPlatform[y][x] == 'O' {
+						break
+					}
+
+					newPlatform[y+1][x] = '.'
+					newPlatform[y][x] = 'O'
+					y--
+				}
+			}
+		}
+	}
+
+	return newPlatform
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -41,5 +65,5 @@ func main() {
 	}
 
 	platform := readInput(file)
-	fmt.Println(platform)
+	fmt.Println(tiltNorth(platform))
 }