Santa was livid. His face was redder than Rudolph’s nose after a Red Bull binge. The elves had tried the sleigh's navigation system again, running the find_best_location
function. Instead of landing them in a cozy, gift-ready location, the sleigh had whisked them right back to the North Pole.
“I told you to land in the best spot, not bring me back to this frosty hellhole!” Santa bellowed. His voice echoed across the frozen tundra, sending a nearby snowman into early retirement.
Bernard cautiously stepped forward. “Erm… Sir, the function seems to be, uh, calculating the densest snow coverage but doesn’t account for it's distance from us." Santa’s eyes narrowed. “I said best place to land, not snowiest! Who do I look like, a yeti?”
“Show me the code,” Santa growled, extending a gloved hand that looked ready to smash a keyboard.
Bernard shakily handed him the tablet. “Here it is, boss.”
Santa squinted at the screen, his rage escalating as he scanned the lines. “FOR HOLLY JOLLY’S SAKE—are you not using references again?! A consuming iterator? Do you want to crash the sleigh into a snowbank?!”
The elves collectively winced, knowing that Santa's rants about proper memory management could last for hours.
Santa shoved the tablet back at Bernard. “Fix it, or I’m replacing the sleigh team with drones. And, use references for the love of all that’s holly and jolly, Bernard! I won’t tolerate another memory leak on Christmas Eve!”
The function find_best_location
is not behaving as expected. But the code might still be useful, so the elves don’t want to scrap it entirely, instead they renamed it to find_most_dense_location
.
Here is what you need to do for today:
&[Location]
instead of a Vec<Location>
and return a reference to a Location
instead of an owned value.find_nearest_location
that will return the nearest location to the current location which is (x = 0, y = 0)
. Ignore the z
coordinate.1000.0
to be considered a good location. If there weren't any, return an Error
.If you’re stuck or need a starting point, here are some hints to help you along the way!
To make the function accept a reference to the Vec<Location>
you can use, &[Location]
as the argument type.
For the return type, you should return a reference to a Location
instead of an owned value. Result<&Location, Box<dyn Error>>
In order to return the type as a reference, the iterator should not be consumed anymore, so instead of into_iter
use iter()
.
Filter the locations that have a density greater than 1000.0
using the filter
function from the Iterator
trait. e.g. locations.iter().filter(|a| a.density() >= 1000.0)
.
To find the nearest location to the current location, you can use the min_by
function from the Iterator
trait.
The min_by
function takes a closure that compares two elements and returns the one that should be considered the minimum.
You can get the distance between the origin (0, 0) and (x, y) by using the hypot
method from the f64
type. e.g. a.x.hypot(a.y)
. This will return the distance between the origin and the location a
.
Use a partial_cmp
just like the other function to compare the distances between two locations.
Convert the Option<&Location>
to a Result<&Location, Box<dyn Error>>
using the ok_or
method.
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .into_iter() .filter(|l| l.density() >= 1000.0) .min_by(|a, b| { let d_a = a.x.abs() + a.y.abs(); let d_b = b.x.abs() + b.y.abs(); d_a.partial_cmp(&d_b).unwrap_or(Ordering::Equal) }) .ok_or("No location found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const LOCATION_DENSITY_MIN: f64 = 1000.0;pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more // North pole is origin (x = 0, y = 0) let dist_from_pole = |loc: &Location| loc.x.hypot(loc.y); locations .iter() // This gets rid of NANs, but positive inf would pass .filter(|&loc| loc.density() >= LOCATION_DENSITY_MIN) .min_by(|a, b| dist_from_pole(a).total_cmp(&dist_from_pole(b))) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more const MAX_DENSITY: f64 = 1000.0; locations .iter() .filter(|a| a.density().ge(&MAX_DENSITY)) .min_by(|a, b| { (a.x.abs() + a.y.abs()) .partial_cmp(&(b.x.abs() + b.y.abs())) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more // (a.x + a.y + a.z).abs().partial_cmp((b.x + b.y + b.z).abs() locations .iter() .filter(|v| v.density() >= 1000.0) .min_by(|a, b| (a.x + a.y + a.z).abs().partial_cmp(&(b.x + b.y + b.y).abs()).ok_or("").unwrap()) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .into_iter() .filter(|l| l.density() >= 1000.0) .min_by(|a, b| { let k1 = a.x * a.x + a.y * a.y; let k2 = b.x * b.x + b.y * b.y; k1.partial_cmp(&k2).unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let locations = locations .iter() .filter(|l| l.density() >= 1000.0) .min_by(|a, b| { let distance_a = a.x.hypot(a.y); let distance_b = b.x.hypot(b.y); distance_a .partial_cmp(&distance_b) .expect("failed to get distance") }); locations.ok_or("Failed to get nearest location".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}fn main() {}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .filter(|loc| loc.density() >= 1000.0) .min_by(|a, b| { let a_dist = a.x.hypot(a.y); let b_dist = b.x.hypot(b.y); a_dist.partial_cmp(&b_dist).unwrap_or(Ordering::Equal) }) .ok_or("Problem".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let mut distance: f64 = f64::INFINITY; let mut min_distance_idx: Option<usize> = None; if locations.len() == 0 { return Err("Empty locations vector".into()); } for (idx, location) in locations.iter().enumerate() { if location.density() < 1000.0 { continue; } let new_distance = ((location.x + location.y).powf(2.0)).sqrt(); if new_distance < distance { distance = new_distance; min_distance_idx = Some(idx); } } if let Some(di) = min_distance_idx { Ok(&locations[di]) } else { Err("no min distance".into()) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};#[derive(PartialEq)]pub struct OrdFloat(f64);impl Ord for OrdFloat { fn cmp(&self, OrdFloat(other): &Self) -> Ordering { self.0.partial_cmp(other).unwrap_or(Ordering::Equal) }}impl PartialOrd for OrdFloat { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }}impl Eq for OrdFloat {}// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|location| location.density() >= 1000.0) .min_by_key(|location| location.distance()) .ok_or("No location found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Clone, Debug, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } } pub fn distance(&self) -> OrdFloat { OrdFloat(self.x.hypot(self.y)) }}#[cfg(test)]mod test { use crate::{find_nearest_location, Location, Snowball}; #[test] fn test_nearest() { let locations: Vec<Location> = vec![ Location::new(10.0, 10.0, 0.0, 1.0, Snowball(1000)), Location::new(10.0, -10.0, 0.0, 1.0, Snowball(1000)), Location::new(-10.0, -10.0, 0.0, 1.0, Snowball(1000)), Location::new(14.0, 0.0, 0.0, 1.0, Snowball(1000)), Location::new(-10.0, 10.0, 0.0, 1.0, Snowball(1000)), ]; assert_eq!(find_nearest_location(&locations).ok(), Some(&Location::new(14.0, 0.0, 0.0, 1.0, Snowball(1000)),)); } #[test] fn test_nearest_density() { let locations: Vec<Location> = vec![ Location::new(1.0, 0.0, 0.0, 1.0, Snowball(800)), Location::new(2.0, 0.0, 0.0, 1.0, Snowball(900)), Location::new(3.0, 0.0, 0.0, 1.0, Snowball(1000)), Location::new(4.0, 0.0, 0.0, 1.0, Snowball(1100)), Location::new(5.0, 0.0, 0.0, 1.0, Snowball(1200)), ]; assert_eq!(find_nearest_location(&locations).ok(), Some(&Location::new(3.0, 0.0, 0.0, 1.0, Snowball(1000)))); }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|location| location.density() >= 1000.0) .min_by(|a,b| { a.distance() .partial_cmp(&b.distance()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } } pub fn distance(&self) -> f64 { self.x * self.x + self.y * self.y }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let origin_loc = Location{ x: 0.0, y: 0.0, z: 0.0, area: 0.0, snow: Snowball(0) }; locations .iter() .filter(|loc| loc.density() >= 1000.0) .min_by(|a, b| { a.distance(&origin_loc) .partial_cmp(&b.distance(&origin_loc)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } } pub fn distance(&self, other: &Location) -> f64 { ((self.x - other.x).powf(2.0) + (self.y - other.y).powf(2.0)).sqrt() }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn distance(a: &Location) -> f64 { a.x.hypot(a.y)}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.distance() .partial_cmp(&b.distance()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn distance(&self) -> f64 { self.x.hypot(self.y).hypot(self.z) } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { let loc = locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }); match loc { Some(location) => Ok(location), None => Err("No_Locations Found".into()), }}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations.iter() .filter(|loc| loc.density() >= 1000.0) .min_by(|a, b| { a.distance() .partial_cmp(&b.distance()) .unwrap_or(Ordering::Equal) }) .ok_or_else(|| "No qualifying locations found".into()) }const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } } pub fn distance(&self) -> f64 { (self.x*self.x + self.y*self.y + self.z*self.z).sqrt() }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations.iter().filter(|a| a.density() >= 1000.0).min_by(|a, b| { a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)).unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more match locations .iter() .min_by(|a, b| { a.distance().partial_cmp(&b.distance()).unwrap_or(Ordering::Equal) }){ Some(loc) if loc.density() >= 1000.0 => Ok(loc), _ => Err("No suitable location".into()) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn distance(&self) -> f64 { ((self.x * self.x) + (self.y * self.y)).sqrt() } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more match locations .iter() .min_by(|a, b| { a.distance().partial_cmp(&b.distance()).unwrap_or(Ordering::Equal) }){ Some(loc) if loc.density() >= 1000.0 => Ok(loc), _ => Err("No suitable location".into()) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn distance(&self) -> f64 { ((self.x * self.x) + (self.y * self.y)).sqrt() } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};pub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .filter(|location| location.density() >= 1000.0) .min_by(|a, b| { let distance_a = (a.x.powi(2) + a.y.powi(2)).sqrt(); let distance_b = (b.x.powi(2) + b.y.powi(2)).sqrt(); distance_a .partial_cmp(&distance_b) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found with density >= 1000.0".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location<'a>(locations: &'a [Location]) -> Result< &'a Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let distance = |x: f64, y: f64| f64::sqrt(x.powf(2.0) + y.powf(2.0)); locations.iter() .filter(|location| location.density() >= 1000.0) .min_by(|a, b| distance(a.x, a.y).partial_cmp(&distance(b.x, b.y)) .unwrap_or(Ordering::Equal)) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let dist = |x:&Location| (x.x * x.x + x.y * x.y); locations.into_iter() .filter(|x| x.density() >= 1000.0) .min_by(|a, b| dist(a).total_cmp(&dist(b))) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { cmpf64(a.density(), b.density()) }) .ok_or("No locations found".into())}fn cmpf64(a: f64, b: f64) -> Ordering { a.partial_cmp(&b).unwrap_or(Ordering::Equal)}fn dist(l: &Location) -> f64 { (l.x * l.x + l.y * l.y).sqrt()}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations.iter().filter(|loc| loc.density() >= 1000.0).min_by(|a, b| cmpf64(dist(a), dist(b))).ok_or("nope".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { let high_density = locations.iter().filter(|x| x.density() >= 1000.0).collect::<Vec<_>>(); //let iter = locations.iter().for_each(|x| println!("{}", x.density())); //density check, debugging if high_density.is_empty() { Err("No locations found".into()) //turn this into the type i want } else { let best_location = high_density.iter().min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) //cmp takes a reference, iter() turns everything into //references, this is the correct syntax .unwrap_or(Ordering::Equal) }); Ok(best_location.expect("no locations were found in the second phase")) }}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations.iter() .filter(|a| a.density() >= 1000.0) .min_by(|a,b|{ a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|location| location.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let dense_locations = locations .iter() .filter(|location| location.density() >= 1000.0); dense_locations .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations.iter() .filter(|a| a.density() >= 1000.0) .min_by( |a, b| a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)).unwrap_or(Ordering::Equal)) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};pub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|loc1, loc2| loc1.x.hypot(loc1.y) .partial_cmp(&loc2.x.hypot(loc2.y)).unwrap_or(Ordering::Equal) ) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| { a.density() >= 1000.0}) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("error".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| { a.density() >= 1000.0}) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("error".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|location| location.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)).unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|location| location.density() >= 1000.0) .min_by(|a, b| { let distance_a = (a.x.powi(2) + a.y.powi(2)).sqrt(); let distance_b = (b.x.powi(2) + b.y.powi(2)).sqrt(); distance_a .partial_cmp(&distance_b) .unwrap_or(Ordering::Equal) }) .ok_or("No suitable locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .into_iter() .filter(|loc| loc.density() >= 1000.0) .min_by(|loc1, loc2| { let dist1 = (loc1.x * loc1.x + loc1.y * loc1.y).sqrt(); let dist2 = (loc2.x * loc2.x + loc2.y * loc2.y).sqrt(); dist1.partial_cmp(&dist2).unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .filter(|location| location.density() >= 1000.0) .min_by(|a, b| { a.distance() .partial_cmp(&b.distance()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } } pub fn distance(&self) -> f64 { (self.x.powi(2) + self.y.powi(2)).sqrt() }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into()) }const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let mut res : Option<&Location> = None; for location in locations { if location.density() >= 1000.0 { res = match res { Some(r) => {if distance(r.x, r.y) > distance(location.x, location.y) { Some(location) } else { Some(r) }}, None => Some(location) } } } match res { Some(l) => Ok(l), None => Err("None Found".into()) }}pub fn distance(x: f64, y: f64) -> f64 { (x.powf(2.0) + y.powf(2.0)).powf(0.5)}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .into_iter() .filter(|l| l.density() >= 1000.0) .min_by(|a, b| { distance(a.x, a.y) .partial_cmp(&distance(b.x, b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}fn distance(x: f64, y: f64) -> f64 { ((x*x) + (y*y)).sqrt()}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a,b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};pub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations.iter().filter(|l| l.density() >= 1000.0) .min_by(|a, b| a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal)) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(location_array: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more location_array.iter() .filter(|&x| x.density() >= 1000.0) .min_by(|a,b| { a.distance_from_zero().partial_cmp(&b.distance_from_zero()) .unwrap_or(Ordering::Equal) }).ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } } pub fn distance_from_zero(&self) -> f64 { f64::sqrt((self.x.powf(2.0)) + (self.y.powf(2.0))) }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let loclist = locations.iter().filter(|loc| { loc.density() >= 1000. }).collect::<Vec<&Location>>(); loclist.into_iter().min_by(|a, b| { a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)).unwrap_or(Ordering::Equal) }).ok_or("Array empty".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more let loclist = locations.into_iter().filter(|loc| { loc.density() >= 1000. }).collect::<Vec<&Location>>(); loclist.into_iter().min_by(|a, b| { a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)).unwrap_or(Ordering::Equal) }).ok_or("Array empty".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &Vec<Location>) -> Result<Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into()).cloned()}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a,b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| { a.density() >= 1000.0 }) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { // 2. Find the nearest location // Only consider locations with a density of 1000 or more locations .iter() .filter(|a| { a.density() >= 1000.0 }) .min_by(|a,b| { a.x.hypot(a.y) .partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("nearest location not found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}
use std::{cmp::Ordering, error::Error, ops::Deref};// 1. Update the function signature to accept and return references to Locationspub fn find_most_dense_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .into_iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations.iter() .filter(|e| e.density() >= 1000.0) .min_by(|a, b| a.x.hypot(a.y).partial_cmp(&b.x.hypot(b.y)) .unwrap_or(Ordering::Equal)) .ok_or("No locations found".into())}const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;#[derive(Debug)]pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug)]pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}#[derive(Debug, Clone, PartialEq)]pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}#[derive(Debug, Clone, PartialEq)]pub struct Location { pub x: f64, pub y: f64, pub z: f64, pub area: f64, pub snow: Snowball,}impl Location { pub fn new(x: f64, y: f64, z: f64, area: f64, snow: impl Into<Snowball>) -> Self { Self { x, y, z, area, snow: snow.into(), } } pub fn density(&self) -> f64 { if self.area > 0.0 { *self.snow as f64 / self.area } else { 0.0 } }}