summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 92b0a1b95824e3325f60d68ddbe7601c6ab9c238 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::io::prelude::*;
use std::io::stdin;

mod command;
mod consts;
mod json;
mod state;

use command::*;
use state::*;

fn main() {
    for line in stdin().lock().lines() {
        let round_number = line.expect("Failed to read line from stdin: {}");
        let command =
            match json::read_state_from_json_file(&format!("./rounds/{}/state.json", round_number))
            {
                Ok(state) => choose_command(&state),
                Err(e) => {
                    eprintln!("WARN: State file could not be parsed: {}", e);
                    Command::Nothing
                }
            };
        println!("C;{};{}", round_number, command);
    }
}

fn choose_command(state: &GameState) -> Command {
    let player_moves = state.valid_moves(0);
    let naive_result = player_moves
        .into_iter()
        .map(|player_move| {
            let mut state = state.clone();
            state.update([player_move, Command::Nothing]);
            (player_move, state)
        })
        .max_by(|(_, state_1), (_, state_2)| {
            state_1.players[0]
                .position
                .x
                .cmp(&state_2.players[0].position.x)
                .then(state_1.players[0].speed.cmp(&state_2.players[0].speed))
        })
        .unwrap()
        .0;
    naive_result
}