DEV Community

Discussion on: Daily Challenge #287 - 16+18=214

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Rust

(without stringification)

fn euthanize(mut a: u64, mut b: u64) -> u64 {
    let mut out = 0;
    let mut cursor = 1;
    loop {
        let sum = a % 10 + b % 10;
        out += sum * cursor;
        cursor *= if sum < 10 { 10 } else { 100 };
        a /= 10;
        b /= 10;
        if a == 0 && b == 0 {
            return out;
        }
    }
}

// test

fn main() {
    assert_eq!(euthanize(2, 11), 13);
    assert_eq!(euthanize(16, 18), 214);
    assert_eq!(euthanize(122, 81), 1103);
    dbg!(euthanize(1222, 30277));
    dbg!(euthanize(1236, 30889));
    dbg!(euthanize(49999, 49999));
}

(look at it go)