advent-of-code

advent of code (partial solutions)
Log | Files | Refs | README

commit 2bf354c9637a51351d6c5a874974d4a1510a5708
parent 05033fcc7ca02f6232df42cc774351505c714b39
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon,  6 Dec 2021 21:50:07 -0800

cleanup median function

Diffstat:
M2021/aoc07.rs | 18++----------------
1 file changed, 2 insertions(+), 16 deletions(-)

diff --git a/2021/aoc07.rs b/2021/aoc07.rs @@ -7,12 +7,12 @@ fn main() -> Result<(), Box<dyn Error>> { let mut stdin = io::stdin(); let mut input = String::new(); stdin.read_to_string(&mut input); - let nums = input + let nums: Vec<i32> = input .trim() .split(",") .map(|n| n.parse::<i32>().unwrap()) .collect(); - let m = median(&nums).round() as i32; + let m = nums[nums.len() / 2]; let total: i32 = nums.iter().map(|n| (n - m).abs()).sum(); println!("{}", total); let total = |m2: i32| { @@ -27,17 +27,3 @@ fn main() -> Result<(), Box<dyn Error>> { println!("{}", part2); Ok(()) } - -// stolen -fn median(v: &Vec<i32>) -> f32 { - if v.len() < 1 { - return 0.0; - } - - let mut vec = v.clone(); - vec.sort(); - if vec.len() % 2 == 1 { - return *vec.get(vec.len() / 2).unwrap() as f32; - } - return (*vec.get(vec.len() / 2 - 1).unwrap() + *vec.get(vec.len() / 2).unwrap()) as f32 / 2.0; -}