clean up day 4 a little

This commit is contained in:
David 2021-12-04 13:07:25 -05:00
parent 77170d40b9
commit 47ac68b261

View File

@ -54,6 +54,19 @@ func contains(i int, a []int) bool {
return false
}
func bingo(set map[int]struct{}, drawnNumbers []int) bool {
hits := 0
for _, i := range drawnNumbers {
if _, ok := set[i]; ok {
hits++
}
}
if hits == 5 {
return true
}
return false
}
// a bingo card like this:
// 60 79 46 9 58
// 97 81 6 94 84
@ -75,21 +88,10 @@ type bingoCard struct {
func readBingoCard(scanner *bufio.Scanner) bingoCard {
card := bingoCard{
allNumbers: make([]int, 0),
rows: make([]map[int]struct{}, 5),
columns: make([]map[int]struct{}, 5),
allNumbers: []int{},
rows: []map[int]struct{}{0: {}, 1: {}, 2: {}, 3: {}, 4: {}},
columns: []map[int]struct{}{0: {}, 1: {}, 2: {}, 3: {}, 4: {}},
}
// what a pain
card.rows[0] = make(map[int]struct{})
card.rows[1] = make(map[int]struct{})
card.rows[2] = make(map[int]struct{})
card.rows[3] = make(map[int]struct{})
card.rows[4] = make(map[int]struct{})
card.columns[0] = make(map[int]struct{})
card.columns[1] = make(map[int]struct{})
card.columns[2] = make(map[int]struct{})
card.columns[3] = make(map[int]struct{})
card.columns[4] = make(map[int]struct{})
for i := 0; i < 5; i++ {
scanner.Scan()
@ -110,24 +112,12 @@ func readBingoCard(scanner *bufio.Scanner) bingoCard {
func checkBoard(currentNumbers []int, card bingoCard) bool {
for _, row := range card.rows {
hits := 0
for _, i := range currentNumbers {
if _, ok := row[i]; ok {
hits++
}
}
if hits == 5 {
if bingo(row, currentNumbers) {
return true
}
}
for _, col := range card.columns {
hits := 0
for _, i := range currentNumbers {
if _, ok := col[i]; ok {
hits++
}
}
if hits == 5 {
if bingo(col, currentNumbers) {
return true
}
}