This commit is contained in:
Max Nuding 2021-12-04 11:21:54 +01:00
parent 42e442da09
commit c09561597e
Signed by: phlaym
GPG Key ID: A06651BAB6777237
2 changed files with 612 additions and 27 deletions

View File

@ -23,17 +23,20 @@ struct Board {
}
mutating func checkHasWon() -> Bool {
//print("HasWon?: \(numbers)")
let foundInRows = numbers.contains { row in
var found = false
defer {
hasWon = hasWon || found
}
found = numbers.contains { row in
row.allSatisfy { $0 == nil }
}
if foundInRows {
if found {
return true
}
let foundInColumns = numbers.indices.contains { columnIndex in
return numbers.allSatisfy { $0[columnIndex] == nil }
found = numbers.indices.contains { columnIndex in
numbers.allSatisfy { $0[columnIndex] == nil }
}
hasWon = foundInColumns
hasWon = found
return hasWon
}
@ -72,9 +75,7 @@ struct Day04 {
func runA(boards: inout [Board], inputNumbers: [Int]) {
for inputNumber in inputNumbers {
//print("\nInput: \(inputNumber)")
for (idx, var board) in boards.enumerated() {
//print("Checking board: \(idx)")
if board.crossOf(num: inputNumber) && board.checkHasWon() {
print(board.calculateScore() * inputNumber)
return
@ -85,11 +86,13 @@ struct Day04 {
}
func runB(boards: inout [Board], inputNumbers: [Int]) {
var countWon = 0
for inputNumber in inputNumbers {
//print("\nInput: \(inputNumber)")
for (idx, var board) in boards.enumerated() {
//print("Checking board: \(idx)")
if !board.hasWon && board.crossOf(num: inputNumber) && board.checkHasWon() {
if board.crossOf(num: inputNumber) && !board.hasWon && board.checkHasWon() {
countWon += 1
}
if countWon == boards.count {
print(board.calculateScore() * inputNumber)
return
}

View File

@ -1,19 +1,601 @@
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
87,7,82,21,47,88,12,71,24,35,10,90,4,97,30,55,36,74,19,50,23,46,13,44,69,27,2,0,37,33,99,49,77,15,89,98,31,51,22,96,73,94,95,18,52,78,32,83,85,54,75,84,59,25,76,45,20,48,9,28,39,70,63,56,5,68,61,26,58,92,67,53,43,62,17,81,80,66,91,93,41,64,14,8,57,38,34,16,42,11,86,72,40,65,79,6,3,29,60,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 55 15 54 81
56 77 20 99 25
90 57 67 0 97
28 45 69 84 14
91 94 39 36 85
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
52 60 30 7 36
71 97 77 19 46
6 3 75 82 24
4 57 2 11 91
56 84 23 43 48
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7
69 60 78 87 11
79 30 93 39 63
32 31 23 90 17
57 98 13 46 9
65 94 26 77 19
87 68 81 63 95
91 43 98 49 70
67 46 12 10 3
65 93 39 29 34
45 35 78 40 59
95 11 27 19 78
8 47 87 44 10
48 83 93 52 23
16 13 21 1 4
69 12 40 39 81
50 73 63 93 86
55 76 33 22 75
61 23 82 37 99
26 29 85 74 2
19 78 30 40 6
58 2 95 92 78
25 56 26 9 38
43 83 4 87 8
79 61 18 69 49
24 29 5 41 89
56 8 72 92 9
34 26 28 66 22
51 42 89 87 90
98 48 40 18 19
13 80 71 79 52
87 77 82 38 48
92 36 85 66 71
97 29 94 9 1
70 50 19 45 84
43 6 72 54 89
9 60 6 17 14
97 2 10 84 0
7 98 51 8 93
54 77 37 36 1
72 29 26 47 19
92 22 25 19 36
80 14 24 59 78
29 45 88 54 4
37 85 77 46 6
42 52 43 26 72
41 84 36 95 49
22 17 83 47 99
25 77 12 71 39
76 73 60 79 85
63 94 11 54 51
88 29 7 26 45
25 11 9 82 28
43 5 41 70 39
20 69 14 79 37
78 59 98 93 6
99 24 81 51 57
56 47 7 50 66
84 85 71 86 31
17 74 90 26 73
15 11 12 61 45
61 27 84 49 21
76 15 5 0 2
35 7 79 77 20
40 91 72 57 89
11 33 18 42 78
21 38 46 53 50
49 10 9 59 67
3 51 96 44 63
93 45 82 27 47
69 92 41 85 95
14 70 5 4 34
59 98 53 45 7
97 29 58 1 84
19 69 37 93 57
86 68 35 30 73
38 21 7 1 43
41 9 54 0 11
51 94 99 75 72
89 78 79 2 73
18 90 85 84 10
33 68 5 26 8
51 85 20 49 58
27 24 88 11 21
9 65 62 72 31
95 53 12 14 83
1 22 76 11 56
97 95 79 55 14
20 53 27 77 23
37 49 30 72 84
91 68 8 18 5
45 68 6 49 92
43 90 20 1 78
77 89 5 10 56
84 28 73 62 72
21 47 51 36 14
30 71 52 21 13
20 8 73 38 92
17 50 59 3 14
19 76 41 4 90
57 25 94 82 62
52 76 18 60 9
29 38 5 96 63
58 70 28 82 1
40 49 43 25 19
92 56 80 66 69
58 60 90 33 92
13 74 0 80 84
77 97 51 70 56
15 87 68 98 55
1 64 35 3 83
77 43 69 58 34
42 12 75 27 28
74 54 30 2 15
63 21 81 90 9
6 22 29 65 19
60 89 43 26 86
47 52 12 17 90
56 54 96 37 75
3 80 0 19 85
39 61 13 59 73
94 59 82 21 68
87 19 69 79 31
64 57 72 43 25
49 5 81 67 76
95 16 58 52 51
33 36 1 61 85
11 74 37 89 30
24 59 31 76 62
4 50 81 69 78
66 57 9 94 7
6 20 26 2 9
52 34 63 89 13
16 65 30 29 27
97 18 47 53 19
79 7 38 33 39
90 20 79 95 44
0 96 98 74 60
80 65 36 93 67
54 25 81 84 10
21 56 83 14 66
99 1 17 41 95
66 29 47 74 43
63 68 72 97 96
92 79 13 93 4
25 42 44 78 23
17 19 98 55 14
2 35 42 22 64
52 32 69 27 82
85 16 89 41 81
10 70 95 56 84
64 4 94 51 35
25 60 33 24 80
59 40 14 47 93
32 74 42 46 26
49 75 7 95 79
39 98 43 56 73
92 63 13 61 36
4 34 35 10 11
30 19 75 26 41
54 37 65 15 58
89 64 83 43 42
71 32 25 63 34
80 93 99 86 16
31 33 94 27 91
8 17 81 41 85
16 89 41 87 53
27 29 64 30 81
20 47 18 1 50
56 82 93 4 45
71 21 63 25 15
61 12 97 41 32
51 85 64 16 99
68 2 43 10 86
69 5 38 94 11
92 74 20 83 90
98 96 44 95 36
63 42 56 24 21
40 7 65 48 59
18 11 2 58 13
86 41 0 53 20
0 74 23 35 48
21 34 97 10 2
17 25 80 89 60
15 27 94 68 7
72 75 69 32 85
29 72 40 13 9
48 41 21 66 94
20 23 42 53 84
93 14 7 57 77
67 36 10 81 0
85 92 45 94 75
58 79 9 98 5
14 39 40 48 88
96 61 36 62 7
32 81 77 8 27
78 80 98 62 87
90 53 91 81 23
46 15 4 63 74
30 6 47 64 44
12 45 95 68 99
88 4 53 84 25
71 36 20 46 74
48 70 5 63 94
15 65 78 33 67
99 64 76 97 26
64 96 69 13 60
31 26 86 38 46
35 90 45 97 40
98 87 21 61 1
27 67 77 29 66
74 97 72 66 1
34 50 2 57 33
29 71 52 88 73
16 7 39 40 24
15 38 80 54 17
22 99 67 95 97
60 68 38 18 96
89 46 57 8 11
7 12 61 83 35
17 74 42 52 58
10 19 44 12 41
82 71 1 26 55
52 88 6 97 48
16 66 36 50 29
67 2 65 0 45
68 16 99 57 12
36 50 32 19 91
43 21 52 31 69
41 82 37 49 75
20 74 87 76 63
81 10 7 80 78
29 86 6 13 67
19 12 23 54 68
64 85 40 43 87
16 5 59 3 34
79 37 13 17 31
56 22 24 7 58
65 76 38 39 85
45 36 90 3 66
25 52 28 61 71
18 20 21 74 33
77 30 29 56 26
86 49 44 75 62
11 47 10 90 97
64 94 6 43 67
77 59 44 58 41
85 30 22 56 19
62 16 35 32 88
91 94 34 51 9
31 33 13 60 90
94 56 72 12 59
64 78 2 80 96
41 99 69 21 79
17 88 36 37 85
89 7 66 15 84
61 80 26 83 11
62 42 51 79 31
86 98 64 28 58
2 19 71 35 52
14 34 0 32 44
1 50 76 15 12
96 55 73 61 19
74 0 79 5 8
3 36 53 67 52
60 49 93 43 85
71 75 35 44 1
82 96 22 68 81
19 25 52 62 97
28 64 73 99 92
38 60 42 70 20
38 15 77 19 74
5 89 17 10 16
96 48 40 42 57
21 18 51 56 29
86 63 25 45 93
98 36 26 69 2
70 75 18 89 81
33 24 10 76 47
28 59 54 49 58
62 19 53 73 16
96 84 52 86 47
70 14 26 46 81
90 53 66 16 87
34 9 91 83 41
61 29 13 49 77
78 49 94 33 18
91 83 55 73 44
19 4 14 6 3
45 28 57 12 9
41 1 82 79 95
74 38 35 88 6
80 21 32 3 71
1 34 17 8 2
94 24 84 26 91
64 27 83 60 23
93 18 27 52 28
83 90 40 8 87
21 3 4 99 26
14 13 71 41 91
20 63 69 38 30
49 20 31 70 5
37 7 94 85 84
68 90 45 4 6
96 46 10 97 63
9 89 34 25 52
9 79 30 87 53
43 3 47 32 0
59 29 44 11 98
31 75 41 91 12
8 14 62 73 94
43 52 60 16 61
40 5 99 90 44
35 78 83 75 64
29 10 32 11 79
59 18 72 54 30
11 19 84 76 12
7 91 24 81 93
4 59 78 63 69
46 72 74 43 28
94 55 73 35 1
51 6 64 31 82
52 75 43 68 42
95 49 21 63 46
96 19 33 97 70
1 60 78 56 23
73 40 83 36 26
32 61 43 66 39
63 15 34 98 3
68 79 44 12 27
4 57 96 7 52
63 77 12 89 17
64 16 90 46 88
33 65 58 55 85
45 83 41 71 68
57 81 56 13 15
34 33 49 54 10
97 77 14 22 59
26 45 16 13 76
3 87 75 66 47
2 53 56 23 63
42 38 69 33 96
32 90 53 2 59
56 22 45 11 54
92 25 8 30 60
75 41 19 79 70
17 88 80 21 3
96 91 27 15 72
61 62 5 23 75
67 8 37 40 65
76 46 49 13 35
90 7 11 28 54
98 37 88 35 14
12 20 71 44 80
24 55 97 83 6
36 59 66 48 26
90 87 75 28 7
32 14 80 62 6
81 12 18 22 52
51 89 16 41 93
55 0 43 40 11
28 49 8 75 21
34 77 84 66 17
6 82 98 22 1
20 93 94 96 40
58 80 18 63 19
94 63 36 29 53
27 61 32 79 71
8 87 67 58 1
78 97 88 28 12
59 74 70 89 80
48 37 57 42 26
31 69 10 1 91
23 38 99 79 49
68 19 61 75 43
90 59 30 94 44
76 56 21 17 24
73 59 71 46 50
35 9 97 92 26
33 98 30 7 34
52 5 91 68 60
35 63 55 99 8
2 94 66 29 14
50 54 72 57 44
90 34 9 69 95
31 45 49 27 33
62 14 84 0 85
59 36 64 43 31
47 4 33 8 24
28 46 39 20 22
3 80 72 21 40
19 92 34 62 2
7 98 44 3 5
59 45 25 33 52
85 87 65 9 89
16 63 36 28 78
72 84 91 77 93
48 99 80 21 90
52 6 15 86 47
19 32 83 53 22
94 1 61 38 10
52 1 12 93 33
59 11 82 96 10
54 36 97 32 55
5 2 76 49 81
48 60 62 98 27
65 62 7 22 52
19 91 53 9 81
51 20 82 99 60
24 72 3 49 54
35 94 74 38 42
60 28 11 10 35
18 40 22 78 27
53 3 52 1 63
41 31 23 16 68
70 98 42 84 33
64 99 92 95 74
35 86 87 33 40
3 79 13 44 39
34 57 69 50 80
9 42 29 77 6
22 71 46 44 88
42 70 94 60 43
50 92 95 57 63
24 39 35 18 68
28 34 77 81 91
49 41 21 59 32
44 46 19 20 22
88 45 85 10 38
84 18 70 77 11
33 43 63 0 47
86 23 32 30 25
85 40 26 39 71
88 3 72 93 19
53 59 52 55 73
51 58 90 49 81
33 25 87 46 58
90 55 53 82 54
30 89 11 81 29
41 83 28 35 94
49 97 48 72 34
86 22 0 27 89
78 64 87 59 11
85 19 60 61 3
31 55 17 79 5
93 4 1 26 66
96 11 86 81 31
28 37 7 64 36
27 29 6 53 69
30 21 8 50 60
43 1 12 63 97
81 3 10 25 41
4 54 96 45 51
76 68 74 52 63
26 77 21 82 69
70 6 19 43 93
63 72 20 36 53
65 84 61 92 67
28 15 69 52 68
95 21 44 48 96
93 78 94 62 91
39 13 53 94 1
95 42 48 93 79
71 32 65 2 82
96 22 99 46 54
37 35 34 29 11
52 40 66 96 26
11 69 62 91 85
58 92 75 48 71
19 31 28 12 15
99 76 90 87 98
52 95 93 9 76
11 63 68 89 17
13 8 99 78 33
14 53 6 4 47
26 35 57 67 73
96 71 14 40 34
16 80 55 67 19
36 4 63 91 61
46 77 43 81 23
22 54 79 76 89
69 28 48 84 35
31 86 58 91 45
37 25 97 13 14
65 32 80 99 23
94 63 60 41 90
19 47 13 80 58
69 53 15 81 20
21 39 70 22 26
56 92 61 65 42
96 76 86 87 41