Skip to content

Instantly share code, notes, and snippets.

@sgeos
Created August 10, 2023 02:40
Show Gist options
  • Save sgeos/6bf96faf981b53bd256fcc8f0d19e501 to your computer and use it in GitHub Desktop.
Save sgeos/6bf96faf981b53bd256fcc8f0d19e501 to your computer and use it in GitHub Desktop.
Water fasting calculations written in Rust. Most code written by chat GPT. Prompts, combining code, and refactoring done by a human engineer.
use std::fmt;
#[derive(Debug, Copy, Clone)]
#[allow(dead_code)]
enum Sex {
Male,
Female,
}
impl Sex {
fn iter() -> impl Iterator<Item = Sex> {
[Sex::Male, Sex::Female].iter().cloned()
}
}
impl fmt::Display for Sex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let sex_str = match *self {
Sex::Male => "Male",
Sex::Female => "Female",
};
write!(f, "{}", sex_str)
}
}
#[derive(Debug, Copy, Clone)]
#[allow(dead_code)]
enum ActivityLevel {
Sedentary,
LightlyActive,
ModeratelyActive,
VeryActive,
ExtremelyActive,
}
impl ActivityLevel {
fn iter() -> impl Iterator<Item = ActivityLevel> {
[
ActivityLevel::Sedentary,
ActivityLevel::LightlyActive,
ActivityLevel::ModeratelyActive,
ActivityLevel::VeryActive,
ActivityLevel::ExtremelyActive,
]
.iter()
.cloned()
}
}
impl fmt::Display for ActivityLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let activity_level_str = match *self {
ActivityLevel::Sedentary => "Sedentary",
ActivityLevel::LightlyActive => "Lightly Active",
ActivityLevel::ModeratelyActive => "Moderately Active",
ActivityLevel::VeryActive => "Very Active",
ActivityLevel::ExtremelyActive => "Extremely Active",
};
write!(f, "{}", activity_level_str)
}
}
fn calculate_bmr(
age: u32,
sex: Sex,
weight: f64,
height: f64,
) -> f64 {
// Calculate BMR using the Mifflin-St Jeor Equation
match sex {
Sex::Male => 10.0 * weight + 6.25 * height - 5.0 * age as f64 + 5.0,
Sex::Female => 10.0 * weight + 6.25 * height - 5.0 * age as f64 - 161.0,
}
}
fn estimate_calories_expended(
age: u32,
sex: Sex,
weight: f64,
height: f64,
activity_level: ActivityLevel,
days_since_beginning_fast: u32,
) -> f64 {
let bmr = calculate_bmr(age, sex, weight, height);
// Define metabolic rate based on activity level
let metabolic_rate = match activity_level {
ActivityLevel::Sedentary => 1.2,
ActivityLevel::LightlyActive => 1.375,
ActivityLevel::ModeratelyActive => 1.55,
ActivityLevel::VeryActive => 1.725,
ActivityLevel::ExtremelyActive => 1.9,
};
// Apply the metabolic rate
let adjusted_bmr = bmr * metabolic_rate;
// Optionally, apply a metabolic slowdown factor based on days of fasting
// This is a simplified assumption; real metabolic changes would likely be more complex
let slowdown_factor = 1.0 - (days_since_beginning_fast as f64 * 0.01).min(0.10); // up to 10% slowdown
// Calculate total daily energy expenditure (TDEE)
let tdee = adjusted_bmr * slowdown_factor;
tdee
}
fn estimate_kilograms_lost(
age: u32,
sex: Sex,
weight: f64,
height: f64,
activity_level: ActivityLevel,
total_days_fasting: u32,
) -> f64 {
let mut total_calories_expended = 0.0;
// Iterate through each day of the fast, calculating the daily calories expended
for day in 0..total_days_fasting {
total_calories_expended +=
estimate_calories_expended(age, sex, weight, height, activity_level, day);
}
// Convert total calories into kilograms (assuming 7700 calories per kilogram)
let kilograms_lost = total_calories_expended / 7700.0;
kilograms_lost
}
fn calculate_bmi(
weight: f64,
height: f64,
) -> f64 {
10_000.0 * weight / height / height
}
fn days_to_target_bmi(
age: u32,
sex: Sex,
mut weight: f64,
height: f64,
activity_level: ActivityLevel,
target_bmi: f64,
) -> u32 {
let mut days_fasting = 0;
while target_bmi < calculate_bmi(weight, height) {
// Estimate the calories expended for the current day
let daily_calories_expended =
estimate_calories_expended(age, sex, weight, height, activity_level, days_fasting);
// Convert the calories into kilograms lost (assuming 7700 calories per kilogram)
let kilograms_lost = daily_calories_expended / 7700.0;
// Update the weight
weight -= kilograms_lost;
// Increment the days of fasting
days_fasting += 1;
}
days_fasting
}
#[no_mangle]
pub extern "C" fn run() {
let age = 25;
let height = 175.0; // in cm
let weight = 75.0;
let total_days_fasting = 14;
let days_since_beginning_fast = total_days_fasting / 2;
let target_bmi = 25.0;
println!("Water Fasting Calculations");
println!("");
for sex in Sex::iter() {
for activity_level in ActivityLevel::iter() {
let bmi = calculate_bmi(weight, height);
let bmr = calculate_bmr(age, sex, weight, height);
println!(
"{age} Year Old {height}cm {activity_level} {sex}, starting weight {weight} kg, BMI {bmi:.1}, BMR {bmr:.1}"
);
let calories = estimate_calories_expended(
age,
sex,
weight,
height,
activity_level,
days_since_beginning_fast,
);
println!(
"Estimated calories expended in on day {days_since_beginning_fast}: {calories:.2}"
);
let kilograms_lost = estimate_kilograms_lost(
age,
sex,
weight,
height,
activity_level,
total_days_fasting,
);
println!(
"Estimated kilograms lost during {total_days_fasting} days of fasting: {kilograms_lost:.2}"
);
let days_fasting =
days_to_target_bmi(age, sex, weight, height, activity_level, target_bmi);
println!("Estimated days of fasting to reach BMI {target_bmi}: {days_fasting}");
println!("");
}
}
}
fn main() {
run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment