-
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.
- Loading branch information
Showing
3 changed files
with
158 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,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 | ||
} |
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,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) | ||
} | ||
} | ||
} |