Skip to content

Instantly share code, notes, and snippets.

@xtoss
Created April 13, 2014 02:35
Show Gist options
  • Save xtoss/10566577 to your computer and use it in GitHub Desktop.
Save xtoss/10566577 to your computer and use it in GitHub Desktop.
Python solution for Problem D. Deceitful War - Google Code Jam 2014
# Google Code Jam 2014
# Problem D. Deceitful War
# https://code.google.com/codejam/contest/2974486/dashboard#s=p3
# Author: jamie.xu - https://github.com/eytoss
import copy
PROBLEM_ID = "D" # A B or C
PROBLEM_SIZE = "large"
def run():
"""I/O handler"""
file_name = "{}-{}".format(PROBLEM_ID, PROBLEM_SIZE)
in_f = open('{}.txt'.format(file_name), 'r')
out_f = open('{}.out'.format(file_name), 'w')
num_of_case = int(in_f.readline().rstrip('\n'))
print "num of cases:{}".format(num_of_case)
for i in range(1, num_of_case+1):
solve_case(in_f, out_f, i)
def solve_case(in_f, out_f, case_index):
"""problem solver"""
print "case #{}".format(case_index)
in_f.readline()
A = [float(x) for x in in_f.readline().rstrip('\n').split(" ")]
B = [float(x) for x in in_f.readline().rstrip('\n').split(" ")]
total_tile = len(A)
A.sort()
A.reverse()
B.sort()
B.reverse()
A1 = copy.copy(A)
B1 = copy.copy(B)
# start war
WP = game(A, B)
# start deceitful war
DWP = total_tile - game(B1, A1)
out_f.write("Case #{}: {} {}\n".format(case_index, DWP, WP))
def game(A, B):
"""return game result of A in favor of B"""
# strategy for war, used by B(Dan):
# After A states a weigh:
# if has bigger one, use lowest-bigger one.
# else, discard lowest one.
# strategy for deceitful war, used by A(Naomi):
# find the smallest-bigger one to B's current smallest one,
# but state the weight to be bigger than B's biggest one,
# this way B would discard the lowest one, based on the war strategy.
# NOTE: actually, no need to be thus aggressive as above, any bigger is fine
AW = 0
BW = 0
while len(A) > 0:
if B[0] > A[0]: # win one!
BW += 1
B.pop(0)
A.pop(0)
else: # there is no equal, throw smallest one
AW += 1
A.pop(0)
B.pop(-1)
return AW
run()
@xtoss
Copy link
Author

xtoss commented Apr 13, 2014

Google stated that this would be the hardest one in this round, I am glad I didn't get scared by that and solved this one before trying to solve Minesweeper Master, which I personally think is much harder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment