From 7a37e28de1a5409d30e31a9e026216269f66c4ce Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sun, 24 May 2026 10:49:40 +0200 Subject: [PATCH] Mastermind en Go --- src/Go/005.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/Go/005.go diff --git a/src/Go/005.go b/src/Go/005.go new file mode 100644 index 0000000..af221d4 --- /dev/null +++ b/src/Go/005.go @@ -0,0 +1,57 @@ +package main + +import "fmt" + +func mastermind(lst_1 []int, lst_2 []int) (int, int) { + indexMatch := 0 + valueMatch := 0 + + for idx, element := range lst_1 { + if lst_1[idx] == lst_2[idx] { + indexMatch++ + continue + } + found := false + for _, value := range lst_2 { + if element == value { + found = true + break + } + } + if found { + valueMatch++ + } + } + + return indexMatch, valueMatch +} + +func main() { + { + check_1 := []int{2, 6, 0, 7} + check_2 := []int{1, 4, 0, 6} + result_1, result_2 := mastermind(check_1, check_2) + fmt.Println(result_1, result_2) // (1, 1) + } + + { + check_1 := []int{2, 6, 0, 7} + check_2 := []int{3, 5, 9, 1} + result_1, result_2 := mastermind(check_1, check_2) + fmt.Println(result_1, result_2) // (0, 0) + } + + { + check_1 := []int{2, 6, 0, 7} + check_2 := []int{1, 6, 0, 4} + result_1, result_2 := mastermind(check_1, check_2) + fmt.Println(result_1, result_2) // (2, 0) + } + + { + check_1 := []int{2, 6, 0, 7} + check_2 := []int{2, 6, 0, 7} + result_1, result_2 := mastermind(check_1, check_2) + fmt.Println(result_1, result_2) // (4, 0) + } +}