-
-
Save rubik/11086f09886ddd1d652c to your computer and use it in GitHub Desktop.
Google Code Jam 2014 - Round 1B
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from utils import * | |
def read_case(f): | |
n = read_int(f) | |
return [read_line(f) for _ in range(n)] | |
def write_case(f, i, res): | |
f.write('Case #%d: ' % i) | |
f.write('%s' % res) | |
f.write('\n') | |
def solve_small(words): | |
l, n = zip(*[zip(*[(k, len(list(v))) for k, v in itertools.groupby(word)]) | |
for word in words]) | |
if len(set(l)) > 1: | |
return 'Fegla Won' | |
moves = 0 | |
for rep in zip(*n): | |
moves += min(sum(abs(target - r) for r in rep) | |
for target in range(min(rep), max(rep) + 1)) | |
return moves | |
def solve_large(*a, **kw): | |
return solve_small(*a, **kw) | |
if __name__ == '__main__': | |
solve((solve_small, solve_large), read_case, write_case, sys.argv[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
import itertools | |
import functools | |
import collections | |
def read_line(f): | |
return next(f).strip() | |
def read_int(f, b=10): | |
return int(read_line(f), b) | |
def read_letters(f): | |
return list(read_line(f)) | |
def read_digits(f, b=10): | |
return [int(x, b) for x in read_letters(f)] | |
def read_words(f, d=' '): | |
return read_line(f).split(d) | |
def read_ints(f, b=10, d=' '): | |
return [int(x, b) for x in read_words(f, d)] | |
def read_floats(f, d=' '): | |
return [float(x) for x in read_words(f, d)] | |
def read_arr(f, R, reader=read_ints, *args, **kwargs): | |
return [reader(f, *args, **kwargs) for i in range(R)] | |
def solve(solvers, rc, wc, fn, out_fn=None, debug=False): | |
if not fn.endswith('.in'): | |
in_fn = fn + '.in' | |
else: | |
in_fn = fn | |
fn = fn[:-3] | |
solver = solvers[1] if 'large' in in_fn else solvers[0] | |
if out_fn is None: | |
out_fn = fn + '.out' | |
with open(in_fn, 'r') as fi: | |
with open(out_fn, 'w') as fo: | |
T = read_int(fi) | |
for i in range(T): | |
case = rc(fi) | |
res = solver(case) | |
if debug: | |
print(case, res) | |
wc(fo, i + 1, res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment