Skip to main content
Commonmark migration
Source Link

#Rust - Paranoid#

Rust - Paranoid

#Rust - Paranoid#

Rust - Paranoid

Source Link
Herjan
  • 1.2k
  • 8
  • 11

#Rust - Paranoid#

Keeps comparing himself to the liveliest on the battlefield.

struct Bot {
    id: i32,
    life: i32,
    power: i32,
    action: String,
}

fn main() {
    let args: Vec<_> = std::env::args().collect();
    if args.len() < 2 {
        print!("ok");
    } else {
        let id: i32 = args[1].to_string().trim().parse()
            .ok()
            .expect("Please type a number!");

        let mut target_bot = Bot { id:-1, life:-1, power:-1, action:"D".to_string() };
        let mut own_bot = Bot { id:id, life:0, power:1, action:"D".to_string() };

        for arg in args {
            let split: Vec<&str> = arg.split(",").collect();
            if split.len() == 4 {
                let bot_id: i32 = split[0].to_string().trim().parse()
                    .ok()
                    .expect("Please type a number!");
                let bot_life: i32 = split[1].to_string().trim().parse()
                    .ok()
                    .expect("Please type a number!");
                let bot_power: i32 = split[2].to_string().trim().parse()
                    .ok()
                    .expect("Please type a number!");
                let bot_action: String = split[3].to_string();

                let bot = Bot { id:bot_id, life:bot_life, power:bot_power, action:bot_action };

                if bot.id != id && bot.life > target_bot.life {
                    target_bot = bot;
                } else if bot.id == id {
                    own_bot = bot;
                }
            }
        }

        /* If I am not stronger than the strongest, defend */
        let turns_to_kill = target_bot.life/own_bot.power + 1;
        let turns_to_be_killed = own_bot.life/target_bot.power;

        if target_bot.id > -1 && turns_to_kill < turns_to_be_killed {
            print!("{}", target_bot.id);
        } else {
            print!("D");
        }
    }
}