-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathascii-art-the-drunken-bishop-algorithm.rs
81 lines (72 loc) · 2.12 KB
/
ascii-art-the-drunken-bishop-algorithm.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
use std::io;
macro_rules! parse_input {
($t:ident) => {{
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
input_line.trim_matches('\n').parse::<$t>().unwrap()
}}
}
#[derive(Debug, Clone, PartialEq)]
enum Direction {
NorthWest,
NorthEast,
SouthWest,
SouthEast,
}
impl Direction {
fn from(val: u8) -> Self {
match val {
0 => Direction::NorthWest,
1 => Direction::NorthEast,
2 => Direction::SouthWest,
_ => Direction::SouthEast,
}
}
fn offset(&self) -> (i8, i8) {
match self {
Direction::NorthWest => (-1, -1),
Direction::NorthEast => (-1, 1),
Direction::SouthWest => (1, -1),
Direction::SouthEast => (1, 1),
}
}
}
const ROWS: usize = 9;
const COLS: usize = 17;
const SYMBOLS: [char; 15] = [' ', '.', 'o', '+', '=', '*', 'B', 'O',
'X', '@', '%', '&', '#', '/', '^'];
fn pos_update(from: &mut (i8, i8), dir: &Direction) {
let (dx, dy) = dir.offset();
*from = (
(from.0 + dx).clamp(0, (ROWS - 1) as i8),
(from.1 + dy).clamp(0, (COLS - 1) as i8));
}
fn main() {
let mut grid = vec![vec![0; COLS]; ROWS];
let mut pos: (i8, i8) = (4, 8);
parse_input!(String).split(':').for_each(|byte_str| {
let byte = u8::from_str_radix(byte_str, 16).expect("Invalid hex");
for i in 0..4 {
let bits = (byte >> (i * 2)) & 0b11;
pos_update(&mut pos, &Direction::from(bits));
grid[pos.0 as usize][pos.1 as usize] += 1;
}
});
println!("+---[CODINGAME]---+");
for (i, row) in grid.iter().enumerate() {
print!("|");
for (j, &cell) in row.iter().enumerate() {
if (i, j) == (pos.0 as usize, pos.1 as usize) {
print!("E");
}
else if (i, j) == (4, 8) {
print!("S");
}
else {
print!("{}", SYMBOLS[cell % 15]);
}
}
println!("|");
}
println!("+-----------------+");
}