Skip to content

Commit

Permalink
Merge 9474e8e into 94c05f1
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Nov 30, 2018
2 parents 94c05f1 + 9474e8e commit c42483e
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ problems from
* [Day 95](https://github.com/vaskoz/dailycodingproblem-go/issues/198)
* [Day 96](https://github.com/vaskoz/dailycodingproblem-go/issues/200)
* [Day 97](https://github.com/vaskoz/dailycodingproblem-go/issues/203)
* [Day 98](https://github.com/vaskoz/dailycodingproblem-go/issues/204)
* [Day 99](https://github.com/vaskoz/dailycodingproblem-go/issues/205)
100 changes: 100 additions & 0 deletions day98/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package day98

// Board is a 2D grid of characters.
type Board [][]rune

// Position represents a Row and Col position
// on the 2D board.
type Position struct {
Row, Col int
}

// DoesExistSeqAdj returns true if the word exists sequentially in
// adjacent cells. Cells can not be reused to achieve this goal.
// Returns false otherwise.
func DoesExistSeqAdj(board Board, word string) bool {
letters := []rune(word)
exists := false
for row := range board {
for col := range board[row] {
exists = exists || search(board, row, col, letters, make(map[Position]struct{}))
if exists {
return true
}
}
}
return false
}

func search(board Board, row, col int, letters []rune, used map[Position]struct{}) bool {
if len(letters) == 0 {
return true
}
if board[row][col] != letters[0] {
return false
}
pos := Position{row, col}
if _, found := used[pos]; found {
return false
}
used[pos] = struct{}{}
// go up
if search(board, up(row, len(board)), col, letters[1:], used) {
return true
}
// go left
if search(board, row, left(col, len(board[0])), letters[1:], used) {
return true
}
// go right
if search(board, row, right(col, len(board[0])), letters[1:], used) {
return true
}
// go down
if search(board, down(row, len(board)), col, letters[1:], used) {
return true
}

delete(used, pos)
return false
}

func up(row, limit int) int {
var r int
if row == 0 {
r = limit - 1
} else {
r = row - 1
}
return r
}

func down(row, limit int) int {
var r int
if row == limit-1 {
r = 0
} else {
r = row + 1
}
return r
}

func left(col, limit int) int {
var c int
if col == 0 {
c = limit - 1
} else {
c = col - 1
}
return c
}

func right(col, limit int) int {
var c int
if col == limit-1 {
c = 0
} else {
c = col + 1
}
return c
}
57 changes: 57 additions & 0 deletions day98/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package day98

import "testing"

var testcases = []struct {
board Board
word string
expected bool
}{
{Board{
[]rune("ABCE"),
[]rune("SFCS"),
[]rune("ADEE"),
}, "ABCCED", true},
{Board{
[]rune("ABCE"),
[]rune("SFCS"),
[]rune("ADEE"),
}, "SEE", true},
{Board{
[]rune("ABCE"),
[]rune("SFCS"),
[]rune("ADEE"),
}, "ABCB", false},
{Board{
[]rune("ABCE"),
[]rune("SFCS"),
[]rune("ADEE"),
}, "ABCESCFSADEE", true},
{Board{
[]rune("ABCE"),
[]rune("SFCS"),
[]rune("ADEE"),
}, "ABCESCFSADEEA", false},
{Board{
[]rune("ABCE"),
[]rune("SFCS"),
[]rune("ADEE"),
}, "BCEA", true},
}

func TestDoesExistSeqAdj(t *testing.T) {
t.Parallel()
for tcid, tc := range testcases {
if result := DoesExistSeqAdj(tc.board, tc.word); result != tc.expected {
t.Errorf("TC%d expected %v got %v", tcid, tc.expected, result)
}
}
}

func BenchmarkDoesExistSeqAdj(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
DoesExistSeqAdj(tc.board, tc.word)
}
}
}

0 comments on commit c42483e

Please sign in to comment.