45 lines
963 B
Rust
45 lines
963 B
Rust
const LOOP_COUNT: i32 = 1_000_000;
|
|
const EXPECTED_CHECKSUM: i32 = 1_185_071;
|
|
|
|
fn configured_loop_count() -> i32 {
|
|
let mut input = String::new();
|
|
if std::io::stdin().read_line(&mut input).is_err() {
|
|
return LOOP_COUNT;
|
|
}
|
|
|
|
input
|
|
.trim()
|
|
.parse::<i32>()
|
|
.ok()
|
|
.filter(|value| *value > 0)
|
|
.unwrap_or(LOOP_COUNT)
|
|
}
|
|
|
|
fn branch_loop(limit: i32) -> i32 {
|
|
let mut i = 0;
|
|
let mut acc = 7;
|
|
|
|
while i < limit {
|
|
acc = if acc > 1_000_000_000 {
|
|
acc - 1_000_000_000
|
|
} else {
|
|
acc
|
|
};
|
|
acc = if i < 500_000 { acc + 3 } else { acc + 7 };
|
|
acc = if acc > 1_234_567 {
|
|
acc - 1_234_567
|
|
} else {
|
|
acc + 11
|
|
};
|
|
i += 1;
|
|
}
|
|
|
|
acc
|
|
}
|
|
|
|
fn main() {
|
|
let result = branch_loop(configured_loop_count());
|
|
println!("{}", result);
|
|
std::process::exit(if result == EXPECTED_CHECKSUM { 0 } else { 1 });
|
|
}
|