From 47ac68b261c7a2c79e36cc5b910939407a1e243f Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sat, 4 Dec 2021 13:07:25 -0500 Subject: [PATCH] clean up day 4 a little --- 04/main.go | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/04/main.go b/04/main.go index 8eb6c0f..cf0fddf 100644 --- a/04/main.go +++ b/04/main.go @@ -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 } }