clean up day 4 a little
This commit is contained in:
parent
77170d40b9
commit
47ac68b261
46
04/main.go
46
04/main.go
@ -54,6 +54,19 @@ func contains(i int, a []int) bool {
|
|||||||
return false
|
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:
|
// a bingo card like this:
|
||||||
// 60 79 46 9 58
|
// 60 79 46 9 58
|
||||||
// 97 81 6 94 84
|
// 97 81 6 94 84
|
||||||
@ -75,21 +88,10 @@ type bingoCard struct {
|
|||||||
|
|
||||||
func readBingoCard(scanner *bufio.Scanner) bingoCard {
|
func readBingoCard(scanner *bufio.Scanner) bingoCard {
|
||||||
card := bingoCard{
|
card := bingoCard{
|
||||||
allNumbers: make([]int, 0),
|
allNumbers: []int{},
|
||||||
rows: make([]map[int]struct{}, 5),
|
rows: []map[int]struct{}{0: {}, 1: {}, 2: {}, 3: {}, 4: {}},
|
||||||
columns: make([]map[int]struct{}, 5),
|
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++ {
|
for i := 0; i < 5; i++ {
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
@ -110,24 +112,12 @@ func readBingoCard(scanner *bufio.Scanner) bingoCard {
|
|||||||
|
|
||||||
func checkBoard(currentNumbers []int, card bingoCard) bool {
|
func checkBoard(currentNumbers []int, card bingoCard) bool {
|
||||||
for _, row := range card.rows {
|
for _, row := range card.rows {
|
||||||
hits := 0
|
if bingo(row, currentNumbers) {
|
||||||
for _, i := range currentNumbers {
|
|
||||||
if _, ok := row[i]; ok {
|
|
||||||
hits++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if hits == 5 {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, col := range card.columns {
|
for _, col := range card.columns {
|
||||||
hits := 0
|
if bingo(col, currentNumbers) {
|
||||||
for _, i := range currentNumbers {
|
|
||||||
if _, ok := col[i]; ok {
|
|
||||||
hits++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if hits == 5 {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user