-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
134 lines (119 loc) · 4.48 KB
/
main.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::io;
macro_rules! print_err {
($($arg:tt)*) => (
{
use std::io::Write;
writeln!(&mut ::std::io::stderr(), $($arg)*).ok();
}
)
}
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.
**/
fn main() {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let size = parse_input!(input_line, i32);
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let units_per_player = parse_input!(input_line, i32);
// game loop
loop {
let mut grid = vec![];
let mut my_units = vec![];
for i in 0..size as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let row = input_line.trim().to_string();
grid.push(row);
}
for i in 0..units_per_player as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let inputs = input_line.split(" ").collect::<Vec<_>>();
let unit_x = parse_input!(inputs[0], i32);
let unit_y = parse_input!(inputs[1], i32);
my_units.push((unit_x, unit_y));
}
for i in 0..units_per_player as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let inputs = input_line.split(" ").collect::<Vec<_>>();
let other_x = parse_input!(inputs[0], i32);
let other_y = parse_input!(inputs[1], i32);
}
let mut actions = vec![];
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let legal_actions = parse_input!(input_line, i32);
for i in 0..legal_actions as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let inputs = input_line.split(" ").collect::<Vec<_>>();
let atype = inputs[0].trim().to_string();
let index = parse_input!(inputs[1], i32);
let dir_1 = inputs[2].trim().to_string();
let dir_2 = inputs[3].trim().to_string();
let move_from = my_units[index as usize];
let move_to = get_pos(&move_from, &dir_1);
let to_height = get_height(&move_to, grid.as_slice());
let from_height = get_height(&move_from, grid.as_slice());
let build_pos = get_pos(&move_to, &dir_2);
let build_height = get_height(&build_pos, grid.as_slice());
actions.push((atype, index, dir_1, dir_2, from_height, to_height, build_height, move_from, move_to));
}
// Write an action using println!("message...");
// To debug: print_err!("Debug message...");
print_grid(&grid);
print_action(best_action(actions.as_slice()));
// println!("MOVE&BUILD 0 N S");
}
}
fn print_grid(rows: &[String]) {
for row in rows {
print_err!("{}", row);
}
}
fn print_action(action: &(String, i32, String, String, u32, u32, u32, (i32, i32), (i32, i32))) {
print_err!("printing action {:?}", action);
println!("{} {} {} {}", action.0, action.1, action.2, action.3);
}
fn get_pos(from: &(i32, i32), dir: &String) -> (i32, i32) {
let mut x = from.0;
let mut y = from.1;
if dir.contains("N") {
y -= 1;
} else if dir.contains("S") {
y += 1;
}
if dir.contains("W") {
x -= 1;
} else if dir.contains("E") {
x += 1;
}
return (x, y);
}
fn get_height(pos: &(i32, i32), grid: &[String]) -> u32 {
return grid[pos.1 as usize].chars().nth(pos.0 as usize).unwrap().to_digit(10).unwrap();
}
fn best_action(actions: &[(String, i32, String, String, u32, u32, u32, (i32, i32), (i32, i32))]) -> &(String, i32, String, String, u32, u32, u32, (i32, i32), (i32, i32)) {
let mut selected = &actions[0];
let mut max_height = selected.5;
let mut min_build = selected.6;
for action in actions {
if action.5 > max_height {
selected = action;
max_height = action.5;
min_build = action.6;
} else if action.5 == max_height && action.6 < min_build {
selected = action;
max_height = action.5;
min_build = action.6;
}
}
return selected;
}