Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abhinav Jha golang assignment #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
39 changes: 39 additions & 0 deletions play/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
24 changes: 22 additions & 2 deletions reader/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}