-
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 #60 from vaskoz/day25
Day25
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
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
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 day25 | ||
|
||
// Match returns if there is a regex match on the given input. | ||
func Match(input, pattern string) bool { | ||
var ii, pi, missed int | ||
for ii < len(input) && pi < len(pattern) && missed < 2 { | ||
if pattern[pi] == '*' { | ||
prev := pattern[pi-1] | ||
var next byte | ||
if pi+1 < len(pattern) { | ||
next = pattern[pi+1] | ||
} | ||
for ii < len(input) && input[ii] != next { | ||
if prev != '.' && input[ii] != prev { | ||
return false | ||
} | ||
ii++ | ||
} | ||
if ii == len(input) && pi < len(pattern)-1 { | ||
return false | ||
} | ||
ii-- | ||
} else if pattern[pi] != '.' && input[ii] != pattern[pi] { | ||
missed++ | ||
} | ||
ii++ | ||
pi++ | ||
} | ||
return ii == len(input) && pi == len(pattern) | ||
} |
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,36 @@ | ||
package day25 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
input, pattern string | ||
expected bool | ||
}{ | ||
{"ray", "ra.", true}, | ||
{"raymond", "ra.", false}, | ||
{"chat", ".*at", true}, | ||
{"chats", ".*at", false}, | ||
{"chaaaa", "cha*", true}, | ||
{"raymond", "ra.*", true}, | ||
{"raymond", "ra.*d", true}, | ||
{"chaaab", "cha*", false}, | ||
{"chaaa", "cha*b", false}, | ||
{"abcd", "efg", false}, | ||
{"ab", "e", false}, | ||
} | ||
|
||
func TestMatch(t *testing.T) { | ||
for _, tc := range testcases { | ||
if result := Match(tc.input, tc.pattern); result != tc.expected { | ||
t.Errorf("Given (%v, %v), expected %v got %v", tc.input, tc.pattern, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMatch(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
Match(tc.input, tc.pattern) | ||
} | ||
} | ||
} |