Skip to content

Instantly share code, notes, and snippets.

@cfsamson
Created March 4, 2019 18:05
Show Gist options
  • Save cfsamson/b3a43cb005bada0cbeb54f6b781a93c2 to your computer and use it in GitHub Desktop.
Save cfsamson/b3a43cb005bada0cbeb54f6b781a93c2 to your computer and use it in GitHub Desktop.
optimizing part 1 code changes
// -------- We change the code from this: --------
lazy_static! {
static ref LETTERS: Vec<i32> = {
let x: String = [
"5O5_", "5W9W", "5_9_", // P (without curve)
"AOEO", "COC_", "A_E_", // I
"IOQ_", "I_QO", // X
"UOY_", "Y_]O", "WW[W", // A
"aOa_", "aWeW", "a_e_", "cWiO" // R (without curve)
].concat();
x.chars().map(|c| c as i32).collect::<Vec<i32>>()
};
// {...}
}
// -------- Into this: --------
lazy_static! {
static ref LETTER_BLOCKS: Vec<(i32,i32,i32,i32)> = {
let x: Vec<i32> = [
"5O5_", "5W9W", "5_9_", // P (without curve)
"AOEO", "COC_", "A_E_", // I
"IOQ_", "I_QO", // X
"UOY_", "Y_]O", "WW[W", // A
"aOa_", "aWeW", "a_e_", "cWiO" // R (without curve)
].concat().chars().map(|c| c as i32).collect::<Vec<i32>>();
(0..x.len())
.step_by(4)
.map(|i| (x[i], x[i+1], x[i+2], x[i+3]))
.collect::<Vec<(i32, i32, i32, i32)>>()
};
// {...}
}
// --------- And we change the corresponding method in `query_database` from this --------
fn query_database(position: Vec3, hit_type: &mut u8) -> f32 {
let mut distance = std::f32::MAX;
let mut f = position;
f.z = 0.0;
let letter_count = LETTERS.len();
for i in (0..letter_count).step_by(4) {
let begin = Vec3::new_ab((LETTERS[i] - 79) as f32, (LETTERS[i + 1] - 79) as f32) * Vec3::from(0.5);
let e = Vec3::new_ab((LETTERS[i + 2] - 79) as f32, (LETTERS[i + 3] - 79) as f32) * Vec3::from(0.5)
+ begin * Vec3::from(-1.0);
let o_part1 = -min((begin + f * Vec3::from(-1.0)) % e / (e % e), 0.0);
let o = f + (begin + e * min(o_part1, 1.0).into()) * Vec3::from(-1.0);
distance = min(distance, o % o);
}
// {...}
}
// -------- Into this: --------
fn query_database(position: Vec3, hit_type: &mut u8) -> f32 {
let mut distance = std::f32::MAX;
let mut f = position;
f.z = 0.0;
for (a, b, c, d) in LETTER_BLOCKS.iter() {
let begin = Vec3::new_ab((a - 79) as f32, (b - 79) as f32) * Vec3::from(0.5);
let e = Vec3::new_ab((c - 79) as f32, (d - 79) as f32) * Vec3::from(0.5)
+ begin * Vec3::from(-1.0);
let o_part1 = -min((begin + f * Vec3::from(-1.0)) % e / (e % e), 0.0);
let o = f + (begin + e * min(o_part1, 1.0).into()) * Vec3::from(-1.0);
distance = min(distance, o % o);
}
// {...}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment