-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.d
More file actions
110 lines (96 loc) · 3.16 KB
/
Copy pathday04.d
File metadata and controls
110 lines (96 loc) · 3.16 KB
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
module day04;
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
string[] input = File("input/day04.txt")
.byLine()
.map!(to!string)
.map!strip
// .filter!(line => line.length > 0)
.array();
string[] numbers = input[0].split(",").array();
string[][][] boards;
int idx = 2;
while (idx + 5 <= input.length) {
auto b = input[idx..idx+5]
.map!(function(string line) {
return line.split(" ")
.filter!(p => p.length > 0)
// .map!(to!int)
.array();
})
.array();
idx += 6;
boards ~= [b];
// writeln(b);
}
int[] won_boards;
int[int] won_boards_scores;
foreach(string number; numbers) {
// mark boards
foreach(string[][] board; boards) {
foreach(string[] line; board) {
for (int i = 0; i < line.length; i++) {
if (line[i] == number) {
line[i] = "*" ~ number;
}
}
}
}
// check boards
for (int i = 0; i < boards.length; i++) {
string[][] board = boards[i];
if (i in won_boards_scores) {
continue;
}
if (check_board(board)) {
int score = board_score(board, number);
// writeln("Winning board is: ", board);
// writeln("Its score is: ", score);
won_boards ~= [i];
won_boards_scores[i] = board_score(board, number);
}
}
}
// writeln(won_boards);
writeln("First won board score: ", won_boards_scores[won_boards[0]]);
writeln("Last won board score: ", won_boards_scores[won_boards[$-1]]);
}
int board_score(string[][] board, string called_num) {
int board_sum = board
.map!(line => line.filter!(v => v[0] != '*').map!(to!int).sum())
.sum();
return board_sum * called_num.to!int;
}
bool check_board(string[][] board) {
for (int i = 0; i < board.length; i++) {
bool has_horiz = true;
bool has_vert = true;
for (int j = 0; j < board[i].length; j++) {
if (board[i][j][0] != '*') {
has_horiz = false;
}
if (board[j][i][0] != '*') {
has_vert = false;
}
if (!has_horiz && !has_vert) {
break;
}
}
if (has_horiz || has_vert) {
return true;
}
}
return false;
}
unittest {
assert(check_board([["1", "2", "3"], ["1", "2", "3"], ["1", "2", "3"]]) == false);
assert(check_board([["*1", "2", "3"], ["1", "*2", "3"], ["1", "2", "*3"]]) == false);
assert(check_board([["*1", "2", "*3"], ["1", "*2", "3"], ["*1", "2", "*3"]]) == false);
assert(check_board([["*1", "*2", "3"], ["*1", "*2", "3"], ["1", "2", "3"]]) == false);
assert(check_board([["*1", "*2", "*3"], ["1", "*2", "3"], ["1", "2", "*3"]]) == true);
assert(check_board([["*1", "2", "*3"], ["1", "2", "*3"], ["1", "2", "*3"]]) == true);
}