-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
30 lines (27 loc) · 788 Bytes
/
day06.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
use axum::{routing::post, Json, Router};
pub(crate) fn router() -> Router {
Router::new().route("/6", post(count_elf))
}
#[derive(serde::Serialize)]
struct CountElf {
elf: usize,
#[serde(rename = "elf on a shelf")]
elf_on_a_shelf: usize,
#[serde(rename = "shelf with no elf on it")]
shelf_with_no_elf: usize,
}
async fn count_elf(body: String) -> Json<CountElf> {
let elf = body.matches("elf").count();
let elf_on_a_shelf = "elf on a shelf".as_bytes();
let elf_on_a_shelf = body
.as_bytes()
.windows(14)
.filter(|&w| w == elf_on_a_shelf)
.count();
let shelf_with_no_elf = body.matches("shelf").count() - elf_on_a_shelf;
Json(CountElf {
elf,
elf_on_a_shelf,
shelf_with_no_elf,
})
}