This commit is contained in:
Aadi Desai 2023-12-22 14:33:53 +00:00
parent 581e83b241
commit a1d50f0948
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
4 changed files with 143 additions and 4 deletions

58
Cargo.lock generated
View file

@ -459,6 +459,8 @@ dependencies = [
"futures", "futures",
"image", "image",
"isocountry", "isocountry",
"multimap",
"pathfinding",
"radians", "radians",
"regex", "regex",
"reqwest", "reqwest",
@ -714,6 +716,18 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5"
[[package]]
name = "deprecate-until"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a3767f826efbbe5a5ae093920b58b43b01734202be697e1354914e862e8e704"
dependencies = [
"proc-macro2",
"quote",
"semver",
"syn 2.0.39",
]
[[package]] [[package]]
name = "der" name = "der"
version = "0.7.8" version = "0.7.8"
@ -858,6 +872,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6" checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6"
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.28" version = "1.0.28"
@ -1480,6 +1500,15 @@ dependencies = [
"hashbrown 0.14.3", "hashbrown 0.14.3",
] ]
[[package]]
name = "integer-sqrt"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "276ec31bcb4a9ee45f58bec6f9ec700ae4cf4f4f8f2fa7e06cb406bd5ffdd770"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.9.0" version = "2.9.0"
@ -1741,6 +1770,12 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "multimap"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1a5d38b9b352dbd913288736af36af41c48d61b1a8cd34bcecd727561b7d511"
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.11" version = "0.2.11"
@ -2078,6 +2113,21 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
[[package]]
name = "pathfinding"
version = "4.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f4a3f5089b981000cb50ec24320faf7a19649a45e8730e4adf49f78f066528"
dependencies = [
"deprecate-until",
"fixedbitset",
"indexmap 2.1.0",
"integer-sqrt",
"num-traits",
"rustc-hash",
"thiserror",
]
[[package]] [[package]]
name = "pem" name = "pem"
version = "3.0.2" version = "3.0.2"
@ -3298,18 +3348,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.50" version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.50" version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -15,6 +15,8 @@ country-boundaries = "1.2.0"
futures = "0.3.29" futures = "0.3.29"
image = "0.24.7" image = "0.24.7"
isocountry = "0.3.2" isocountry = "0.3.2"
multimap = { version = "0.9.1", default-features = false }
pathfinding = "4.8.0"
radians = "0.3.1" radians = "0.3.1"
regex = "1.10.2" regex = "1.10.2"
reqwest = { version = "0.11.22", features = ["json"] } reqwest = { version = "0.11.22", features = ["json"] }

85
src/cal/day22.rs Normal file
View file

@ -0,0 +1,85 @@
use axum::{http::StatusCode, response::IntoResponse, routing::post, Router};
pub(crate) fn router() -> Router {
Router::new()
.route("/22/integers", post(integers))
.route("/22/rocket", post(rocket))
}
async fn integers(nums: String) -> Result<impl IntoResponse, StatusCode> {
let mut nums = nums
.lines()
.map(|s| s.parse::<u64>().expect("All lines should be valid u64s"))
.collect::<Vec<_>>();
nums.sort_unstable();
for i in (0..nums.len()).step_by(2) {
if i == (nums.len() - 1) || nums[i] != nums[i + 1] {
return Ok("🎁".repeat(nums[i] as usize));
}
}
Err(StatusCode::BAD_REQUEST)
}
async fn rocket(input: String) -> Result<impl IntoResponse, StatusCode> {
fn star_loc(s: &str) -> (i32, i32, i32) {
let mut s = s.split_ascii_whitespace().take(3);
(
s.next().expect("Exists").parse().expect("Valid i32"),
s.next().expect("Exists").parse().expect("Valid i32"),
s.next().expect("Exists").parse().expect("Valid i32"),
)
}
fn star_dist(a: (i32, i32, i32), b: (i32, i32, i32)) -> f32 {
let total = (a.0 - b.0).pow(2) + (a.1 - b.1).pow(2) + (a.2 - b.2).pow(2);
(total as f32).sqrt()
}
fn portal_path(s: &str) -> (u32, u32) {
let mut s = s.split_ascii_whitespace().take(3);
(
s.next().expect("Exists").parse().expect("Valid u32"),
s.next().expect("Exists").parse().expect("Valid u32"),
)
}
let mut input = input.lines();
let s_cnt = input
.next()
.expect("Line exists")
.parse::<u32>()
.expect("In 2..=100");
let mut stars = vec![];
for _ in 0..s_cnt {
let s = input.next().expect("Line exists");
stars.push(star_loc(s));
}
let p_cnt = input
.next()
.expect("Line exists")
.parse::<u32>()
.expect("In 1..=100");
let mut portals = multimap::MultiMap::<u32, u32>::new();
for _ in 0..p_cnt {
let (s, e) = portal_path(input.next().expect("Line exists"));
portals.insert(s, e);
}
let Some(path) = pathfinding::directed::bfs::bfs(
&0u32,
|n| portals.get_vec(n).cloned().unwrap_or_default(),
|&n| n == s_cnt - 1,
) else {
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};
let d = path
.windows(2)
.map(|p| star_dist(stars[p[0] as usize], stars[p[1] as usize]))
.sum::<f32>();
Ok(format!("{} {d:.3}", path.len() - 1))
}

View file

@ -14,6 +14,7 @@ mod day18;
mod day19; mod day19;
mod day20; mod day20;
mod day21; mod day21;
mod day22;
pub(crate) fn router(pool: sqlx::PgPool) -> axum::Router { pub(crate) fn router(pool: sqlx::PgPool) -> axum::Router {
axum::Router::new() axum::Router::new()
@ -33,4 +34,5 @@ pub(crate) fn router(pool: sqlx::PgPool) -> axum::Router {
.nest("/", day19::router()) .nest("/", day19::router())
.nest("/", day20::router()) .nest("/", day20::router())
.nest("/", day21::router()) .nest("/", day21::router())
.nest("/", day22::router())
} }