From 4bdd3b3b3062dfb6d5bf5fa34f151034dfeb3092 Mon Sep 17 00:00:00 2001 From: Hamza Ali Date: Mon, 20 Dec 2021 22:42:41 +0700 Subject: [PATCH] refactor: cleanup q01 --- question/q01.go | 58 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/question/q01.go b/question/q01.go index f836346..bc80dc3 100644 --- a/question/q01.go +++ b/question/q01.go @@ -63,8 +63,13 @@ func q1Total(a, b int) int { return a + b } +const ( + q1Entries = 100 + q1Range = 20 + directions = "NSWE" +) + func init() { - const directions = "NSWE" var q *Question q = &Question{ ID: "directions", @@ -77,7 +82,7 @@ func init() { known := make(map[int]map[int]bool) var x, y int - for i := 0; i < 100; i++ { + for i := 0; i < q1Entries; i++ { dir := directions[r.Intn(4)] steps := r.Intn(30) + 1 newX, newY := move(x, y, dir, steps) @@ -96,48 +101,43 @@ func init() { res = append(res, fmt.Sprintf("%c%d", dir, steps)) } - n := rand.Intn(20) + 10 + n := rand.Intn(q1Range) + 10 locX, locY := q1P1(res[:n]) - dup := rand.Intn(20) + 35 - lastX, lastY := q1P1(res[:dup]) - - fmt.Println(locX, locY) - fmt.Println(lastX, lastY) - fmt.Println(dup) + n = rand.Intn(q1Range) + 35 + lastX, lastY := q1P1(res[:n]) 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 { - res[dup] = fmt.Sprintf("E%d", int(dX)) - dup++ + res[n] = fmt.Sprintf("E%d", int(dX)) + n++ } else if locX < lastX { - res[dup] = fmt.Sprintf("W%d", int(dX)) - dup++ + res[n] = fmt.Sprintf("W%d", int(dX)) + n++ } - fmt.Println(res[dup-1]) - fmt.Println("!!") - 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 { - 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") }, - Validate: func(u *models.User, level Part, input string) bool { - inp := q.Generate(u) - lastX, lastY := q1P1(strings.Split(inp, "\n")) - if level == Part1 { - total := q1Total(lastX, lastY) - return strconv.Itoa(total) == input - } - lastX, lastY = q1P2(strings.Split(inp, "\n")) - if level == Part2 { - total := q1Total(lastX, lastY) - return strconv.Itoa(total) == input + Validate: func(u *models.User, level Part, input string) bool { + raw := q.Generate(u) + inp := strings.Split(raw, "\n") + + switch level { + case Part1: + x, y := q1P1(inp) + return q1Total(x, y) == 0 + case Part2: + x, y := q1P2(inp) + return q1Total(x, y) == 0 } return false },