Browse Source

Let's play a game

Piotr Czajkowski 3 năm trước cách đây
mục cha
commit
aee595a242
1 tập tin đã thay đổi với 28 bổ sung1 xóa
  1. 28 1
      day22/day22.go

+ 28 - 1
day22/day22.go

@@ -48,6 +48,33 @@ func readFile(file *os.File) [2][]int {
 	return decks
 }
 
+func play(decks [2][]int) []int {
+	for {
+		if len(decks[0]) == 0 || len(decks[1]) == 0 {
+			break
+		}
+
+		player1Hand := decks[0][0]
+		decks[0] = decks[0][1:len(decks[0])]
+
+		player2Hand := decks[1][0]
+		decks[1] = decks[1][1:len(decks[1])]
+
+		if player1Hand > player2Hand {
+			decks[0] = append(decks[0], player1Hand)
+			decks[0] = append(decks[0], player2Hand)
+		} else {
+			decks[1] = append(decks[1], player2Hand)
+			decks[1] = append(decks[1], player1Hand)
+		}
+	}
+
+	if len(decks[0]) == 0 {
+		return decks[1]
+	}
+	return decks[0]
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -65,5 +92,5 @@ func main() {
 		log.Fatalf("Failed to close file: %s", err)
 	}
 
-	fmt.Println(decks)
+	fmt.Println(play(decks))
 }