82 lines
1.4 KiB
Go
82 lines
1.4 KiB
Go
|
package q05
|
||
|
|
||
|
import (
|
||
|
_ "embed"
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"text/template"
|
||
|
|
||
|
"github.com/hhhapz/codequest/models"
|
||
|
"github.com/hhhapz/codequest/question"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
t := template.New("fuel")
|
||
|
var err error
|
||
|
t, err = t.Parse(q05Text)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
question.Register(
|
||
|
&question.Question{
|
||
|
ID: "fuel",
|
||
|
Name: "Fuel for the Farlands",
|
||
|
Text: t,
|
||
|
Level: question.Level1,
|
||
|
Generate: func(u *models.User) string {
|
||
|
inp := generate(u)
|
||
|
nums := make([]string, nums)
|
||
|
for i, num := range inp {
|
||
|
nums[i] = strconv.Itoa(num)
|
||
|
}
|
||
|
return strings.Join(nums, "\n")
|
||
|
},
|
||
|
Validate: func(u *models.User, part question.Part, solution string) bool {
|
||
|
return Validate(u, part, solution)
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
nums = 350
|
||
|
)
|
||
|
|
||
|
func generate(u *models.User) []int {
|
||
|
res := make([]int, nums)
|
||
|
|
||
|
r := question.UserRandom(u)
|
||
|
|
||
|
for i := 0; i < nums; i++ {
|
||
|
if nums < 10 {
|
||
|
res[i] = r.Intn(10000-200) + 200
|
||
|
continue
|
||
|
}
|
||
|
res[i] = r.Intn(1000000-1000) + 1000
|
||
|
}
|
||
|
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func Validate(u *models.User, p question.Part, sol string) bool {
|
||
|
inp := generate(u)
|
||
|
|
||
|
var n int
|
||
|
switch p {
|
||
|
case question.Part1:
|
||
|
n = solveP1(inp)
|
||
|
case question.Part2:
|
||
|
n = solveP2(inp)
|
||
|
|
||
|
default:
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
fmt.Println("submitted", u.Name, p, sol)
|
||
|
fmt.Println("actual", u.Name, p, n)
|
||
|
return strconv.Itoa(n) == sol
|
||
|
}
|
||
|
|
||
|
//go:embed q05.md
|
||
|
var q05Text string
|