-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
132 lines (115 loc) · 2.99 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
use rusttype::Point as RPoint;
use std::{
cmp::*,
collections::HashMap,
io::{self, BufRead},
};
type Point = RPoint<usize>;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum SeaCucumber {
EAST,
SOUTH,
}
#[derive(PartialEq, Eq, Debug)]
struct Map {
map: HashMap<Point, SeaCucumber>,
width: usize,
height: usize,
}
impl Map {
pub fn new() -> Self {
Self {
map: HashMap::new(),
width: 0,
height: 0,
}
}
pub fn east_moved(&self) -> Self {
let mut new_map = Self::new();
new_map.width = self.width;
new_map.height = self.height;
for (point, item) in &self.map {
if *item != SeaCucumber::EAST {
new_map.map.insert(*point, *item);
continue;
};
let dest = Point {
x: (point.x + 1) % self.width,
y: point.y,
};
if self.map.get(&dest).is_some() {
new_map.map.insert(*point, *item);
} else {
new_map.map.insert(dest, *item);
}
}
new_map
}
pub fn south_moved(&self) -> Self {
let mut new_map = Self::new();
new_map.width = self.width;
new_map.height = self.height;
for (point, item) in &self.map {
if *item != SeaCucumber::SOUTH {
new_map.map.insert(*point, *item);
continue;
};
let dest = Point {
x: point.x,
y: (point.y + 1) % self.height,
};
if self.map.get(&dest).is_some() {
new_map.map.insert(*point, *item);
} else {
new_map.map.insert(dest, *item);
}
}
new_map
}
pub fn print(&self) {
for y in 0..self.height {
for x in 0..self.width {
if let Some(item) = self.map.get(&Point { x, y }) {
print!("{}", if *item == SeaCucumber::EAST { ">" } else { "v" });
} else {
print!(".");
}
}
println!();
}
}
}
fn main() {
let stdin = io::stdin();
let lines = stdin.lock().lines();
let mut map = Map::new();
for (y, _line) in lines.enumerate() {
map.height = y + 1;
let line = _line.unwrap();
for (x, c) in line.chars().enumerate() {
map.width = x + 1;
if c == '.' {
continue;
}
map.map.insert(
Point { x, y },
if c == '>' {
SeaCucumber::EAST
} else {
SeaCucumber::SOUTH
},
);
}
}
let mut step = 0;
loop {
step += 1;
let next = map.east_moved().south_moved();
if map == next {
break;
}
map = next;
map.print();
}
println!("ans {}", step);
}