Skip to content

Commit

Permalink
Optimize getting unique value in the Go implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vikman90 committed Apr 12, 2020
1 parent 140b422 commit fe870a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
9 changes: 3 additions & 6 deletions Go/chess/chess.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package chess

// import "container/list"
import "fmt"

type chess struct {
Expand Down Expand Up @@ -101,7 +100,7 @@ func (chess *chess) discard(index, value int) bool {
return false

case 1:
value := queen.getValues()[0]
value := queen.findValue()

if !chess.restrict(index, value) {
return false
Expand Down Expand Up @@ -140,14 +139,12 @@ func (chess *chess) selectIndex() int {

func (chess *chess) Print() {
for i, q := range chess.queens {
values := q.getValues()

switch len(values) {
switch q.count {
case 0:
fmt.Printf("[%d] (invalid)\n", i)

case 1:
fmt.Printf("[%d] %d\n", i, values[0])
fmt.Printf("[%d] %d\n", i, q.findValue())

default:
fmt.Printf("[%d] (unsolved)\n", i)
Expand Down
14 changes: 14 additions & 0 deletions Go/chess/queen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ func (queen *queen) unsetValue(value int) {
queen.positions[value] = false
queen.count--
}

func (queen *queen) findValue() int {
if queen.count != 1 {
return -1
}

for i, v := range queen.positions {
if v {
return i
}
}

return -1
}

0 comments on commit fe870a6

Please sign in to comment.