Skip to content

Commit

Permalink
Merge pull request #562 from vaskoz/day259
Browse files Browse the repository at this point in the history
Day259
  • Loading branch information
vaskoz committed May 26, 2019
2 parents ae3c2bd + f805cdb commit 3deb484
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ problems from
* [Day 256](https://github.com/vaskoz/dailycodingproblem-go/issues/528)
* [Day 257](https://github.com/vaskoz/dailycodingproblem-go/issues/529)
* [Day 258](https://github.com/vaskoz/dailycodingproblem-go/issues/530)
* [Day 259](https://github.com/vaskoz/dailycodingproblem-go/issues/531)
* [Day 260](https://github.com/vaskoz/dailycodingproblem-go/issues/533)
* [Day 263](https://github.com/vaskoz/dailycodingproblem-go/issues/539)
* [Day 266](https://github.com/vaskoz/dailycodingproblem-go/issues/542)
Expand Down
30 changes: 30 additions & 0 deletions day259/problem.go
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
}
32 changes: 32 additions & 0 deletions day259/problem_test.go
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)
}
}
}

0 comments on commit 3deb484

Please sign in to comment.