Skip to content

Instantly share code, notes, and snippets.

@okaq
Created May 13, 2011 00:40
Show Gist options
  • Save okaq/969747 to your computer and use it in GitHub Desktop.
Save okaq/969747 to your computer and use it in GitHub Desktop.
Solution: Candy Splitting (Google Code Jam 2011 Qualification Round Problem C)
import sys
fin = file(sys.argv[1])
fout = open(sys.argv[2], 'w')
lines = fin.readlines()
tests = int(lines[0])
candys0 = [map(int, line.split()) for line in lines]
def test0(a): return not (len(a) == 1)
candys = filter(test0, candys0)
data = []
"""
no need to separate lists into piles!
patricks summation is exclusive or (XOR).
bitwise solution is very fast indeed.
"""
def xor0(a,b): return a^b
for candy in candys:
if (reduce(xor0, candy) == 0):
candy.sort()
data.append(sum(candy[1:]))
continue
data.append('NO')
for i in range(len(data)):
fout.write("Case #%s: %s\n" % (str(i+1), str(data[i])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment