rust: add solution for year 2017, day 18 #123
Merged
+151
−139
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was actually pretty fun. I thought that I was going to need to have multithreading when I first encountered part 2 (which was two years ago), but after studying things like distributed systems, it appears that my thinking has changed a bit.
The solution is based on the fact that we are able to choose our own "scheduling" of the programs. So I chose an alternating approach, where each process runs until it blocks (by not being able to receive anything, or by running to the end of the program) and produces some output. That output is then fed to the other program. As soon as one program blocks and has produced no output, the two programs are deadlocked or terminated.
For example, consider (without loss of generality) that program 0 just finished running and is now blocked or terminated, and it is time for program 1 to run. If the output from program 1 is empty, program 0 will never progress: if it is blocked waiting for input, it will never receive one, since program 1 has not produced any output; if it is terminated, it will never progress. Therefore, the two programs are now deadlocked and/or terminated.