Skip to content

rust: add solution for year 2017, day 18 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 13, 2020
Merged

rust: add solution for year 2017, day 18 #123

merged 5 commits into from
Feb 13, 2020

Conversation

saser
Copy link
Owner

@saser saser commented Feb 13, 2020

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.

@saser saser merged commit ad32c4a into master Feb 13, 2020
@saser saser deleted the rust/2017/18 branch February 13, 2020 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant