Skip to content

Commit e5382a8

Browse files
committed
Fix slowness
1 parent be7f354 commit e5382a8

3 files changed

Lines changed: 48 additions & 44 deletions

File tree

src/models.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ impl Point {
1717
Point::new(self.x + p.x, self.y + p.y)
1818
}
1919

20-
pub fn move_with(&self, command: Command) -> Point {
20+
pub fn move_with(&self, kind: &Move) -> Point {
2121
let (x, y) = (self.x, self.y);
22-
match command {
23-
Command::MoveUp => Point::new(x, y + 1),
24-
Command::MoveDown => Point::new(x, y - 1),
25-
Command::MoveRight => Point::new(x + 1, y),
26-
Command::MoveLeft => Point::new(x - 1, y),
22+
match kind {
23+
Move::MoveUp => Point::new(x, y + 1),
24+
Move::MoveDown => Point::new(x, y - 1),
25+
Move::MoveRight => Point::new(x + 1, y),
26+
Move::MoveLeft => Point::new(x - 1, y),
2727
_ => *self,
2828
}
2929
}
3030

31-
pub fn revert_with(&self, command: Command) -> Point {
31+
pub fn revert_with(&self, kind: &Move) -> Point {
3232
let (x, y) = (self.x, self.y);
33-
match command {
34-
Command::MoveUp => Point::new(x, y - 1),
35-
Command::MoveDown => Point::new(x, y + 1),
36-
Command::MoveRight => Point::new(x - 1, y),
37-
Command::MoveLeft => Point::new(x + 1, y),
33+
match kind {
34+
Move::MoveUp => Point::new(x, y - 1),
35+
Move::MoveDown => Point::new(x, y + 1),
36+
Move::MoveRight => Point::new(x - 1, y),
37+
Move::MoveLeft => Point::new(x + 1, y),
3838
_ => unreachable!(),
3939
}
4040
}
@@ -99,7 +99,7 @@ impl Map {
9999
}
100100
}
101101

102-
#[derive(Debug, Clone, Copy)]
102+
#[derive(Debug, Clone)]
103103
pub enum BoosterType {
104104
NewHand,
105105
FastMove,
@@ -109,7 +109,7 @@ pub enum BoosterType {
109109
Unknown,
110110
}
111111

112-
#[derive(Debug, Clone, Copy)]
112+
#[derive(Debug, Clone)]
113113
pub struct Booster {
114114
pub kind: BoosterType,
115115
pub point: Point,
@@ -129,13 +129,18 @@ pub struct Task {
129129
pub boosters: Vec<Booster>,
130130
}
131131

132-
#[derive(Debug, Clone, Copy)]
133-
pub enum Command {
132+
#[derive(Debug, Clone)]
133+
pub enum Move {
134134
MoveUp,
135135
MoveDown,
136136
MoveLeft,
137137
MoveRight,
138-
Noop,
138+
Noop
139+
}
140+
141+
#[derive(Debug, Clone)]
142+
pub enum Command {
143+
Move(Move),
139144
TurnRight,
140145
TurnLeft,
141146
NewHand(Point),
@@ -144,11 +149,11 @@ pub enum Command {
144149
impl fmt::Display for Command {
145150
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
146151
match self {
147-
Command::MoveUp => write!(f, "W"),
148-
Command::MoveDown => write!(f, "S"),
149-
Command::MoveLeft => write!(f, "A"),
150-
Command::MoveRight => write!(f, "D"),
151-
Command::Noop => write!(f, "Z"),
152+
Command::Move(Move::MoveUp) => write!(f, "W"),
153+
Command::Move(Move::MoveDown) => write!(f, "S"),
154+
Command::Move(Move::MoveLeft) => write!(f, "A"),
155+
Command::Move(Move::MoveRight) => write!(f, "D"),
156+
Command::Move(Move::Noop) => write!(f, "Z"),
152157
Command::TurnRight => write!(f, "E"),
153158
Command::TurnLeft => write!(f, "Q"),
154159
Command::NewHand(p) => write!(f, "B({}, {})", p.x, p.y),

src/solve.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub fn solve_small(task: Task) -> Vec<Command> {
2626
remaining += 1;
2727
}
2828

29-
for &b in &task.boosters {
30-
booster_map.set(b.point, Some(b.kind));
29+
for b in &task.boosters {
30+
booster_map.set(b.point, Some(b.kind.clone()));
3131
}
3232

3333
for o in &task.obstacles {
@@ -41,10 +41,10 @@ pub fn solve_small(task: Task) -> Vec<Command> {
4141
}
4242

4343
let mut moves = [
44-
Command::MoveUp,
45-
Command::MoveDown,
46-
Command::MoveLeft,
47-
Command::MoveRight,
44+
Move::MoveUp,
45+
Move::MoveDown,
46+
Move::MoveLeft,
47+
Move::MoveRight,
4848
];
4949
let mut res = Vec::new();
5050
let mut cp = task.initial;
@@ -71,14 +71,14 @@ pub fn solve_small(task: Task) -> Vec<Command> {
7171
while hand_count > 0 && !new_bodies.is_empty() {
7272
let new_hand = new_bodies.pop_front().unwrap();
7373
hand_count -= 1;
74-
//bodies_diff.push(new_hand);
74+
bodies_diff.push(new_hand);
7575
res.push(Command::NewHand(new_hand));
7676
}
7777

78-
let mut data = Matrix::new(width, height, None);
78+
let mut data: Matrix<Option<Move>> = Matrix::new(width, height, None);
7979
let mut queue = VecDeque::new();
8080
queue.push_back(cp);
81-
data.set(cp, Some(Command::Noop));
81+
data.set(cp, Some(Move::Noop));
8282
let mut reached = false;
8383
while let Some(c) = queue.pop_front() {
8484
let bodies = bodies_diff
@@ -102,15 +102,16 @@ pub fn solve_small(task: Task) -> Vec<Command> {
102102
let mut local_cmds = Vec::new();
103103
let mut iter = c;
104104
while iter != cp {
105-
/*
106105
if let Some(Some(BoosterType::NewHand)) = booster_map.get(iter) {
107106
booster_map.set(iter, None);
108107
hand_count += 1;
109108
}
110-
*/
111-
let cmd = data.get(iter).unwrap().unwrap();
112-
local_cmds.push(cmd);
113-
iter = iter.revert_with(cmd);
109+
if let Some(Some(mv)) = data.get(iter) {
110+
iter = iter.revert_with(mv);
111+
local_cmds.push(Command::Move(mv.clone()));
112+
} else {
113+
panic!("cannot revert command");
114+
}
114115
}
115116
local_cmds.reverse();
116117
res.extend(local_cmds);
@@ -121,10 +122,10 @@ pub fn solve_small(task: Task) -> Vec<Command> {
121122
}
122123
moves.shuffle(&mut rng);
123124
for m in &moves {
124-
let nc = c.move_with(*m);
125+
let nc = c.move_with(m);
125126
if let Some(None) = data.get(nc) {
126127
if let Some(true) = valid.get(nc) {
127-
data.set(nc, Some(*m));
128+
data.set(nc, Some(m.clone()));
128129
queue.push_back(nc);
129130
}
130131
}

src/utils.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub struct Matrix<T> {
66
inner: Vec<T>,
77
}
88

9-
impl<T: Copy> Matrix<T> {
9+
impl<T: Clone> Matrix<T> {
1010
pub fn new(width: usize, height: usize, init: T) -> Matrix<T> {
1111
let n = width * height;
1212
Matrix {
@@ -16,9 +16,9 @@ impl<T: Copy> Matrix<T> {
1616
}
1717
}
1818

19-
pub fn get(&self, p: Point) -> Option<T> {
19+
pub fn get(&self, p: Point) -> Option<&T> {
2020
if p.x >= 0 && p.y >= 0 && (p.x as usize) < self.width && (p.y as usize) < self.height {
21-
Some(self.inner[p.y as usize * self.width + p.x as usize])
21+
Some(&self.inner[p.y as usize * self.width + p.x as usize])
2222
} else {
2323
None
2424
}
@@ -34,9 +34,7 @@ impl<T: Copy> Matrix<T> {
3434

3535
pub fn try_set(&mut self, p: Point, value: T) -> Option<T> {
3636
if let Some(r) = self.get_mut(p) {
37-
let old = *r;
38-
*r = value;
39-
Some(old)
37+
Some(std::mem::replace(r, value))
4038
} else {
4139
None
4240
}

0 commit comments

Comments
 (0)