The specific problem can be seen here
A simple summary description is: Given some integer inputs as seeds, and some mappings (list list (dst, src, length)), if a seed falls in the range [src, src + length], it is mapped to (dst + seed  src). The question is to find the minimum value among the seeds after several mappings.
Brute Force with F#
A very intuitive idea when seeing the problem is to scan all mappings for each input seed, get a final seed, and find the minimum value from it. Part1 is easily passed, but when it comes to part2, each pair of seeds represents all seeds in the range [seed1, seed1 + seed2], adding up to more than 19.8 billion seeds.
At first, I wrote a brute force simulation in F#, but it was interrupted after running for a while, possibly because there were too many seeds. So I manually split the input in half, ran it twice, each time took about 8 minutes, but it could get to the correct answer.
Since it is so timeconsuming, the first thing that comes to mind is to use multithreading for parallel computation. In F#, it can be easily implemented with async block + Async.Parallel, which seems to be able to run on seven or eight cores.


Using multithreading in F#, it can be finished in just over 5 minutes.
Since it's slow, switching to a faster language, like Rust, should improve a lot.
Brute Force with Rust
First, I wrote a brute force simulation in Rust with the same logic, basically moving the logic written in F# over, and it could be finished in about 4 minutes. This speed is even faster than when I used multithreading in F#! The performance of Rust is indeed welldeserved.
Then I wrote another version using multithreading, with every two seeds as a group, and a thread was opened to handle each seed. Then it could be finished in about one minute, indeed fast, Rust is just too strong.


The Real Solution
The previous brute force method was to do multiple mappings for each seed to get the result and then find the minimum value, but the quantity was too large, and the number of times needed to run was too many. But in fact, you don't need to do mapping for each seed, you can do mapping according to the range.
For a range of seeds, after one round of mapping,
 If it intersects with one of the maps, you can take this intersection out for mapping
 The two endpoints of the intersection part can be mapped to get a new range
 The nonintersection part is taken out to get a new range, and continue to deal with other maps
 If there is no intersection, then directly enter the next round of mapping
The specific code is as follows


After switching to this solution, it only takes 0.05s!
A small question: For this kind of code logic that needs three nested loops, if it is converted to use functional programming thinking, such as using F# to implement, what should we do? Please see the next episode for the specific solution.