diff --git a/data/data.go b/data/data.go index 140656a..8a31193 100644 --- a/data/data.go +++ b/data/data.go @@ -4,13 +4,20 @@ type Board [5][5]Holder // Returns a new 2D board func NewBoard() *Board { - return nil + return &Board{} } // FindNumber finds the given number in the board and returns // the row and column of the number alongwith true if value was found // else returns (-1, -1, false) func (b *Board) FindNumber(num int) (int, int, bool) { + for i_row, row := range b { + for i_col, v_col := range row { + if num == v_col.Val { + return i_row, i_col, true + } + } + } return -1, -1, false } @@ -24,12 +31,55 @@ func (b *Board) MarkValue(num int) { // CheckBingo checks if the board is bingo or not func (b *Board) CheckBingo() bool { - return false + var any_row, any_col bool= false, false + + for row := 0; row < 5; row++ { + // Checking if a row is bingo + var row_val int + for col := 0; col < 5; col++ { + if !b[row][col].Marked { + break + } else { + row_val++ + } + } + + if row_val == 5 { + any_row = true + } + } + + for col := 0; col < 5; col++ { + // Checking if a column is bingo + var col_val int + for row := 0; row < 5; row++ { + if !b[row][col].Marked { + break + } else { + col_val++ + } + } + + if col_val == 5 { + any_col = true + } + } + + return any_col || any_row } // AddUnmarked adds the unmarked values in the board func (b *Board) AddUnmarked() int { - return 0 + var sum int + for _, row := range b { + for _, v_col := range row { + if !v_col.Marked { + sum += v_col.Val + } + } + } + + return sum } // Holder stores information about a particular diff --git a/play/play.go b/play/play.go index d89f8ca..2c0147e 100644 --- a/play/play.go +++ b/play/play.go @@ -24,5 +24,44 @@ func FindWinner(order []int, boards []*data.Board) int { // returns the iteration value multiplied by addition of // unmarked values in the board. func FindLastBoard(order []int, boards []*data.Board) int { + // What I could do is iterate over the values and boards. Once a board goes bingo, pop it off the 'boards' slice, + // keep repeating it until slice's length is 1. + + var bingoed = make(map[*data.Board]bool) + var bingoed_count int + var board_init_count int = len(boards) + + for _, v := range boards { + bingoed[v] = false + } + + for _, val := range order { + for _, board := range boards { + board.MarkValue(val) + var bingo bool = board.CheckBingo() + + if bingo && !bingoed[board] { + bingoed[board] = true + bingoed_count++ + } + + if bingoed_count == board_init_count { + return val * board.AddUnmarked() + } + + // For some reason the plan didn't work + // if bingo { + // if (board_i == 0) { + // boards = boards[1:] + // } else if (board_i < len(boards) - 1) { + // boards = append(boards[ :board_i], boards[board_i+1: ]...) + // } else { + // boards = boards[:board_i] + // } + // continue + // } + + } + } return 0 } diff --git a/reader/io.go b/reader/io.go index 9414f4a..6cd635c 100644 --- a/reader/io.go +++ b/reader/io.go @@ -12,7 +12,8 @@ import ( // GetScanner returns a scanner for the given file. func GetScanner(f io.Reader) *bufio.Scanner { - return nil + new_scanner := bufio.NewScanner(f) + return new_scanner } // ReadOrder reads the order of values to mark in bingo game from the given scanner. @@ -44,5 +45,24 @@ func ReadOrder(scanner *bufio.Scanner) ([]int, error) { // ReadBoard reads a single board from the given scanner. func ReadBoard(scanner *bufio.Scanner) (*data.Board, error) { - return nil, nil + var board_data [5]string + + for i := 0; i < 5; i++ { + // Read the board as a string + if scanner.Scan() { + board_data[i] = scanner.Text() + } + } + + // Convert the board_data string into a new board of all the values + var new_board data.Board + + for row := 0; row < 5; row++ { + var line_data []string = strings.Fields(board_data[row]) + for col := 0; col < 5; col++ { + new_board[row][col].Val, _ = strconv.Atoi(line_data[col]) + new_board[row][col].Marked = false + } + } + return &new_board, nil }