Last active
March 9, 2018 01:11
-
-
Save makazeu/54394d837b68efc6f5ca3adb0a55b7e0 to your computer and use it in GitHub Desktop.
Revisions
-
makazeu revised this gist
Mar 9, 2018 . 1 changed file with 3 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -138,13 +138,6 @@ var ( func dfs(depth int) { if depth == QuestionNum+1 { for i := 1; i <= QuestionNum; i++ { if !checkers[i-1](answers[i]) { return @@ -156,7 +149,9 @@ func dfs(depth int) { for _, c := range Choices { answers[depth] = c counts[c]++ dfs(depth + 1) counts[c]-- } } @@ -184,4 +179,4 @@ func If(condition bool, trueVal, falseVal interface{}) interface{} { func AbsInt(x, y int) int { return If(x < y, y-x, x-y).(int) } -
makazeu revised this gist
Mar 8, 2018 . 1 changed file with 2 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,17 +7,11 @@ import ( const QuestionNum = 10 var ( Choices = [4]byte{'A', 'B', 'C', 'D'} answers = make([]byte, QuestionNum+1) counts = make(map[byte]int) checkers = []func(c byte) bool{ // question 1 func(c byte) bool { return true @@ -152,7 +146,7 @@ func dfs(depth int) { } for i := 1; i <= QuestionNum; i++ { if !checkers[i-1](answers[i]) { return } } -
makazeu created this gist
Mar 8, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,193 @@ package main import ( "fmt" "time" ) const QuestionNum = 10 type questionChecker func(c byte) bool var ( Choices = [4]byte{'A', 'B', 'C', 'D'} answers = make([]byte, QuestionNum+1) counts = make(map[byte]int) checkers = []questionChecker{ // placeholder func(c byte) bool { return true }, // question 1 func(c byte) bool { return true }, // question 2 func(c byte) bool { choices := map[byte]byte{ 'A': 'C', 'B': 'D', 'C': 'A', 'D': 'B', } return choices[c] == answers[5] }, // question 3 func(c byte) bool { choices := map[byte]byte{ 'A': answers[3], 'B': answers[6], 'C': answers[2], 'D': answers[4], } sum := make(map[byte]int) for _, v := range choices { sum[v]++ } return sum[choices[c]] == 1 }, // question 4 func(c byte) bool { choices := map[byte]bool{ 'A': answers[1] == answers[5], 'B': answers[2] == answers[7], 'C': answers[1] == answers[9], 'D': answers[6] == answers[10], } return choices[c] }, // question 5 func(c byte) bool { choices := map[byte]int{ 'A': 8, 'B': 4, 'C': 9, 'D': 7, } return c == answers[choices[c]] }, // question 6 func(c byte) bool { choices := map[byte]byte{ 'A': If(answers[2] == answers[4], answers[2], byte(0)).(byte), 'B': If(answers[1] == answers[6], answers[1], byte(0)).(byte), 'C': If(answers[3] == answers[10], answers[3], byte(0)).(byte), 'D': If(answers[5] == answers[9], answers[5], byte(0)).(byte), } return choices[c] == answers[8] }, // question 7 func(c byte) bool { choices := map[byte]byte{ 'A': 'C', 'B': 'B', 'C': 'A', 'D': 'D', } var minN = QuestionNum + 1 for k, v := range counts { if v < minN && choices[c] != k { minN = v } } return counts[choices[c]] < minN }, // question 8 func(c byte) bool { choices := map[byte]int{ 'A': 7, 'B': 5, 'C': 2, 'D': 10, } return AbsInt(int(answers[1]), int(answers[choices[c]])) != 1 }, // question 9 func(c byte) bool { choices := map[byte]int{ 'A': 6, 'B': 10, 'C': 2, 'D': 9, } flag1 := answers[1] == answers[6] flag2 := answers[choices[c]] == answers[5] return flag1 != flag2 }, // question 10 func(c byte) bool { choices := map[byte]int{ 'A': 3, 'B': 2, 'C': 4, 'D': 1, } var minN = QuestionNum + 1 for _, v := range counts { if v < minN { minN = v } } var maxN = 0 for _, v := range counts { if v > maxN { maxN = v } } return choices[c] == maxN-minN }, } ) func dfs(depth int) { if depth == QuestionNum+1 { for _, c := range Choices { counts[c] = 0 } for i := 1; i <= QuestionNum; i++ { counts[answers[i]]++ } for i := 1; i <= QuestionNum; i++ { if !checkers[i](answers[i]) { return } } output() return } for _, c := range Choices { answers[depth] = c dfs(depth + 1) } } func output() { for i := 1; i <= QuestionNum; i++ { fmt.Printf("%c ", answers[i]) } fmt.Println() //os.Exit(0) } func main() { startTime := time.Now().UnixNano() dfs(1) endTime := time.Now().UnixNano() fmt.Printf("time elapsed: %d ms\n", (endTime-startTime)/1000000) } func If(condition bool, trueVal, falseVal interface{}) interface{} { if condition { return trueVal } return falseVal } func AbsInt(x, y int) int { return If(x < y, y-x, x-y).(int) }