add initial data
This commit is contained in:
commit
c4e841b4bc
43 files changed
+5284
No files matched your search
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc07"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "aoc07"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,36 @@
|
||||
fn main() {
|
||||
let data = vec![16,1,2,0,4,2,7,1,2,14];
|
||||
let (fuel, position) = optimise(&data);
|
||||
println!("fuel: {}, position: {}", fuel, position);
|
||||
}
|
||||
|
||||
fn fuel_for_pos(data: &Vec<i32>, position: i32) -> i32 {
|
||||
let mut result = 0;
|
||||
for item in data {
|
||||
result += (item - position).abs();
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fn get_min(vec: &Vec<i32>) -> i32 {
|
||||
return vec.iter().min().unwrap().to_owned();
|
||||
}
|
||||
|
||||
fn get_max(vec: &Vec<i32>) -> i32 {
|
||||
return vec.iter().max().unwrap().to_owned();
|
||||
}
|
||||
|
||||
fn optimise(data: &Vec<i32>) -> (i32, i32) {
|
||||
let mut positions: Vec<i32> = Vec::new();
|
||||
let mut fuel: Vec<i32> = Vec::new();
|
||||
for i in get_min(&data)..get_max(&data) {
|
||||
positions.push(i);
|
||||
fuel.push(fuel_for_pos(&data, i));
|
||||
}
|
||||
let min = get_min(&fuel);
|
||||
let index = fuel.iter().position(|&val| val == min).unwrap();
|
||||
return (min, index as i32)
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user