Correct using a Parity Block For more information on Parity: Wikipedia

Challenge

Write a program that takes an input (stdin, argv, ect) of four nibbles and two parity nibbles. Your program should then test to see whether the 'block' is valid, using even parity; if it is then output 1. Then try to make any corrections, if correction is possible output the corrected block. If you cannot correct the block output -1. You can assume the parity nibbles are never corrupt.

To re-cap and clarify:


If the input is valid output 1.
If the input is invalid and fixable output the fixed block.
If the input is invalid and cannot be fixed output -1.
You can only edit bits if their respective row and column in the parity bit is incorrect. 


Input Format:

&lt;NIBBLE&gt; &lt;NIBBLE&gt; &lt;NIBBLE&gt; &lt;NIBBLE&gt; &lt;PARITY-Y&gt; &lt;PARITY-X&gt;


So 1010 0111 1101 1011 1011 0111 Would look like:



Winning

This is code-golf: Smallest program wins.

Input and Output Examples

Input 1010 0111 1101 1011 1011 0111
Output 1
The Input is Valid

Input 1010 0011 1101 1011 1011 0111
Output 1010 0111 1101 1011
The Input was Invalid, but correctable

Input 1010 0001 1101 1011 1011 0111
Output -1
The Input was Invalid, and not correctable
Python 2, 387

I feel it's kinda long though.
Do suggest improvements or any fixes... 



i=raw_input()
z="""#h(a?(a&amp;1)^h(a/2)~a@0
#y(i,m,l,c=0?h(i&amp;m)|y(i&gt;&gt;l,m,l,c+1)*2~c&lt;4@0
#p(i?y(i,4369,1)*16+y(i,15,4)
k=i.replace(" ","")
m=int(k[:16],2)
c=int(k[-8:],2)
#s(i?s(i&gt;&gt;4)+" "+bin(i&amp;15^16)[3:]~i@"" 
#q(j=16):d=m^1&lt;&lt;j;!s(d)[1:]~p(d)==c@q(j-1)~j@-1
#f(?1~p(m)==c@q()"""
for k,l in("?","):!"),("!","return "),("@"," else "),("#","def "),("~","if "):z=z.replace(k,l)
exec z
print f()


Slightly ungolfed...

def y(i,m,l,c=0):
    h=lambda a:(a&amp;1)^h(a/2)if a else 0 # Calculate how many set bits in i
    return h(i&amp;m)|y(i&gt;&gt;l,m,l,c+1)*2if c&lt;4 else 0 # Sets the parity bits accordingly recursively

p=lambda i:y(i,4369,1)*16+y(i,15,4) # Returns the y and x parity together

i="1010 0111 1101 1011 1011 0111"

k=i.replace(" ","") # Remove all spaces
m=int(k[:16],2)     # Extracts the matrix
c=int(k[-8:],2)     # Extracts the parities

s=lambda i:s(i&gt;&gt;4)+" "+bin(i&amp;15^16)[3:]if i else"" 
# Converts from int to space separated hexas in binary

def f(i):
    if p(m)==c:return 1 # If calculated parities matches the test
    for i in range(16): # Else, for each of the 16 bits in the matrix
        d=m^1&lt;&lt;i        # xor it
        if p(d)==c:return s(d)[1:] # return the matrix if its parities matches the test
    return -1 # If all else fails, return -1


print f(i)