refactor d4

This commit is contained in:
Gregory 2022-02-22 01:55:00 +02:00
parent 0338b14930
commit dfb6e1952d
3 changed files with 40 additions and 36 deletions

View file

@ -66,41 +66,6 @@ func gotBingo(b board) bool {
return false 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 { func score(b board, take int) int {
sum := 0 sum := 0

View file

@ -1,9 +1,25 @@
package d4 package d4
import ( import (
"strconv"
"strings" "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 { func P1(input string) int {
lines := strings.Split(input, "\n\n") lines := strings.Split(input, "\n\n")
takes := strings.Split(lines[0], ",") takes := strings.Split(lines[0], ",")

View file

@ -1,6 +1,29 @@
package d4 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 { func P2(input string) int {
lines := strings.Split(input, "\n\n") lines := strings.Split(input, "\n\n")