fn main() {
let lines_vec = std::io::stdin()
.lines()
.map(|x| x.unwrap())
.collect::<Vec<_>>();
println!(
"{}",
lines_vec[0]
.bytes()
.zip(lines_vec[1].bytes())
.zip(lines_vec[2].bytes())
.rev()
.scan(
(0, 0),
|(normal, with_carry): &mut (u64, u64), ((a, b), c): ((u8, u8), u8)| {
let a = a - b'0';
let b = b - b'0';
let c = c - b'0';
let new_normal = if a + b == c { *normal + 1 } else { 0 }
+ if a + b + 1 == c { *with_carry } else { 0 };
let new_with_carry = if a + b == c + 10 { *normal + 1 } else { 0 }
+ if a + b + 1 == c + 10 { *with_carry } else { 0 };
// eprintln!(
// "{} + {} = {}, n{} c{} -> n{} c{}",
// a, b, c, normal, with_carry, new_normal, new_with_carry
// );
*normal = new_normal;
*with_carry = new_with_carry;
Some(new_normal)
}
)
.sum::<u64>()
);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | fn main() { let lines_vec = std::io::stdin() .lines() .map(|x| x.unwrap()) .collect::<Vec<_>>(); println!( "{}", lines_vec[0] .bytes() .zip(lines_vec[1].bytes()) .zip(lines_vec[2].bytes()) .rev() .scan( (0, 0), |(normal, with_carry): &mut (u64, u64), ((a, b), c): ((u8, u8), u8)| { let a = a - b'0'; let b = b - b'0'; let c = c - b'0'; let new_normal = if a + b == c { *normal + 1 } else { 0 } + if a + b + 1 == c { *with_carry } else { 0 }; let new_with_carry = if a + b == c + 10 { *normal + 1 } else { 0 } + if a + b + 1 == c + 10 { *with_carry } else { 0 }; // eprintln!( // "{} + {} = {}, n{} c{} -> n{} c{}", // a, b, c, normal, with_carry, new_normal, new_with_carry // ); *normal = new_normal; *with_carry = new_with_carry; Some(new_normal) } ) .sum::<u64>() ); } |
English