Skip to content

Commit b06ca03

Browse files
committed
Use hand:
1 parent f152a42 commit b06ca03

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

src/bin/score.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ fn score_small(task: Task, output_len: usize) -> ScoreInfo {
4646
let mut passed = Matrix::new(width as usize, height as usize, true);
4747
let mut valid = Matrix::new(width as usize, height as usize, false);
4848

49-
for p in &map_points {
49+
for &p in &map_points {
5050
passed.set(p, false);
5151
valid.set(p, true);
5252
remaining += 1;
5353
}
5454

5555
for o in &task.obstacles {
56-
for p in o.enumerate_points().iter() {
56+
for &p in o.enumerate_points().iter() {
5757
if let Some(true) = valid.get(p) {
5858
valid.set(p, false);
5959
passed.set(p, true);

src/models.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ impl Point {
1313
Point { x, y }
1414
}
1515

16+
pub fn add(&self, p: &Point) -> Point {
17+
Point::new(self.x + p.x, self.y + p.y)
18+
}
19+
1620
pub fn move_with(&self, command: Command) -> Point {
1721
let (x, y) = (self.x, self.y);
1822
match command {

src/solve.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ pub fn solve_small(task: Task) -> Vec<Command> {
1919
let mut passed = Matrix::new(width, height, true);
2020
let mut valid = Matrix::new(width, height, false);
2121

22-
for p in &map_points {
22+
for &p in &map_points {
2323
passed.set(p, false);
2424
valid.set(p, true);
2525
remaining += 1;
2626
}
2727

2828
for o in &task.obstacles {
29-
for p in o.enumerate_points().iter() {
29+
for &p in o.enumerate_points().iter() {
3030
if let Some(true) = valid.get(p) {
3131
valid.set(p, false);
3232
passed.set(p, true);
@@ -43,25 +43,45 @@ pub fn solve_small(task: Task) -> Vec<Command> {
4343
];
4444
let mut res = Vec::new();
4545
let mut cp = task.initial;
46-
if !passed.get(&cp).unwrap() {
47-
passed.set(&cp, true);
46+
if !passed.get(cp).unwrap() {
47+
passed.set(cp, true);
4848
remaining -= 1;
4949
}
50+
let bodies_diff = vec![
51+
Point::new(0, 0),
52+
Point::new(1, 1),
53+
Point::new(1, 0),
54+
Point::new(1, -1),
55+
];
5056
while remaining > 0 {
5157
let mut data = Matrix::new(width, height, None);
5258
let mut queue = VecDeque::new();
5359
queue.push_back(cp);
54-
data.set(&cp, Some(Command::Noop));
60+
data.set(cp, Some(Command::Noop));
5561
let mut reached = false;
5662
while let Some(c) = queue.pop_front() {
57-
if !passed.get(&c).unwrap() {
58-
passed.set(&c, true);
59-
remaining -= 1;
63+
let bodies = bodies_diff
64+
.iter()
65+
.map(|diff| c.add(diff))
66+
.collect::<Vec<_>>();
67+
let not_passed = bodies.iter().any(|p| {
68+
if let Some(false) = passed.get(*p) {
69+
true
70+
} else {
71+
false
72+
}
73+
});
74+
if not_passed {
75+
for body in bodies {
76+
if let Some(false) = passed.try_set(body, true) {
77+
remaining -= 1;
78+
}
79+
}
6080

6181
let mut local_cmds = Vec::new();
6282
let mut iter = c;
6383
while iter != cp {
64-
let cmd = data.get(&iter).unwrap().unwrap();
84+
let cmd = data.get(iter).unwrap().unwrap();
6585
local_cmds.push(cmd);
6686
iter = iter.revert_with(cmd);
6787
}
@@ -75,9 +95,9 @@ pub fn solve_small(task: Task) -> Vec<Command> {
7595
moves.shuffle(&mut rng);
7696
for m in &moves {
7797
let nc = c.move_with(*m);
78-
if let Some(None) = data.get(&nc) {
79-
if let Some(true) = valid.get(&nc) {
80-
data.set(&nc, Some(*m));
98+
if let Some(None) = data.get(nc) {
99+
if let Some(true) = valid.get(nc) {
100+
data.set(nc, Some(*m));
81101
queue.push_back(nc);
82102
}
83103
}

src/utils.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,33 @@ 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 {
2121
Some(self.inner[p.y as usize * self.width + p.x as usize])
2222
} else {
2323
None
2424
}
2525
}
2626

27-
pub fn get_mut(&mut self, p: &Point) -> Option<&mut T> {
27+
pub fn get_mut(&mut self, p: Point) -> Option<&mut T> {
2828
if p.x >= 0 && p.y >= 0 && (p.x as usize) < self.width && (p.y as usize) < self.height {
2929
Some(&mut self.inner[p.y as usize * self.width + p.x as usize])
3030
} else {
3131
None
3232
}
3333
}
3434

35-
pub fn set(&mut self, p: &Point, value: T) {
35+
pub fn try_set(&mut self, p: Point, value: T) -> Option<T> {
36+
if let Some(r) = self.get_mut(p) {
37+
let old = *r;
38+
*r = value;
39+
Some(old)
40+
} else {
41+
None
42+
}
43+
}
44+
45+
pub fn set(&mut self, p: Point, value: T) {
3646
if let Some(r) = self.get_mut(p) {
3747
*r = value;
3848
} else {

0 commit comments

Comments
 (0)