This commit is contained in:
Aadi Desai 2023-12-12 18:06:21 +00:00
parent fd0413d1ae
commit 0b2c9f1465
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
4 changed files with 114 additions and 0 deletions

14
Cargo.lock generated
View file

@ -316,6 +316,7 @@ dependencies = [
"axum 0.7.2", "axum 0.7.2",
"axum-extra", "axum-extra",
"base64", "base64",
"chrono",
"image", "image",
"reqwest", "reqwest",
"serde", "serde",
@ -325,6 +326,8 @@ dependencies = [
"tokio", "tokio",
"tower-http 0.5.0", "tower-http 0.5.0",
"tracing", "tracing",
"ulid",
"uuid",
] ]
[[package]] [[package]]
@ -2851,6 +2854,17 @@ version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "ulid"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e37c4b6cbcc59a8dcd09a6429fbc7890286bcbb79215cea7b38a3c4c0921d93"
dependencies = [
"rand",
"serde",
"uuid",
]
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.7.0" version = "2.7.0"

View file

@ -7,6 +7,7 @@ edition = "2021"
axum = { version = "0.7.2", features = ["macros", "multipart"] } axum = { version = "0.7.2", features = ["macros", "multipart"] }
axum-extra = { version = "0.9.0", features = ["typed-header"] } axum-extra = { version = "0.9.0", features = ["typed-header"] }
base64 = "0.21.5" base64 = "0.21.5"
chrono = { version = "0.4.31", default-features = false, features = ["std"] }
image = "0.24.7" image = "0.24.7"
reqwest = { version = "0.11.22", features = ["json"] } reqwest = { version = "0.11.22", features = ["json"] }
serde = { version = "1.0.193", features = ["derive"] } serde = { version = "1.0.193", features = ["derive"] }
@ -16,3 +17,5 @@ shuttle-runtime = "0.35.0"
tokio = "1.34.0" tokio = "1.34.0"
tower-http = { version = "0.5.0", features = ["fs"] } tower-http = { version = "0.5.0", features = ["fs"] }
tracing = "0.1.40" tracing = "0.1.40"
ulid = { version = "1.1.0", features = ["uuid", "serde"] }
uuid = "1.6.1"

95
src/cal/day12.rs Normal file
View file

@ -0,0 +1,95 @@
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Json, Router,
};
use chrono::{DateTime, Datelike, Utc};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
time::SystemTime,
};
struct Day12State {
times: HashMap<String, SystemTime>,
}
pub(crate) fn router() -> Router {
Router::new()
.route("/12/save/:string", post(save))
.route("/12/load/:string", get(load))
.route("/12/ulids", post(ulids))
.route("/12/ulids/:weekday", post(ulids_weekday))
.with_state(Arc::new(Mutex::new(Day12State {
times: HashMap::new(),
})))
}
async fn save(State(state): State<Arc<Mutex<Day12State>>>, Path(string): Path<String>) {
state
.lock()
.expect("Should not error unless another thread panics")
.times
.insert(string, SystemTime::now());
}
async fn load(
State(state): State<Arc<Mutex<Day12State>>>,
Path(string): Path<String>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
Ok(state
.lock()
.expect("Should not error unless another thread panics")
.times
.get(&string)
.ok_or((
StatusCode::NOT_FOUND,
"String not saved previously".to_string(),
))?
.elapsed()
.expect("Time should not go backwards")
.as_secs()
.to_string())
}
async fn ulids(Json(ulids): Json<Vec<ulid::Ulid>>) -> Json<Vec<uuid::Uuid>> {
Json(ulids.into_iter().map(Into::into).rev().collect())
}
#[derive(serde::Serialize)]
struct UlidsWeekday {
#[serde(rename = "christmas eve")]
christmas_eve: usize,
weekday: usize,
#[serde(rename = "in the future")]
in_the_future: usize,
#[serde(rename = "LSB is 1")]
lsb_is_1: usize,
}
#[axum::debug_handler]
async fn ulids_weekday(
Path(weekday): Path<u32>,
Json(ulids): Json<Vec<ulid::Ulid>>,
) -> Json<UlidsWeekday> {
let lsb_is_1 = ulids.iter().filter(|u| u.random() & 1 == 1).count();
let ulids: Vec<DateTime<Utc>> = ulids.into_iter().map(|u| u.datetime().into()).collect();
let christmas_eve = ulids
.iter()
.filter(|u| u.month() == 12 && u.day() == 24)
.count();
let weekday = ulids
.iter()
.filter(|u| u.weekday().num_days_from_monday() == weekday)
.count();
let now = Utc::now();
let in_the_future = ulids.iter().filter(|&u| u > &now).count();
Json(UlidsWeekday {
christmas_eve,
weekday,
in_the_future,
lsb_is_1,
})
}

View file

@ -5,6 +5,7 @@ mod day06;
mod day07; mod day07;
mod day08; mod day08;
mod day11; mod day11;
mod day12;
pub(crate) fn router() -> axum::Router { pub(crate) fn router() -> axum::Router {
axum::Router::new() axum::Router::new()
@ -15,4 +16,5 @@ pub(crate) fn router() -> axum::Router {
.nest("/", day07::router()) .nest("/", day07::router())
.nest("/", day08::router()) .nest("/", day08::router())
.nest("/", day11::router()) .nest("/", day11::router())
.nest("/", day12::router())
} }