Last active
August 29, 2015 14:13
-
-
Save thedeemon/b5eb64d82b0b855d55b0 to your computer and use it in GitHub Desktop.
This file contains 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 characters
#![allow(unstable)] | |
enum Exp { | |
IfGt(usize, usize, Vec<Exp>, Vec<Exp>), | |
Swap(usize, usize), | |
Copy(usize, usize) | |
} | |
fn eval(a : &mut Vec<i32>, e : &Exp) -> i32 { | |
match *e { | |
Exp::IfGt(i, j, ref b1, ref b2) => | |
1 + (if a[i] > a[j] { evalBlock(a, b1) } else { evalBlock(a, b2) }), | |
Exp::Swap(i,j) => { | |
let ai = a[i]; let aj = a[j]; | |
a[i] = aj; a[j] = ai; | |
1 | |
}, | |
Exp::Copy(i,j) => { | |
a[i] = a[j]; 1 | |
} | |
} | |
} | |
fn evalBlock(a : &mut Vec<i32>, blk : &Vec<Exp>) -> i32 { | |
blk.iter().fold(0, |cnt, e| cnt + eval(a, e)) | |
} | |
fn numSteps(blk : &Vec<Exp>, ua : &Vec<i32>) -> i32 { | |
let mut a = ua.clone(); | |
let n = evalBlock(&mut a, blk); | |
if a[1]==2 && a[6]==7 { n } else { 25000 } | |
} | |
fn calcScore(blk : &Vec<Exp>, inputs : &Vec<Vec<i32>>) -> i32 { | |
inputs.iter().fold(0, |sum, a| sum + numSteps(blk, a)) | |
} | |
fn cmp(i : usize, j : usize) -> Exp { | |
Exp::IfGt(i, j, vec![Exp::Swap(i,j)], vec![]) | |
} | |
fn main() { | |
let zs0 = [0,0,0,0]; | |
let zs = zs0.as_slice(); | |
let inputs : Vec<Vec<i32>> = | |
[1,2,3,4,5,6,7,8].permutations().map(|mut a : Vec<i32>| { a.push_all(zs); a } ).collect(); | |
let sornet = vec![cmp(0, 1), cmp(2, 3), cmp(4, 5), cmp(6, 7), | |
cmp(0, 2), cmp(1, 3), cmp(4, 6), cmp(5, 7), | |
cmp(1, 2), cmp(5, 6), cmp(0, 4), cmp(3, 7), | |
cmp(1, 5), cmp(2, 6), | |
cmp(1, 4), cmp(3, 6)]; | |
let score = range(0, 40).map(|i| calcScore(&sornet, &inputs)).min().unwrap(); | |
println!("{}", score); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment