Created
February 27, 2013 19:54
-
-
Save bouk/5051139 to your computer and use it in GitHub Desktop.
NIO 2012-2013 ronde 2 opdracht 0
This file contains hidden or 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 re | |
import copy | |
import sys | |
board = None | |
moves_done = 0 | |
columns = 0 | |
rows = 0 | |
moves = [] | |
squares_open = 0 | |
VERTICAL = re.compile(r'^([a-z])(\d)\d$') | |
HORIZONTAL = re.compile(r'^([a-z])[a-z](\d)$') | |
def cti(c): | |
return ord(c) - ord('a') | |
def itc(i): | |
return chr(ord('a') + i) | |
def get_possible_moves(vert): | |
if vert: | |
for column in range(columns): | |
for row in range(rows - 1): | |
if board[row][column] and board[row + 1][column]: | |
yield itc(column) + str(row + 1) + str(row + 2) | |
else: | |
for column in range(columns - 1): | |
for row in range(rows): | |
if board[row][column] and board[row][column + 1]: | |
yield itc(column) + itc(column + 1) + str(row + 1) | |
def opgave0a(): | |
sys.stdout.write("Vera" if moves_done % 2 == 0 else "Herman") | |
sys.stdout.write("\n") | |
def opgave0b(): | |
sys.stdout.write(str(squares_open) + "\n") | |
def opgave0c(): | |
moves = list(get_possible_moves(moves_done % 2 == 0)) | |
sys.stdout.write(str(len(moves)) + "\n") | |
for move in moves: | |
sys.stdout.write(move + "\n") | |
def opgave0d(): | |
current = moves_done % 2 == 0 | |
while True: | |
try: | |
move = get_possible_moves(current).next() | |
sys.stdout.write(move + "\n") | |
domove(move) | |
current = not current | |
except: | |
sys.stdout.write("Herman" if current else "Vera") | |
sys.stdout.write("\n") | |
return | |
def domove(m): | |
global squares_open, moves_done | |
moves.append(m) | |
matches = VERTICAL.match(m) | |
squares_open -= 2 | |
moves_done += 1 | |
if matches: | |
column = cti(matches.group(1)) | |
row = int(matches.group(2)) - 1 | |
board[row][column] = board[row + 1][column] = False | |
else: | |
matches = HORIZONTAL.match(m) | |
column = cti(matches.group(1)) | |
row = int(matches.group(2)) - 1 | |
board[row][column] = board[row][column + 1] = False | |
starting_board = None | |
moves_done = 0 | |
squares_open = 0 | |
def restore(): | |
board = copy.deepcopy(starting_board) | |
moves_done = starting_moves_done | |
squares_open = starting_squares_open | |
if __name__ == '__main__': | |
size = sys.stdin.readline().strip() | |
columns = cti(size[0]) + 1 | |
rows = int(size[1:]) | |
moves_done = 0 | |
squares_open = columns * rows | |
board = [[True] * columns for _ in range(rows)] | |
m = int(sys.stdin.readline().strip()) | |
for _ in range(m): | |
domove(sys.stdin.readline().strip()) | |
starting_board = copy.deepcopy(board) | |
starting_moves_done = moves_done | |
starting_squares_open = squares_open | |
# for row in board[::-1]: | |
# for column in row: | |
# sys.stdout.write(str(int(column))) | |
# sys.stdout.write("\n") | |
opdrachten = ('a', 'b', 'c', 'd') | |
# opdrachten = ('a',) | |
for o in opdrachten: | |
globals()['opgave0' + o]() | |
if len(opdrachten) > 1: | |
restore() | |
sys.stdout.write("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment