-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.rs
53 lines (45 loc) · 1.23 KB
/
part2.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
use std::fs::read_to_string;
fn read_board() -> Vec<Vec<u8>> {
read_to_string("input.txt")
.unwrap()
.lines()
.map(|s| s.bytes().collect())
.collect()
}
fn try_match_at_offset(board: &Vec<Vec<u8>>, pos: &Point, dir: &Point) -> bool {
let to_match = "MAS";
for (i, ch) in to_match.chars().enumerate() {
let n = to_match.len() as i32 / 2 - i as i32;
let x = pos.x + dir.x * n;
let y = pos.y + dir.y * n;
if x < 0 || y < 0 || x >= (board[0].len() as i32) || y >= (board.len() as i32) ||
board[y as usize][x as usize] as char != ch {
return false;
}
}
return true;
}
struct Point {
x: i32, y: i32
}
fn is_x_mas(board: &Vec<Vec<u8>>, pos: &Point) -> bool {
if board[pos.y as usize][pos.x as usize] != b'A' {
return false;
}
return (try_match_at_offset(board, pos, &Point{x: -1, y: -1}) ||
try_match_at_offset(board, pos, &Point{x: 1, y: 1})) &&
(try_match_at_offset(board, pos, &Point{x: 1, y: -1}) ||
try_match_at_offset(board, pos, &Point{x: -1, y: 1}))
}
fn main() {
let board = read_board();
let mut sum = 0;
for y in 0 .. board.len() {
for x in 0 .. board[y].len() {
if is_x_mas(&board, &Point{x: x as i32, y: y as i32}) {
sum += 1;
}
}
}
println!("{}", sum);
}