-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.go
219 lines (201 loc) · 4.49 KB
/
day04.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"aoc2021/util"
"fmt"
"strconv"
"strings"
)
func main() {
lines := util.GetLines("day04\\04.in")
day04a(lines)
day04b(lines)
}
type board struct {
row1, row2, row3, row4, row5 []int
col1, col2, col3, col4, col5 []int
winner bool
}
func newBoard(row1, row2, row3, row4, row5 []int) board {
col1, col2, col3, col4, col5 := make([]int, 5), make([]int, 5), make([]int, 5), make([]int, 5), make([]int, 5)
col1[0] = row1[0]
col1[1] = row2[0]
col1[2] = row3[0]
col1[3] = row4[0]
col1[4] = row5[0]
col2[0] = row1[1]
col2[1] = row2[1]
col2[2] = row3[1]
col2[3] = row4[1]
col2[4] = row5[1]
col3[0] = row1[2]
col3[1] = row2[2]
col3[2] = row3[2]
col3[3] = row4[2]
col3[4] = row5[2]
col4[0] = row1[3]
col4[1] = row2[3]
col4[2] = row3[3]
col4[3] = row4[3]
col4[4] = row5[3]
col5[0] = row1[4]
col5[1] = row2[4]
col5[2] = row3[4]
col5[3] = row4[4]
col5[4] = row5[4]
return board{row1, row2, row3, row4, row5, col1, col2, col3, col4, col5, false}
}
func (b *board) addNum(num int) {
for i := range b.row1 {
switch num {
case b.row1[i]:
b.row1[i] -= 100
case b.row2[i]:
b.row2[i] -= 100
case b.row3[i]:
b.row3[i] -= 100
case b.row4[i]:
b.row4[i] -= 100
case b.row5[i]:
b.row5[i] -= 100
}
switch num {
case b.col1[i]:
b.col1[i] -= 100
case b.col2[i]:
b.col2[i] -= 100
case b.col3[i]:
b.col3[i] -= 100
case b.col4[i]:
b.col4[i] -= 100
case b.col5[i]:
b.col5[i] -= 100
}
}
}
func (b *board) isWinner() bool {
if b.winner ||
allNeg(b.row1) ||
allNeg(b.row2) ||
allNeg(b.row3) ||
allNeg(b.row4) ||
allNeg(b.row5) ||
allNeg(b.col1) ||
allNeg(b.col2) ||
allNeg(b.col3) ||
allNeg(b.col4) ||
allNeg(b.col5) {
b.winner = true
return true
}
return false
}
func (b *board) unmarkedSum() int {
sum := 0
sum += unusedPerRow(b.row1)
sum += unusedPerRow(b.row2)
sum += unusedPerRow(b.row3)
sum += unusedPerRow(b.row4)
sum += unusedPerRow(b.row5)
return sum
}
func allNeg(slice []int) bool {
for _, v := range slice {
if v >= 0 {
return false
}
}
return true
}
func unusedPerRow(slice []int) int {
sum := 0
for _, v := range slice {
if v > 0 {
sum += v
}
}
return sum
}
func convStringSliceToIntSlice(s string) []int {
slice := strings.Fields(s)
intslice := make([]int, len(slice))
for i, s := range slice {
intslice[i], _ = strconv.Atoi(s)
}
return intslice
}
func day04a(lines []string) {
var drawn []int
boardInput := make([][]int, 5)
var boards []board
unmarkedSum := 0
lastNum := 0
lineCounter := 0
for i, line := range lines {
if i == 0 {
drawn = convStringSliceToIntSlice(strings.Replace(line, ",", " ", -1))
} else if len(line) != 0 {
newIntSlice := convStringSliceToIntSlice(line)
boardInput[lineCounter] = newIntSlice
lineCounter++
if lineCounter == 5 {
anotherBoard := newBoard(boardInput[0], boardInput[1], boardInput[2], boardInput[3], boardInput[4])
boardInput = make([][]int, 5)
boards = append(boards, anotherBoard)
lineCounter = 0
}
}
}
out:
for _, num := range drawn {
for _, b := range boards {
b.addNum(num)
if b.isWinner() {
unmarkedSum = b.unmarkedSum()
lastNum = num
break out
}
}
}
fmt.Printf("Solution for part A: %d * %d = %d\n", unmarkedSum, lastNum, unmarkedSum*lastNum)
}
func day04b(lines []string) {
var drawn []int
boardInput := make([][]int, 5)
var boards []board
unmarkedSum := 0
lastNum := 0
lineCounter := 0
for i, line := range lines {
if i == 0 {
drawn = convStringSliceToIntSlice(strings.Replace(line, ",", " ", -1))
} else if len(line) != 0 {
newIntSlice := convStringSliceToIntSlice(line)
boardInput[lineCounter] = newIntSlice
lineCounter++
if lineCounter == 5 {
anotherBoard := newBoard(boardInput[0], boardInput[1], boardInput[2], boardInput[3], boardInput[4])
boardInput = make([][]int, 5)
boards = append(boards, anotherBoard)
lineCounter = 0
}
}
}
winners := 0
out:
for _, num := range drawn {
for _, b := range boards {
if !b.isWinner() {
b.addNum(num)
if b.isWinner() {
unmarkedSum = b.unmarkedSum()
lastNum = num
winners++
}
}
if winners == len(boards) {
break out
}
}
}
fmt.Printf("Solution for part B: %d * %d = %d\n", unmarkedSum, lastNum, unmarkedSum*lastNum)
}