(needs improved)
This commit is contained in:
parent
412836cdd4
commit
607540b804
@ -32,6 +32,29 @@ fn calc_fuel_for_pos(v: &Vec<i64>, pos: i64) -> i64 {
|
||||
return total;
|
||||
}
|
||||
|
||||
fn calc_fuel_for_pos_part2(v: &Vec<i64>, pos: i64) -> i64 {
|
||||
let mut diff = 0;
|
||||
let mut fuel = 0;
|
||||
|
||||
for x in v {
|
||||
if x > &pos {
|
||||
diff = x - pos;
|
||||
} else if x < &pos {
|
||||
diff = pos - x;
|
||||
} else {
|
||||
diff = 0
|
||||
}
|
||||
|
||||
if diff > 0 {
|
||||
for i in 1..diff+1 {
|
||||
fuel += i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fuel;
|
||||
}
|
||||
|
||||
fn get_max(v: &Vec<i64>) -> i64 {
|
||||
let mut max = 0;
|
||||
for i in v {
|
||||
@ -42,7 +65,7 @@ fn get_max(v: &Vec<i64>) -> i64 {
|
||||
return max
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn part1() {
|
||||
let pos = load_file().unwrap();
|
||||
let max = get_max(&pos);
|
||||
|
||||
@ -60,5 +83,31 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
println!("lowest consumption is pos {} with {} fuel", lastpos, lowest);
|
||||
println!("part 1 lowest consumption is pos {} with {} fuel", lastpos, lowest);
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let pos = load_file().unwrap();
|
||||
let max = get_max(&pos);
|
||||
|
||||
let mut lowest = 0;
|
||||
let mut lastpos = 0;
|
||||
|
||||
for i in 0..max {
|
||||
let fuel = calc_fuel_for_pos_part2(&pos, i);
|
||||
if i == 0 {
|
||||
lowest = fuel;
|
||||
}
|
||||
if fuel < lowest {
|
||||
lowest = fuel;
|
||||
lastpos = i;
|
||||
}
|
||||
}
|
||||
|
||||
println!("part 2 lowest consumption is pos {} with {} fuel", lastpos, lowest);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
part1();
|
||||
part2();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user