Skip to content

Instantly share code, notes, and snippets.

@PeterCxy
Created March 6, 2018 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterCxy/861dc62d429812c5ff12906a76b2ca9f to your computer and use it in GitHub Desktop.
Save PeterCxy/861dc62d429812c5ff12906a76b2ca9f to your computer and use it in GitHub Desktop.
Brute force solution to the viral logic test
const A: usize = 0;
const B: usize = 1;
const C: usize = 2;
const D: usize = 3;
const Q2_SELECTIONS: [usize; 4] = [C, D, A, B];
const Q3_SELECTIONS: [usize; 4] = [3, 6, 2, 4];
const Q4_SELECTIONS: [(usize, usize); 4] = [
(1, 5), (2, 7), (1, 9), (6, 10)
];
const Q5_SELECTIONS: [usize; 4] = [8, 4, 9, 7];
const Q6_SELECTIONS: [(usize, usize); 4] = [
(2, 4), (1, 6), (3, 10), (5, 9)
];
const Q7_SELECTIONS: [usize; 4] = [C, B, A, D];
const Q8_SELECTIONS: [usize; 4] = [7, 5, 2, 10];
const Q9_SELECTIONS: [usize; 4] = [6, 10, 2, 9];
const Q10_SELECTIONS: [usize; 4] = [3, 2, 4, 1];
struct Questions([usize; 10]);
impl Questions {
fn new() -> Questions {
Questions([0; 10])
}
fn is_solution(&self) -> bool {
self.q1() && self.q2() &&
self.q3() && self.q4() &&
self.q5() && self.q6() &&
self.q7() && self.q8() &&
self.q9() && self.q10()
}
fn next(&mut self) -> bool {
self.0[9] += 1;
for i in 0..10 {
let j = 9 - i;
if self.0[j] == 4 {
if j > 0 {
self.0[j - 1] += 1;
self.0[j] = 0;
} else {
return false;
}
}
}
return true;
}
fn answer_of(&self, id: usize) -> usize {
self.0[id - 1]
}
fn collect_counts(&self) -> [usize; 4] {
let mut arr: [usize; 4] = [0; 4];
for i in &self.0 {
arr[i.clone()] += 1;
}
return arr;
}
fn q1(&self) -> bool {
// Always true
true
}
fn q2(&self) -> bool {
// Q2: The answer of Q5 is?
return Q2_SELECTIONS[self.answer_of(2)] == self.answer_of(5);
}
fn q3(&self) -> bool {
// Q3: which of the following questions
// has a different answer from all the others?
let ans = self.answer_of(3);
let sel_ans = self.answer_of(Q3_SELECTIONS[ans]);
let mut other_ans = None;
for i in 0..4 {
if i != ans {
let cur_ans = self.answer_of(Q3_SELECTIONS[i]);
if other_ans.is_some() && cur_ans != other_ans.unwrap() {
return false;
}
other_ans = Some(cur_ans);
}
}
return other_ans.unwrap() != sel_ans;
}
fn q4(&self) -> bool {
// Q4: which of the following questions have the same answer?
let sel = Q4_SELECTIONS[self.answer_of(4)];
return self.answer_of(sel.0) == self.answer_of(sel.1);
}
fn q5(&self) -> bool {
// Q5: Which of the following questions have the same answer
// as this one?
let ans = self.answer_of(5);
return self.answer_of(Q5_SELECTIONS[ans]) == ans;
}
fn q6(&self) -> bool {
// Q6: Which of the following question pairs have the
// same answer as Q8
let q8_ans = self.answer_of(8);
let my_sel = Q6_SELECTIONS[self.answer_of(6)];
return self.answer_of(my_sel.0) == q8_ans && self.answer_of(my_sel.1) == q8_ans;
}
fn q7(&self) -> bool {
// Q7: which of the following answers
// has appeared least
let arr = self.collect_counts();
let mut least = 0usize;
for i in 0..4 {
if arr[i] < arr[least] {
least = i;
}
}
return least == Q7_SELECTIONS[self.answer_of(7)];
}
fn q8(&self) -> bool {
// Q8: Which of the following question's answer
// is not consecutive to the answer of Q1
let ans_q1 = self.answer_of(1) as isize;
let ans_ans = self.answer_of(Q8_SELECTIONS[self.answer_of(8)]) as isize;
return ans_q1 != ans_ans - 1 && ans_q1 != ans_ans + 1;
}
fn q9(&self) -> bool {
// Q9: Select X such that 'The answer of Q1 and Q6 are the same'
// and 'The anser of QX and Q5 are the same' have different truth values
let p1 = self.answer_of(1) == self.answer_of(6);
let p2 = self.answer_of(Q9_SELECTIONS[self.answer_of(9)]) == self.answer_of(5);
return p1 != p2;
}
fn q10(&self) -> bool {
// Q10: What is the difference between the most and least selected
// answers?
let arr = self.collect_counts();
let mut max = 0;
let mut min = 0;
for i in 0..4 {
if arr[i] > arr[max] {
max = i;
}
if arr[i] < arr[min] {
min = i;
}
}
return arr[max] - arr[min] == Q10_SELECTIONS[self.answer_of(10)];
}
}
fn main() {
let mut i = 0;
let mut questions = Questions::new();
while questions.next() {
i += 1;
if questions.is_solution() {
println!("solution: {:?}", questions.0);
break;
}
}
println!("Program exit, iterations: {}", i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment