Created
November 12, 2013 00:54
-
-
Save kennym/7423461 to your computer and use it in GitHub Desktop.
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
| INPUT = <<-EOS | |
| O O O O X O O O O O X | |
| X X O X O O O O X O X | |
| O O O X X O O O O O X | |
| O O O O O O O O O O X | |
| O O O O O X O O O O X | |
| EOS | |
| #INPUT = "BMC_TEST_INPUT_MAGIC" | |
| MINE = "X" | |
| MINEFREE = "O" | |
| NEW_LINE = "\n" | |
| def check_surrounding_fields(column_idx, row_idx) | |
| count = 0 | |
| count += 1 if $matrix[row_idx-1][column_idx-1] == MINE | |
| count += 1 if $matrix[row_idx-1][column_idx] == MINE | |
| count += 1 if $matrix[row_idx-1][column_idx+1] == MINE | |
| count += 1 if $matrix[row_idx][column_idx-1] == MINE | |
| count += 1 if $matrix[row_idx][column_idx+1] == MINE | |
| unless row_idx + 1 >= $matrix.length | |
| count += 1 if $matrix[row_idx+1][column_idx-1] == MINE | |
| count += 1 if $matrix[row_idx+1][column_idx] == MINE | |
| count += 1 if $matrix[row_idx+1][column_idx+1] == MINE | |
| end | |
| return count.to_s | |
| end | |
| def calculate_mines_for_field(column, column_idx, row_idx) | |
| return column if column == MINE | |
| mine_count = check_surrounding_fields(column_idx, row_idx) | |
| return mine_count | |
| end | |
| if __FILE__ == $0 | |
| $matrix = [] | |
| row = [] | |
| INPUT.split("").each do |field| | |
| if field == MINE || field == MINEFREE | |
| row << field | |
| elsif field == NEW_LINE | |
| $matrix << row | |
| row = [] | |
| end | |
| end | |
| $matrix.each_with_index do |row, row_idx| | |
| row.each_with_index do |column, column_idx| | |
| $matrix[row_idx][column_idx] = calculate_mines_for_field(column, column_idx, row_idx) | |
| end | |
| end | |
| printable = "" | |
| $matrix.each_with_index do |row, row_idx| | |
| row.each_with_index do |column, column_idx| | |
| if column_idx + 1 == row.length | |
| printable << "#{column}" | |
| printable << "\n" unless column_idx + 1 == row.length and row_idx + 1 == $matrix.length | |
| else | |
| printable << column + " " | |
| end | |
| end | |
| end | |
| puts printable | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment