Skip to content

Commit

Permalink
fix: other days with cch23-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
MatzHilven committed Dec 11, 2023
1 parent 1581ed2 commit 0d53773
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 51 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ shuttle-runtime = "0.34.0"
tokio = "1.26.0"
tempfile = "3.8.1"
actix-multipart = "0.6.1"
fancy-regex = "0.12.0"
12 changes: 5 additions & 7 deletions src/days/eight/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(part_2);
}

async fn get_weight(id: u32) -> u64 {
async fn get_weight(id: u32) -> f64 {
let body = reqwest::get(format!("https://pokeapi.co/api/v2/pokemon/{id}"))
.await
.unwrap()
Expand All @@ -18,20 +18,18 @@ async fn get_weight(id: u32) -> u64 {
.unwrap();

let json: Value = serde_json::from_str(body.as_str()).unwrap();
let weight = json["weight"].as_u64().unwrap();

weight / 10
json["weight"].as_f64().unwrap()
}

#[get("/8/weight/{pokedex_number}")]
async fn part_1(pokedex_number: web::Path<u32>) -> impl Responder {
HttpResponse::Ok().body(format!("{}", get_weight(pokedex_number.into_inner()).await))
HttpResponse::Ok().body(format!("{}", get_weight(pokedex_number.into_inner()).await / 10.0))
}

#[get("/8/drop/{pokedex_number}")]
async fn part_2(pokedex_number: web::Path<u32>) -> impl Responder {
let weight = get_weight(pokedex_number.into_inner()).await as f64;
let momentum = (2.0 * G * HEIGHT).sqrt() * weight;
let weight = get_weight(pokedex_number.into_inner()).await;
let momentum = (2.0 * G * HEIGHT).sqrt() * weight / 10.0;
HttpResponse::Ok().body(format!("{}", momentum))
}

Expand Down
81 changes: 38 additions & 43 deletions src/days/seven/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use actix_web::{get, web, HttpRequest, HttpResponse, Responder};
use std::collections::HashMap;
use actix_web::{get, HttpRequest, HttpResponse, Responder, web};
use base64::Engine;
use base64::engine::general_purpose;
use serde::{Deserialize, Serialize};

pub fn configure(cfg: &mut web::ServiceConfig) {
Expand Down Expand Up @@ -58,52 +60,45 @@ struct BakeCookieResponse {
pantry: Pantry,
}

#[derive(serde::Deserialize)]
struct Bake {
recipe: HashMap<String, usize>,
pantry: HashMap<String, usize>,
}

#[get("/7/bake")]
async fn part_2(req: HttpRequest) -> impl Responder {
match req.cookie("recipe") {
Some(cookie) => {
let value = cookie.value();
match base64::engine::general_purpose::STANDARD.decode(value) {
Ok(decoded) => {
let decoded = String::from_utf8(decoded).unwrap();
let bake_cookie: BakeCookie = serde_json::from_str(&decoded).unwrap();

let recipe = bake_cookie.recipe;
let mut pantry = bake_cookie.pantry;

let mut cookies = 0;
loop {
if recipe.flour > pantry.flour {
break;
}
if recipe.sugar > pantry.sugar {
break;
}
if recipe.butter > pantry.butter {
break;
}
if recipe.baking_powder > pantry.baking_powder {
break;
}
if recipe.chocolate_chips > pantry.chocolate_chips {
break;
}

cookies += 1;
pantry.flour -= recipe.flour;
pantry.sugar -= recipe.sugar;
pantry.butter -= recipe.butter;
pantry.baking_powder -= recipe.baking_powder;
pantry.chocolate_chips -= recipe.chocolate_chips;
}

HttpResponse::Ok().json(BakeCookieResponse { cookies, pantry })
}
Err(_) => HttpResponse::BadRequest().body("Invalid base64"),
async fn part_2(request: HttpRequest) -> impl Responder {
let cookie = request.cookie("recipe").unwrap();
let encoded = cookie.value();

let decoded = general_purpose::STANDARD.decode(encoded).unwrap();
let mut bake: Bake = serde_json::from_slice(&decoded).unwrap();

let cookies = bake
.recipe
.iter()
.map(|(k, &v)| {
if v == 0 {
usize::MAX
} else {
bake.pantry.get(k).map(|&a| a / v).unwrap_or_default()
}
})
.min()
.unwrap_or_default();

for (key, &value) in &bake.recipe {
if let Some(p) = bake.pantry.get_mut(key) {
*p -= cookies * value;
}
None => HttpResponse::BadRequest().body("No cookie found"),
}

HttpResponse::Ok().json(serde_json::json!(
{
"cookies": cookies,
"pantry": bake.pantry,
}
))
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion src/days/six/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use actix_web::{post, web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use fancy_regex::Regex;

pub fn configure(cfg: &mut web::ServiceConfig) {
// cfg.service(part_1);
Expand All @@ -25,7 +26,7 @@ struct ElfResponse {
#[post("/6")]
async fn part_2(string: String) -> impl Responder {
let elf = string.matches("elf").count();
let elf_on_a_shelf = string.matches("elf on a shelf").count();
let elf_on_a_shelf = Regex::new(r"(?=(elf on a shelf))").unwrap().find_iter(&string).count();
let shelf_with_no_elf_on_it = string.matches("shelf").count() - elf_on_a_shelf;

HttpResponse::Ok().json(ElfResponse {
Expand Down

0 comments on commit 0d53773

Please sign in to comment.