slow ass part 2
All checks were successful
continuous-integration/drone/push Build is passing

(needs improved)
This commit is contained in:
blackbeard420 2021-12-08 22:45:48 -05:00
parent 412836cdd4
commit 607540b804
Signed by: blackbeard420
GPG Key ID: 88C719E09CDDA4A5

View File

@ -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();
}