Day 6 fixes

This commit is contained in:
Aadi Desai 2023-12-12 00:40:11 +00:00
parent dbb73b6bb3
commit 79ab2f9787
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM

View file

@ -7,13 +7,20 @@ pub(crate) fn router() -> Router {
#[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 = body.matches("elf on a shelf").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,