-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.d
116 lines (98 loc) · 2.78 KB
/
day03.d
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
module day03;
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
string[] report = File("input/day03.txt")
.byLine()
.map!(to!string)
.map!strip
.filter!(line => line.length > 0)
.array();
// pt 1
power_consumption(report);
// pt 2
auto oxy = oxygen_rating(report);
auto co2 = co2_rating(report);
writeln(oxy.to!int(2) * co2.to!int(2));
}
struct BinCount {
int ones;
int zeros;
}
BinCount count_ones_zeros(string[] report, int position) {
BinCount result = BinCount();
foreach (string line; report) {
if (line[position] == '1') {
result.ones++;
} else {
result.zeros++;
}
}
return result;
}
string oxygen_rating(string[] report) {
return report_rating(report, (string[] report, int position) {
BinCount c = count_ones_zeros(report, position);
if (c.ones >= c.zeros) {
report = report.filter!(r => r[position] == '1').array();
} else {
report = report.filter!(r => r[position] == '0').array();
}
return report;
});
}
string co2_rating(string[] report) {
return report_rating(report, (string[] report, int position) {
BinCount c = count_ones_zeros(report, position);
if (c.zeros <= c.ones) {
report = report.filter!(r => r[position] == '0').array();
} else {
report = report.filter!(r => r[position] == '1').array();
}
return report;
});
}
string report_rating(string[] report, string[] function (string[] report, int position) report_filter) {
string[] current_report = report[0 .. report.length];
int current_position = 0;
while (current_report.length > 1) {
current_report = report_filter(current_report, current_position);
current_position++;
}
return current_report[0];
}
void power_consumption(string[] report) {
int[] ones = new int[report[0].length];
int[] zeros = new int[report[0].length];
foreach (string number; report) {
for (int i = 0; i < number.length; i++) {
char c = number[i];
if (c == '0') {
zeros[i]++;
} else if (c == '1') {
ones[i]++;
} else {
writeln("oof!");
}
}
}
// writeln(ones, zeros);
string gamma = "";
string epsilon = "";
for (int i = 0; i < ones.length; i++) {
int oc = ones[i];
int zc = zeros[i];
if (oc > zc) {
gamma ~= "1";
epsilon ~= "0";
} else {
gamma ~= "0";
epsilon ~= "1";
}
}
// writeln(gamma, ", ", epsilon);
writeln(gamma.to!int(2) * epsilon.to!int(2));
}