refactor: cleanup q01

master
ALI Hamza 2021-12-20 22:42:41 +07:00
parent 19ffce170a
commit 4bdd3b3b30
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
1 changed files with 29 additions and 29 deletions

@ -63,8 +63,13 @@ func q1Total(a, b int) int {
return a + b return a + b
} }
const (
q1Entries = 100
q1Range = 20
directions = "NSWE"
)
func init() { func init() {
const directions = "NSWE"
var q *Question var q *Question
q = &Question{ q = &Question{
ID: "directions", ID: "directions",
@ -77,7 +82,7 @@ func init() {
known := make(map[int]map[int]bool) known := make(map[int]map[int]bool)
var x, y int var x, y int
for i := 0; i < 100; i++ { for i := 0; i < q1Entries; i++ {
dir := directions[r.Intn(4)] dir := directions[r.Intn(4)]
steps := r.Intn(30) + 1 steps := r.Intn(30) + 1
newX, newY := move(x, y, dir, steps) newX, newY := move(x, y, dir, steps)
@ -96,48 +101,43 @@ func init() {
res = append(res, fmt.Sprintf("%c%d", dir, steps)) res = append(res, fmt.Sprintf("%c%d", dir, steps))
} }
n := rand.Intn(20) + 10 n := rand.Intn(q1Range) + 10
locX, locY := q1P1(res[:n]) locX, locY := q1P1(res[:n])
dup := rand.Intn(20) + 35 n = rand.Intn(q1Range) + 35
lastX, lastY := q1P1(res[:dup]) lastX, lastY := q1P1(res[:n])
fmt.Println(locX, locY)
fmt.Println(lastX, lastY)
fmt.Println(dup)
dX := math.Max(float64(locX), float64(lastX)) - math.Min(float64(locX), float64(lastX)) dX := math.Max(float64(locX), float64(lastX)) - math.Min(float64(locX), float64(lastX))
dY := math.Max(float64(locY), float64(lastY)) - math.Min(float64(locY), float64(lastY))
if locX > lastX { if locX > lastX {
res[dup] = fmt.Sprintf("E%d", int(dX)) res[n] = fmt.Sprintf("E%d", int(dX))
dup++ n++
} else if locX < lastX { } else if locX < lastX {
res[dup] = fmt.Sprintf("W%d", int(dX)) res[n] = fmt.Sprintf("W%d", int(dX))
dup++ n++
} }
fmt.Println(res[dup-1])
fmt.Println("!!")
if locY > lastY { if locY > lastY {
res[dup] = fmt.Sprintf("N%d", int(math.Max(float64(locY), float64(lastY))-math.Min(float64(locY), float64(lastY)))) res[n] = fmt.Sprintf("N%d", int(dY))
} else if locY < lastY { } else if locY < lastY {
res[dup] = fmt.Sprintf("S%d", int(math.Max(float64(locY), float64(lastY))-math.Min(float64(locY), float64(lastY)))) res[n] = fmt.Sprintf("S%d", int(dY))
} }
return strings.Join(res, "\n") return strings.Join(res, "\n")
}, },
Validate: func(u *models.User, level Part, input string) bool {
inp := q.Generate(u)
lastX, lastY := q1P1(strings.Split(inp, "\n")) Validate: func(u *models.User, level Part, input string) bool {
if level == Part1 { raw := q.Generate(u)
total := q1Total(lastX, lastY) inp := strings.Split(raw, "\n")
return strconv.Itoa(total) == input
} switch level {
lastX, lastY = q1P2(strings.Split(inp, "\n")) case Part1:
if level == Part2 { x, y := q1P1(inp)
total := q1Total(lastX, lastY) return q1Total(x, y) == 0
return strconv.Itoa(total) == input case Part2:
x, y := q1P2(inp)
return q1Total(x, y) == 0
} }
return false return false
}, },