-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use axum::{http::StatusCode, response::IntoResponse, routing::get}; | ||
|
||
pub fn router() -> axum::Router { | ||
axum::Router::new() | ||
.route("/", get(hello_world)) | ||
.route("/-1/error", get(internal_server_error)) | ||
} | ||
|
||
pub async fn internal_server_error() -> impl IntoResponse { | ||
StatusCode::INTERNAL_SERVER_ERROR | ||
} | ||
|
||
async fn hello_world() -> impl IntoResponse { | ||
"Hello, World!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use axum::{extract::Path, http::StatusCode, response::IntoResponse, routing::get}; | ||
use std::str::FromStr; | ||
|
||
pub fn router() -> axum::Router { | ||
axum::Router::new().route("/1/*ids", get(packet_ids)) | ||
} | ||
|
||
pub async fn packet_ids(Path(ids): Path<String>) -> impl IntoResponse { | ||
let packet_ids: Vec<i32> = ids | ||
.split('/') | ||
// TODO: How to handle this gracefully? | ||
.map(|id_str| i32::from_str(id_str).unwrap_or(0)) | ||
.collect(); | ||
|
||
// validate on the length of the ids | ||
if packet_ids.len() > 20 { | ||
return ( | ||
StatusCode::BAD_REQUEST, | ||
"packet ids must be between 1 and 20 inclusive packets in a sled", | ||
) | ||
.into_response(); | ||
} | ||
let result = packet_ids.iter().fold(0, |acc, prev| acc ^ prev).pow(3); | ||
format!("{result}").into_response() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use axum::{http::StatusCode, response::IntoResponse, routing::post, Json}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub fn router() -> axum::Router { | ||
axum::Router::new() | ||
.route("/4/strength", post(sum_strength)) | ||
.route("/4/contest", post(contest)) | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Reindeer { | ||
name: String, | ||
strength: i32, | ||
} | ||
async fn sum_strength(Json(reindeers): Json<Vec<Reindeer>>) -> impl IntoResponse { | ||
reindeers | ||
.iter() | ||
.fold(0, |acc, r| acc + r.strength) | ||
.to_string() | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ContestReindeer { | ||
pub name: String, | ||
pub strength: i32, | ||
pub speed: f32, | ||
pub height: u32, | ||
pub antler_width: u32, | ||
pub snow_magic_power: i64, | ||
pub favorite_food: String, | ||
#[serde(alias = "cAnD13s_3ATeN-yesT3rdAy")] | ||
pub candies: i32, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq)] | ||
pub struct ContestResult { | ||
pub fastest: String, | ||
pub tallest: String, | ||
pub magician: String, | ||
pub consumer: String, | ||
} | ||
async fn contest(Json(reindeers): Json<Vec<ContestReindeer>>) -> impl IntoResponse { | ||
let fastest = reindeers.iter().max_by(|a, b| a.strength.cmp(&b.strength)); | ||
let tallest = reindeers.iter().max_by(|a, b| a.height.cmp(&b.height)); | ||
let magician = reindeers | ||
.iter() | ||
.max_by(|a, b| a.snow_magic_power.cmp(&b.snow_magic_power)); | ||
let candiest = reindeers.iter().max_by(|a, b| a.candies.cmp(&b.candies)); | ||
|
||
match (fastest, tallest, magician, candiest) { | ||
(Some(f), Some(t), Some(m), Some(c)) => Json(ContestResult { | ||
fastest: format!( | ||
"Speeding past the finish line with a strength of {} is {}", | ||
f.strength, f.name | ||
), | ||
tallest: format!( | ||
"{} is standing tall with his {} cm wide antlers", | ||
t.name, t.antler_width | ||
), | ||
magician: format!( | ||
"{} could blast you away with a snow magic power of {}", | ||
m.name, m.snow_magic_power | ||
), | ||
consumer: format!("{} ate lots of candies, but also some grass", c.name), | ||
}) | ||
.into_response(), | ||
_ => (StatusCode::BAD_REQUEST, "Invalid contest").into_response(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use axum::{response::IntoResponse, routing::post, Json}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub fn router() -> axum::Router { | ||
axum::Router::new().route("/6", post(elf_on_shelf)) | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq)] | ||
pub struct ElfOnShelfResult { | ||
pub elf: u32, | ||
#[serde(alias = "elf on a shelf")] | ||
pub elf_on_shelf: u32, | ||
#[serde(alias = "shelf with no elf on it")] | ||
pub shelf_with_no_elf: u32, | ||
} | ||
|
||
async fn elf_on_shelf(elf_text: String) -> impl IntoResponse { | ||
let shelf = elf_text.matches("shelf").count() as u32; | ||
let elf_on_shelf = elf_text.matches("elf on a shelf").count() as u32; | ||
let elf = elf_text.matches("elf").count() as u32; | ||
Json(ElfOnShelfResult { | ||
elf, | ||
elf_on_shelf, | ||
shelf_with_no_elf: shelf - elf_on_shelf, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
mod day0; | ||
mod day1; | ||
mod day4; | ||
mod day6; | ||
|
||
pub use day4::{ContestReindeer, ContestResult}; | ||
pub use day6::ElfOnShelfResult; | ||
|
||
pub fn router() -> axum::Router { | ||
axum::Router::new() | ||
.nest("/", day0::router()) | ||
.nest("/", day1::router()) | ||
// Days 2 and 3 are omitted due to being weekends | ||
.nest("/", day4::router()) | ||
.nest("/", day6::router()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters