use std::io;

macro_rules! parse_input {
    ($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/
fn main() {
    let mut input_line = String::new();
    io::stdin().read_line(&mut input_line).unwrap();
    let inputs = input_line.split(" ").collect::<Vec<_>>();
    let light_x = parse_input!(inputs[0], i32); // the X position of the light of power
    let light_y = parse_input!(inputs[1], i32); // the Y position of the light of power
    let initial_tx = parse_input!(inputs[2], i32); // Thor's starting X position
    let initial_ty = parse_input!(inputs[3], i32); // Thor's starting Y position



    /*
    dx = x2 − x1
    dy = y2 − y1
    for x from x1 to x2 do
        y = y1 + dy × (x − x1) / dx
        plot(x, y)
    */


    let mut path = vec![];

    let dx = light_x - initial_tx;
    let dy = light_y - initial_ty;


    let  (mut now_x,mut now_y) = (initial_tx, initial_ty);

    if dx == 0 {
        for y in 0..=(initial_ty - light_y).abs() {
            if dy > 0 {
                path.push("S")
            } else if dy < 0 {
                path.push("N")
            }
        }
    } else {
        for x in initial_tx..=light_x {
            let y = initial_tx + dy * (x - initial_tx) / dx;
            
            let x_dir = x - now_x;
            let y_dir = y - now_y;

            let direction = if x_dir > 0 {
                if y_dir > 0 {
                    "NE"
                } else if y_dir < 0 {
                    "SE"
                } else {
                    "E"
                }
            } else if x_dir < 0 {
                if y_dir > 0 {
                    "NW"
                } else if y_dir < 0 {
                    "SW"
                } else {
                    "W"
                }
            } else {
                ""
            };

            if !direction.is_empty() {
                path.push(direction);
            }
            now_x = x;
            now_y = y;
        }
    }

    // game loop
    loop {
        let mut input_line = String::new();
        io::stdin().read_line(&mut input_line).unwrap();
        let remaining_turns = parse_input!(input_line, i32); // The remaining amount of turns Thor can move. Do not remove this line.

        // Write an action using println!("message...");
        // To debug: eprintln!("Debug message...");


        // A single line providing the move to be made: N NE E SE S SW W or NW

        //println!("{}", [x_str, y_str].join(" "));

        if let Some(dir) = path.pop() {
            println!("{}", dir);
        } else {
            println!("");
        } 
    }
}

Изменить пасту