10000 got example thing working · jamesjarvis/adventofcode@45b82c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45b82c3

Browse files
committed
got example thing working
1 parent 90fefde commit 45b82c3

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

04/main.go

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,56 @@ var input string
1313
//go:embed inputBoards.txt
1414
var inputBoards string
1515

16+
// Board contains the board 2d array and also a map of seen numbers.
17+
type Board struct {
18+
boardArr [][]int
19+
seenNumbers map[int]struct{}
20+
}
21+
22+
// checkBoard checks whether the board has won.
23+
// The board is a 5x5 grid so that makes things a bit easier?
24+
func (b *Board) checkBoard() bool {
25+
nHoriz := 0
26+
nVer := 0
27+
28+
const size = 5
29+
30+
// horiz: i == horiz, j == ver
31+
// ver: i == ver, j == horiz
32+
for i := 0; i < len(b.boardArr); i++ {
33+
nHoriz = 0
34+
nVer = 0
35+
for j := 0; j < len(b.boardArr[i]); j++ {
36+
if _, ok := b.seenNumbers[b.boardArr[i][j]]; ok {
37+
nHoriz++
38+
nVer++
39+
}
40+
}
41+
if nHoriz == size || nVer == size {
42+
return true
43+
}
44+
}
45+
46+
return false
47+
}
48+
49+
// markBoard just marks the board number as seen.
50+
func (b *Board) markBoard(n int) {
51+
b.seenNumbers[n] = struct{}{}
52+
}
53+
54+
// markBoard just marks the board number as seen.
55+
func (b *Board) sumOfUnmarked() (sum int) {
56+
for i := 0; i < len(b.boardArr); i++ {
57+
for j := 0; j < len(b.boardArr[i]); j++ {
58+
if _, ok := b.seenNumbers[b.boardArr[i][j]]; !ok {
59+
sum += b.boardArr[i][j]
60+
}
61+
}
62+
}
63+
return sum
64+
}
65+
1666
func< 10000 /span> main() {
1767
if input == "" {
1868
panic("input cannot be empty")
@@ -32,20 +82,17 @@ func main() {
3282

3383
fmt.Println(inputarr)
3484

35-
boards := [][][]int{}
36-
37-
// string: " 3 15 0 2 22"
38-
// int: 1
39-
// float/real: 2.3
40-
// bool: true/false
41-
// arrays: [type, type]
85+
boards := []*Board{}
4286

4387
boardsstrarr := strings.Split(inputBoards, "\n")
4488

4589
board := [][]int{}
4690
for _, line := range boardsstrarr {
4791
if line == "" {
48-
boards = append(boards, board)
92+
boards = append(boards, &Board{
93+
boardArr: board,
94+
seenNumbers: map[int]struct{}{},
95+
})
4996
board = [][]int{}
5097
continue
5198
}
@@ -65,8 +112,20 @@ func main() {
65112
board = append(board, boardline)
66113
}
67114

68-
fmt.Println(boards)
115+
answer := ifMusicBeTheFoodOfLovePlayOn(boards, inputarr)
116+
fmt.Printf("Winner: %d\n", answer)
117+
}
69118

70-
// ox, co2 := getShit(inputstrarr)
71-
// fmt.Printf("Answer: %d\n", ox*co2)
119+
// ifMusicBeTheFoodOfLovePlayOn returns the sum of all unmarked numbers on the winning board times the winning number.
120+
func ifMusicBeTheFoodOfLovePlayOn(boards []*Board, numbers []int) int {
121+
for _, call := range numbers {
122+
for _, board := range boards {
123+
board.markBoard(call)
124+
if board.checkBoard() {
125+
fmt.Printf("Found winning board! %v\n", board.< 5287 span class=pl-c1>boardArr)
126+
return call * board.sumOfUnmarked()
127+
}
128+
}
129+
}
130+
return 0
72131
}

0 commit comments

Comments
 (0)
0