Problem 2
This commit is contained in:
parent
4fb3360022
commit
567a649c7d
2 changed files with 38 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ use std::fmt::Display;
|
|||
use clap::{Parser, Subcommand};
|
||||
|
||||
mod one;
|
||||
mod two;
|
||||
|
||||
#[derive(Parser, Default)]
|
||||
struct GlobalArgs {}
|
||||
|
|
|
|||
37
src/two.rs
Normal file
37
src/two.rs
Normal 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
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue