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 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 } }}
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)).expect("Comparation Error")) .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)).expect("Comparation Error")) .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| { 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 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.hypot(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 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| { 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(|x| x.density() >= 1000.0) .min_by(|l1, l2| { let l1_diff = (l1.x + l1.y).abs(); let l2_diff = (l2.x + l2.y).abs(); l1_diff.total_cmp(&l2_diff) }) .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(|l| l.density() >= 1000.0) .min_by(|l1, l2| { let l1_diff = (l1.x + l1.y).abs(); let l2_diff = (l2.x + l2.y).abs(); l1_diff.total_cmp(&l2_diff) }) .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 .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(|&x| x.density() >= 1000.0) .min_by(|a,b| { a.distance_from_origin() .partial_cmp(&b.distance_from_origin()) .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 } } pub fn distance_from_origin(&self) -> f64 { return self.x.hypot(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 locations .iter() .filter(|l| l.density() >= 1000.0) .min_by(|a, b| { let distance_a = a.x.abs() + a.y.abs(); let distance_b = b.x.abs() + b.y.abs(); distance_a .partial_cmp(&distance_b) .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_else(|| "No locations found".into())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .filter(|a| a.density() >= 1000.0) .min_by(|a,b| { let distance_a = (a.x + a.y).abs(); let distance_b = (b.x + b.y).abs(); distance_a.partial_cmp(&distance_b) .unwrap_or(Ordering::Equal) }) .ok_or_else(|| "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};const MIN_ACCEPTABLE_DENSITY: f64 = 1000.0;const ORIGIN: Location = Location { x: 0.0, y: 0.0, z: 0.0, area: 0.0, snow: Snowball(0)};// 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() >= MIN_ACCEPTABLE_DENSITY) .min_by(|a,b| { a.distance_to(&ORIGIN).partial_cmp(&b.distance_to(&ORIGIN)).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_to(&self, other: &Location) -> f64 { // Ignoring z ( (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 find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { locations .iter() .filter(|l| l.density() >= 1000.) .min_by(|a, b| { a.distance_from_origin() .partial_cmp(&b.distance_from_origin()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into()) // 2. Find the nearest location // Only consider locations with a density of 1000 or more}impl Location { pub fn distance_from_origin (&self) -> f64 { (self.x.powi(2i32) + self.y.powi(2i32)).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 .iter() .max_by(|a, b| { a.density() .partial_cmp(&b.density()) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found".into())}pub struct Coordinate { pub x: f64, pub y: f64,}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(|location| location.density() >= 1000.0); let current_location = Coordinate { x: 0.0, y: 0.0 }; locations .min_by(|a, b| { a.distance_to_home(¤t_location) .partial_cmp(&b.distance_to_home(¤t_location)) .unwrap_or(Ordering::Equal) }) .ok_or("No locations found 2".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_to_home(&self, home: &Coordinate) -> f64 { f64::sqrt(f64::powf(home.x - &self.x, 2.0)) + (f64::powf(home.y - &self.y, 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() .cloned() .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(|d| d.density() >= 1000.0) .min_by(|a, b| { (a.x * a.y).partial_cmp(&(&b.x * &b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("No good snow around!".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(|a| a.density() >= MINIMUM_DENSITY) .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;const MINIMUM_DENSITY: f64 = 1000.0;#[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 { return (self.x.powi(2) + self.y.powi(2)).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 locations .iter() .filter(|loc| loc.density() >= 1000.0) .min_by(|a, b| { a.distance_to_northpole() .partial_cmp(&b.distance_to_northpole()) .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_to_northpole(&self) -> f64 { self.x.hypot(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 .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(|l| l.density() >= 1000.0) .min_by(|l1, l2| { let d1 = l1.x.powi(2) + l1.y.powi(2); let d2 = l2.x.powi(2) + l2.y.powi(2); d1.partial_cmp(&d2).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(|loc| loc.density() >= 1000.0) .min_by(|a, b| { (a.x * a.y).partial_cmp(&(b.x * b.y)).unwrap_or(Ordering::Equal) }) .ok_or_else(|| "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 locations.iter() .filter(|loc| {loc.density() >= 1000.0}) .map(|loc| {(loc, loc.x*loc.x + loc.y*loc.y)}) .min_by(|a, b| {a.1.total_cmp(&b.1)}) .map(|tuple| {tuple.0}) .ok_or("No nearest 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())}pub fn find_nearest_location(locations: &[Location]) -> Result<&Location, Box<dyn Error>> { 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()) // 2. Find the nearest location // Only consider locations with a density of 1000 or more}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.powi(2) + a.y.powi(2)).partial_cmp(&(b.x.powi(2) + b.y.powi(2))) ).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(|v| v.density() >= 1000.0).min_by(|a, b| { (a.x*a.x as f64 + a.y*a.y as f64).sqrt() .partial_cmp(&(b.x*b.x as f64 + b.y*b.y as f64).sqrt()) .unwrap_or(Ordering::Equal) }).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: &[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 locations .into_iter() .filter(|x|x.density()>=1000.0f64) .min_by(|a, b| { (a.x*a.x+a.y*a.y) .partial_cmp(&(b.x*b.x+b.y*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 .into_iter() .filter(|x|x.density()>=1000.0f64) .min_by(|a, b| { (a.x*a.x+a.y*a.y) .partial_cmp(&(b.x*b.x+b.y*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 best".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(|s| { s.density() >= 1000.0 }).min_by(|x, x1| { (x.x * x.x + x.y * x.y).partial_cmp(&(x1.x * x1.x + x1.y * x1.y)).unwrap() }).ok_or("no nearest".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| { a.distance((0.0, 0.0)) .partial_cmp(&b.distance((0.0, 0.0))) .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, (x, y): (f64, f64)) -> f64 { (self.x - x) * (self.x - x) + (self.y - y) * (self.y - y) } 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(|loc| loc.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(|loc| loc.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) .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()) .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 //.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()) //.collect::<Vec<Location>>(); locations.iter() .filter(|loc| loc.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("location Not 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.x.hypot(a.x) .partial_cmp(&b.y.hypot(b.y)) .unwrap_or(Ordering::Equal) }) .ok_or("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 .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(|loc| loc.density() >= 1000.0) .min_by(|a, b| { a.x.hypot(a.x) .partial_cmp(&b.y.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 founds".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(|loc| loc.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 } }}