Problem 2

This commit is contained in:
Quentin Boyer 2024-11-27 12:26:47 +01:00
parent 4fb3360022
commit 567a649c7d
2 changed files with 38 additions and 0 deletions

View file

@ -3,6 +3,7 @@ use std::fmt::Display;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
mod one; mod one;
mod two;
#[derive(Parser, Default)] #[derive(Parser, Default)]
struct GlobalArgs {} struct GlobalArgs {}

37
src/two.rs Normal file
View file

@ -0,0 +1,37 @@
use crate::{GlobalArgs, Problem};
#[derive(clap::Parser)]
pub struct Args {
#[clap(default_value = "4000000")]
until: u64,
}
impl Problem for Args {
type Solution = u64;
fn solve(self, _: GlobalArgs) -> color_eyre::Result<u64> {
let mut a = 1;
let mut b = 2;
let mut sum = b;
while b < self.until {
(a, b) = (b, a + b);
if b % 2 == 0 {
sum += b;
}
}
Ok(sum)
}
}
#[cfg(test)]
#[test]
fn rsp() {
assert_eq!(
Args { until: 4000000 }
.solve(GlobalArgs::default())
.unwrap(),
4613732
);
}