Why don't you look at the Nethack source code? I guess it wouldn't be "too hard" to make an algorithim to do it. Then again..

I've got a Sil level 3 application ro control, should I use a partitioned PLC to garuntee safety or use an independently wired relay solution?

BTW, we are making sodium azide.[/b]
I would use an independently wired relay solution, since you seem to be coming in from the buzzwords/lack of maintainability angle.