Learning rust by solving an Algolab task I failed to code in c++ thirteen times, with the master solution open. What could go wrong?
Below notes are mostly for me, but they miight be helpful for you too. If for some reason you want to see the idea of the Attack of the Clone task coded in rust instead of c++.
A circle,
The jedi can't fight at the same time if their intervals overlap. They are too stupid to just protect a bit less than they could.
Goal: calculate the largest number of jedi that can fight without overlaps.
for each of the
- numbers start at one, not zero
-
$n$ and$m$ are never zero - It's about the number of jedi, not the number of covered segments
- some jedi intervals might go around the "end" section
$m$ - jedi
$a,b$ overlaps with jedi$b,c$
Only relevant for me if I figure out how to run rust on code expert that is intended for c++, but
- small
$n$ ,$m$ , and there is a segment that no jedi protects - there is a segment that no jedi protects
- there is a segment which at most 10 jedi protect
Having a segment that no jedi protects is useful because it provides a clear starting point where no cyclic overlaps can happen. We could in those cases just take the first ending jedi (or any one of them if there are multiple), then repeat.
This is where we want to start. How do we find it?
We read all the intervals in, and keep one counter per each of the
Con: uses much memory if
Walk around the circle once, only considering start and end positions.
At the initial position, our count is an unknown value
We would like to know where the lowest number of jedi is at. For this, we don't need to know the initial
of the
If we pick none, we shall pick the first other jedi that has its END soonest. There could be multiple of those, but that does not matter. They all do not start before the low-protected segment, they all end at the same time, there is none that ends earlier, so they don't differ in the number of other jedi they occlude.
So we just do the whole program with different starting jedi
In the end we just return the best (highest) of these 11 attempts.
Assume for a moment that the circle is not actually happening. Then we always pick according to Earliest-Deadline-First. Optimality proof by contradiction: If there was a different pick that were actually better, it would occlude less other jedi. Since we know a fixed position at which neither our pick
So we conclude that we choose by EDF.
Of course we have to ignore jedi that overlap with already picked ones. For this I keep track of all jedi Start
s that happened since the last End
of a jedi I picked. That could be done with a constant-access array of size End
and hence invalidate all the jedi that started before that (and are thus already in the set but should no longer be).
when do we stop?
We can't pick any jedi
Since we are tracking the start positions anyway for Option 2 of finding the low-protected segment, we can simply use the already-sorted list of all start and end positions. When we encounter
one list with all starts and ends for choosing the low-protected segment, a different one with only all ends for choosing the jedi after that?
No. Because we need the memory for the
Complete example to read a line from a file and parse it to an integer: https://stackoverflow.com/a/29677167/2550406 https://doc.rust-lang.org/std/primitive.str.html#method.parse
Reading all lines from the file as an iterator: https://doc.rust-lang.org/rust-by-example/std_misc/file/read_lines.html
Splitting the line into strings of numbers: https://stackoverflow.com/a/26643821/2550406
https://blog.burntsushi.net/rust-error-handling/
I wrote a macro and some functions, so that if I have files test1.in
and test1.out
I just have to write in my tests module:
#[cfg(test)]
mod tests{
use super::*;
tmf!(test1); // test my file "test1.in"
tmf!(my_custom_test); // test my file "my_custom_test.in"
}
==TODO==
I guess it could work by compiling to assembly and using it inline in c++...?
If I ever want to find this link again that discusses interoperating between c++ and rust by going through C, I will find it here.