-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.rs
71 lines (65 loc) · 1.92 KB
/
day04.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use axum::{response::IntoResponse, routing::post, Json, Router};
pub(crate) fn router() -> Router {
Router::new()
.route("/4/strength", post(strength))
.route("/4/contest", post(contest))
}
#[allow(dead_code)]
#[derive(serde::Deserialize)]
struct Reindeer {
name: String,
strength: i32,
}
async fn strength(Json(reindeer): Json<Vec<Reindeer>>) -> impl IntoResponse {
reindeer
.into_iter()
.map(|r| r.strength)
.sum::<i32>()
.to_string()
}
#[derive(serde::Deserialize)]
struct AdvReindeer {
name: String,
strength: i32,
speed: f32,
height: i32,
antler_width: i32,
snow_magic_power: i32,
favorite_food: String,
#[serde(rename = "cAnD13s_3ATeN-yesT3rdAy")]
candies: i32,
}
#[derive(serde::Serialize)]
struct ContestResult {
fastest: String,
tallest: String,
magician: String,
consumer: String,
}
async fn contest(Json(reindeer): Json<Vec<AdvReindeer>>) -> Json<ContestResult> {
let fastest = reindeer
.iter()
.max_by(|l, r| l.speed.total_cmp(&r.speed))
.unwrap();
let tallest = reindeer.iter().max_by_key(|r| r.height).unwrap();
let magician = reindeer.iter().max_by_key(|r| r.snow_magic_power).unwrap();
let consumer = reindeer.iter().max_by_key(|r| r.candies).unwrap();
Json(ContestResult {
fastest: format!(
"Speeding past the finish line with a strength of {} is {}",
fastest.strength, fastest.name
),
tallest: format!(
"{} is standing tall with his {} cm wide antlers",
tallest.name, tallest.antler_width
),
magician: format!(
"{} could blast you away with a snow magic power of {}",
magician.name, magician.snow_magic_power
),
consumer: format!(
"{} ate lots of candies, but also some {}",
consumer.name, consumer.favorite_food
),
})
}