-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #562 from vaskoz/day259
Day259
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package day259 | ||
|
||
import "sort" | ||
|
||
// GhostWinningLetterPlayerOne determine the letters the | ||
// first player should start with, such that with optimal | ||
// play they cannot lose. | ||
func GhostWinningLetterPlayerOne(dict []string) []rune { | ||
firstRuneAllEven := make(map[rune]bool) | ||
for _, word := range dict { | ||
runes := []rune(word) | ||
isEvenLength := len(runes)%2 == 0 | ||
firstRune := runes[0] | ||
if _, found := firstRuneAllEven[firstRune]; !isEvenLength { | ||
firstRuneAllEven[firstRune] = false | ||
} else if !found && isEvenLength { | ||
firstRuneAllEven[firstRune] = true | ||
} | ||
} | ||
var result []rune | ||
for r, valid := range firstRuneAllEven { | ||
if valid { | ||
result = append(result, r) | ||
} | ||
} | ||
sort.Slice(result, func(i, j int) bool { | ||
return result[i] < result[j] | ||
}) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package day259 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// nolint | ||
var testcases = []struct { | ||
dict []string | ||
result []rune | ||
}{ | ||
{[]string{"cat", "calf", "dog", "bear"}, []rune{'b'}}, | ||
{[]string{"cat", "calf", "dog", "bear", "pear", "pair"}, []rune{'b', 'p'}}, | ||
} | ||
|
||
func TestGhostWinningLetterPlayerOne(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := GhostWinningLetterPlayerOne(tc.dict); !reflect.DeepEqual(result, tc.result) { | ||
t.Errorf("Expected %v, got %v", string(tc.result), string(result)) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkGhostWinningLetterPlayerOne(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
GhostWinningLetterPlayerOne(tc.dict) | ||
} | ||
} | ||
} |