54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package wordlepattern
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Pattern matches "Wordle XXXX Y/6" format (with optional comma in number)
|
|
var pattern = regexp.MustCompile(`(?i)Wordle\s+([\d,]+)\s+([X1-6])/6`)
|
|
|
|
// Wordle #1 was June 19, 2021
|
|
var wordleStartDate = time.Date(2021, 6, 19, 0, 0, 0, 0, time.UTC)
|
|
|
|
// Match represents a matched Wordle score
|
|
type Match struct {
|
|
PuzzleNumber int
|
|
Score string
|
|
}
|
|
|
|
// GetExpectedPuzzleNumber returns the expected Wordle puzzle number for a given date
|
|
func GetExpectedPuzzleNumber(date time.Time) int {
|
|
daysSinceStart := int(date.Sub(wordleStartDate).Hours() / 24)
|
|
return daysSinceStart + 1
|
|
}
|
|
|
|
// IsValidPuzzleNumber checks if a puzzle number is valid for the given date
|
|
// Allows for timezone differences (±1 day)
|
|
func IsValidPuzzleNumber(puzzleNumber int, date time.Time) bool {
|
|
expected := GetExpectedPuzzleNumber(date)
|
|
// Allow ±1 day for timezone differences
|
|
return puzzleNumber >= expected-1 && puzzleNumber <= expected+1
|
|
}
|
|
|
|
// Find searches for a Wordle pattern in the given text
|
|
// Returns the match and a boolean indicating if a match was found
|
|
func Find(text string) (*Match, bool) {
|
|
matches := pattern.FindStringSubmatch(text)
|
|
if matches == nil {
|
|
return nil, false
|
|
}
|
|
|
|
// Remove commas from puzzle number before parsing
|
|
puzzleNumberStr := strings.ReplaceAll(matches[1], ",", "")
|
|
puzzleNumber, _ := strconv.Atoi(puzzleNumberStr)
|
|
score := strings.ToUpper(matches[2])
|
|
|
|
return &Match{
|
|
PuzzleNumber: puzzleNumber,
|
|
Score: score,
|
|
}, true
|
|
}
|