H: Modelling Problems
https://open.kattis.com/problems/modelling
There are no more than 10 unknown variable assignments [x=?]
. Moreover, each ?
can either be 0 or 1. So, we can iterate over all 10-bit integers (0, ..., 1023) to consider all possible assignment combinations for ?
terms.
For each 10-bit integer i
:
Create a hash map that maps variable names (chars) to values (ints)
Parse statements from the input string. This can just be done by checking all cases and handling each case separately:
Assignment: map var name to a value (either a given value or some of two other vars)
Assignment to an unknown: maintain a count
c
of the unknowns seen so far. Then, treat the unknown as the value(i >> c) & 1
and incrementc
by 1. This sets the unknown to the c-th bit ofi
.Conditional: check if the condition is true based on entries in the map, and parse the assignments/assertions within the conditional if so
Assertions: check if the variables specified have the same value. If not, immediately finish the test case and output
ASSERTIONS INVALID
If all assertions pass for all i
, output ASSERTIONS ALWAYS HOLD
Last updated