/
problems.go
113 lines (92 loc) · 3.03 KB
/
problems.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package util
import (
"crypto/md5"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/koomen/eulercli/consts"
)
// EulerProblem - a structured type representing a project euler problem
type EulerProblem struct {
ProblemNum int
PaddedProblemNum string
ProblemText string
AnswerMD5 string
Answer string
}
// MissingProblemError - used when the user requests a problem that doesn't exist
type MissingProblemError struct {
ProblemNum int
LargestSupportedProblemNum int
}
// Error - convert a MissingProblemError into a string
func (e *MissingProblemError) Error() string {
return fmt.Sprintf(
"Problem %d not found. The euler CLI supports problems 1-%d.",
e.ProblemNum, e.LargestSupportedProblemNum)
}
// getProblemText - extract problem text from consts/assets/project_euler_problems.txt
func getProblemText(problemNum int) (string, error) {
re := regexp.MustCompile(`Problem [0-9]+\s*\=+`)
split := re.Split(consts.ProblemsText, -1)
largestSupportedProblemNum := len(split) - 1
if problemNum > largestSupportedProblemNum || problemNum < 1 {
return "", &MissingProblemError{problemNum, largestSupportedProblemNum}
}
rawText := split[problemNum]
// Remove the MD5'd answer text
re = regexp.MustCompile(`((?s:.*))Answer:\s+(\S+)\s*$`)
submatch := re.FindStringSubmatch(rawText)
if len(submatch) < 3 {
return rawText, nil
}
// Remove leading spaces from problem text
leadingSpaces := regexp.MustCompile(`(?m:^ )`)
formattedProblemText := leadingSpaces.ReplaceAllString(strings.TrimSpace(submatch[1]), "")
return formattedProblemText, nil
}
func getSolutionsText() (string, error) {
resp, err := http.Get(consts.SolutionsTextURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(raw), nil
}
// getAnswer - extract problem answer from https://github.com/luckytoilet/projecteuler-solutions
// We download answer data on-the-fly in order to avoid breaking the Project Euler
// rules about publishing solutions
func getAnswer(problemNum int) (string, error) {
solutionsText, err := getSolutionsText()
if err != nil {
return "", err
}
re := regexp.MustCompile(`(?m:^[0-9]+.\s+)`)
split := re.Split(solutionsText, -1)
largestSupportedProblemNum := len(split) - 1
if problemNum > largestSupportedProblemNum || problemNum < 1 {
return "", &MissingProblemError{problemNum, largestSupportedProblemNum}
}
return strings.TrimSpace(split[problemNum]), nil
}
// GetProblem - return an EulerProblem instance corresponding to the given problem number
func GetProblem(problemNum int) (*EulerProblem, error) {
text, probErr := getProblemText(problemNum)
answer, ansErr := getAnswer(problemNum)
if probErr != nil && ansErr != nil {
return nil, ansErr
}
return &EulerProblem{
ProblemNum: problemNum,
PaddedProblemNum: fmt.Sprintf("%04d", problemNum),
ProblemText: text,
AnswerMD5: fmt.Sprintf("%x", md5.Sum([]byte(answer))),
Answer: answer,
}, nil
}