This commit is contained in:
Aadi Desai 2023-12-04 17:12:08 +00:00
parent 638f878bd9
commit 875de26857
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
4 changed files with 77 additions and 0 deletions

2
Cargo.lock generated
View file

@ -260,6 +260,8 @@ name = "cch23-8bit"
version = "0.1.0"
dependencies = [
"axum 0.7.1",
"serde",
"serde_json",
"shuttle-axum",
"shuttle-runtime",
"tokio",

View file

@ -5,6 +5,8 @@ edition = "2021"
[dependencies]
axum = "0.7.1"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
shuttle-axum = { version = "0.34.1", default-features = false, features = ["axum-0-7"] }
shuttle-runtime = "0.34.1"
tokio = "1.34.0"

71
src/cal/day04.rs Normal file
View file

@ -0,0 +1,71 @@
use axum::{extract::Json, response::IntoResponse, routing::post, 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
),
})
}

View file

@ -1,8 +1,10 @@
mod day00;
mod day01;
mod day04;
pub(crate) fn router() -> axum::Router {
axum::Router::new()
.nest("/", day00::router())
.nest("/", day01::router())
.nest("/", day04::router())
}