Created
November 4, 2013 10:40
-
-
Save Widdershin/7300861 to your computer and use it in GitHub Desktop.
Query Board
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
###### IO Boilerplate ###### | |
import sys | |
if len(sys.argv) < 2: | |
input_file_name = "23-queryboard-in.txt" | |
else: | |
input_file_name = sys.argv[1] | |
with open(input_file_name) as input_file: | |
input_lines = map(lambda x: x.strip(), filter(lambda x: x != '', input_file.readlines())) | |
###### /IO Boilerplate ###### | |
BOARD_SIZE = 256 | |
class Board(object): | |
def __init__(self, board_size): | |
self.size = board_size | |
self.data = [[0 for i in range(board_size)] for j in range(board_size)] | |
self.commands = {'SetCol': self.set_column, 'SetRow': self.set_row, 'QueryCol': self.query_column, 'QueryRow': self.query_row} | |
def print_board(self): | |
for row in self.data: | |
print " ".join(map(str, row)) | |
def set_column(self, column, new_value): | |
for row in self.data: | |
row[column] = new_value | |
def set_row(self, row, new_value): | |
self.data[row] = [new_value for i in range(self.size)] | |
def query_column(self, column): | |
print sum(zip(*self.data)[column]) | |
def query_row(self, row): | |
print sum(self.data[row]) | |
class Command(object): | |
def __init__(self, board, command_string): | |
super(Command, self).__init__() | |
self.board = board | |
self.command_string = command_string | |
self.process_command(self.command_string) | |
def process_command(self, command_string): | |
parts = command_string.split(' ') | |
self.func = self.board.commands[parts.pop(0)] | |
self.args = map(int, parts) | |
def __call__(self): | |
self.func(*self.args) | |
def main(): | |
board = Board(BOARD_SIZE) | |
for line in input_lines: | |
command = Command(board, line) | |
command() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment