diff --git a/d4/common.go b/d4/common.go index 0eebd69..54c127c 100644 --- a/d4/common.go +++ b/d4/common.go @@ -66,41 +66,6 @@ func gotBingo(b board) bool { return false } -func findBingo(boards []board, takes []string) (board, int) { - - for _, take := range takes { - converted, _ := strconv.Atoi(take) - for _, board := range boards { - marked := markBoard(board, converted) - if gotBingo(marked) { - return marked, converted - } - } - } - - return nil, 0 -} - -func findBingo2(boards []board, takes []string) (board, int) { - bingos := make(map[*board]int) - wins := []*board{} - for _, take := range takes { - converted, _ := strconv.Atoi(take) - for boardIndex := range boards { - board := &boards[boardIndex] - if _, ok := bingos[board]; !ok { - marked := markBoard(*board, converted) - if gotBingo(marked) { - bingos[board] = converted - wins = append(wins, board) - } - } - } - } - - return *wins[len(wins)-1], bingos[wins[len(wins)-1]] -} - func score(b board, take int) int { sum := 0 diff --git a/d4/p1.go b/d4/p1.go index 42ac77d..f83235f 100644 --- a/d4/p1.go +++ b/d4/p1.go @@ -1,9 +1,25 @@ package d4 import ( + "strconv" "strings" ) +func findBingo(boards []board, takes []string) (board, int) { + + for _, take := range takes { + converted, _ := strconv.Atoi(take) + for _, board := range boards { + marked := markBoard(board, converted) + if gotBingo(marked) { + return marked, converted + } + } + } + + return nil, 0 +} + func P1(input string) int { lines := strings.Split(input, "\n\n") takes := strings.Split(lines[0], ",") diff --git a/d4/p2.go b/d4/p2.go index 0d4a431..6feb461 100644 --- a/d4/p2.go +++ b/d4/p2.go @@ -1,6 +1,29 @@ package d4 -import "strings" +import ( + "strconv" + "strings" +) + +func findBingo2(boards []board, takes []string) (board, int) { + bingos := make(map[*board]int) + wins := []*board{} + for _, take := range takes { + converted, _ := strconv.Atoi(take) + for boardIndex := range boards { + board := &boards[boardIndex] + if _, ok := bingos[board]; !ok { + marked := markBoard(*board, converted) + if gotBingo(marked) { + bingos[board] = converted + wins = append(wins, board) + } + } + } + } + + return *wins[len(wins)-1], bingos[wins[len(wins)-1]] +} func P2(input string) int { lines := strings.Split(input, "\n\n")