Created
May 3, 2019 14:22
-
-
Save AngelOnFira/20215c383bbf7d15439518c76fb8b709 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
use noise::{NoiseFn, Perlin}; | |
const WIDTH: usize = 20; | |
const PLOTS: usize = WIDTH * WIDTH; | |
const ROOT_RANGE: usize = 1; | |
fn main() { | |
let mut farm_plots: [f64; PLOTS] = [0.0; PLOTS]; | |
for i in 0..PLOTS { | |
farm_plots[i] = get_fertility(i as f64 % WIDTH as f64, i as f64 / WIDTH as f64); | |
} | |
let mut max_score: f64 = 0.0; | |
let mut max_score_co: [usize; 3] = [0,0,0]; | |
for bean1 in 0..PLOTS { | |
for bean2 in bean1 + 1..PLOTS { | |
for bean3 in bean2 + 1..PLOTS { | |
let mut beans: Vec<[i16; 2]> = vec![]; | |
beans.push([(bean1 % WIDTH) as i16, (bean1 / WIDTH) as i16]); | |
beans.push([(bean2 % WIDTH) as i16, (bean2 / WIDTH) as i16]); | |
beans.push([(bean3 % WIDTH) as i16, (bean3 / WIDTH) as i16]); | |
let mut this_score = 0.0; | |
for (i, plot) in farm_plots.iter().enumerate() { | |
if is_plot_root((i % WIDTH) as i16, (i / WIDTH) as i16, &beans) { | |
this_score += plot; | |
} | |
} | |
if this_score > max_score { | |
max_score = this_score; | |
max_score_co = [bean1, bean2, bean3]; | |
} | |
} | |
} | |
} | |
println!("{} at {}, {}, {}", max_score, max_score_co[0], max_score_co[1], max_score_co[2]); | |
} | |
fn get_fertility(x: f64, y: f64) -> f64 { | |
let perlin = Perlin::new(); | |
perlin.get([x / 10.0, y / 10.0]) * 100.0 | |
} | |
fn is_plot_root(plot_x: i16, plot_y: i16, beans: &Vec<[i16; 2]>) -> bool { | |
for bean in beans.iter() { | |
if (bean[0] - plot_x).abs() + (bean[1] - plot_y).abs() <= ROOT_RANGE as i16 { | |
return true; | |
} | |
} | |
false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment