Skip to content

Instantly share code, notes, and snippets.

@makazeu
Last active June 25, 2019 04:51
Show Gist options
  • Select an option

  • Save makazeu/e72d0a205beeaf6be61699960cb8ea6b to your computer and use it in GitHub Desktop.

Select an option

Save makazeu/e72d0a205beeaf6be61699960cb8ea6b to your computer and use it in GitHub Desktop.
江苏2018刑侦科推理试题_Golang
package main
import (
"fmt"
"time"
)
const QuestionNum = 10
var (
startTime int64
Options = [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
},
// question 2
func(c byte) bool {
option := map[byte]byte{
'A': 'C',
'B': 'D',
'C': 'A',
'D': 'B',
}
return option[c] == answers[5]
},
// question 3
func(c byte) bool {
option := map[byte]byte{
'A': answers[3],
'B': answers[6],
'C': answers[2],
'D': answers[4],
}
sum := make(map[byte]int)
for _, v := range option {
sum[v]++
}
return sum[option[c]] == 1
},
// question 4
func(c byte) bool {
option := map[byte]bool{
'A': answers[1] == answers[5],
'B': answers[2] == answers[7],
'C': answers[1] == answers[9],
'D': answers[6] == answers[10],
}
return option[c]
},
// question 5
func(c byte) bool {
option := map[byte]int{
'A': 8,
'B': 4,
'C': 9,
'D': 7,
}
return c == answers[option[c]]
},
// question 6
func(c byte) bool {
option := 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 option[c] == answers[8]
},
// question 7
func(c byte) bool {
option := map[byte]byte{
'A': 'C',
'B': 'B',
'C': 'A',
'D': 'D',
}
var minN = QuestionNum + 1
for k, v := range counts {
if v < minN && option[c] != k {
minN = v
}
}
return counts[option[c]] < minN
},
// question 8
func(c byte) bool {
option := map[byte]int{
'A': 7,
'B': 5,
'C': 2,
'D': 10,
}
return AbsInt(int(answers[1])-int(answers[option[c]])) != 1
},
// question 9
func(c byte) bool {
option := map[byte]int{
'A': 6,
'B': 10,
'C': 2,
'D': 9,
}
flag1 := answers[1] == answers[6]
flag2 := answers[option[c]] == answers[5]
return flag1 != flag2
},
// question 10
func(c byte) bool {
option := 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 option[c] == maxN-minN
},
}
checklists = map[int][]int{
5: {2},
6: {3},
9: {5},
10: {4, 6, 7, 8, 9, 10},
}
)
func init() {
startTime = time.Now().UnixNano()
}
func dfs(depth int) {
if depth == QuestionNum+1 {
output()
return
}
for _, c := range Options {
answers[depth] = c
counts[c]++
checkResult := true
// check
if checklist, exists := checklists[depth]; exists {
for _, check := range checklist {
if !checkers[check-1](answers[check]) {
checkResult = false
break
}
}
}
if checkResult {
dfs(depth + 1)
}
counts[c]--
}
}
func output() {
for i := 1; i <= QuestionNum; i++ {
fmt.Printf("%c ", answers[i])
}
endTime := time.Now().UnixNano()
fmt.Printf(" (%d ms)\n", (endTime-startTime)/1000000)
}
func main() {
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 int) int {
return If(x >= 0, x, -x).(int)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment