-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03_binary_diagnostic_part_2.dart
67 lines (62 loc) · 1.54 KB
/
day_03_binary_diagnostic_part_2.dart
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
// dart day_03_binary_diagnostic_part_2.dart
import 'dart:io';
Future<void> main() async {
final File file = File('inputs/day_03.txt');
final String contents = await file.readAsString();
List<String> inputList = contents.split('\n');
List<String> o2 = inputList;
List<String> co2 = inputList;
List<String> temp = <String>[];
String lookup;
for (int j = 0; j < inputList[0].length - 1; j++) {
int c = 0;
if (o2.length > 1) {
for (int i = 0; i < o2.length; i++) {
if (o2[i][j] == '0') {
c++;
}
}
if (c > o2.length / 2) {
lookup = '0';
} else {
lookup = '1';
}
for (int i = 0; i < o2.length; i++) {
if (o2[i][j] == lookup) {
temp.add(o2[i]);
}
}
o2 = temp;
temp = <String>[];
}
}
for (int j = 0; j < inputList[0].length - 1; j++) {
int c = 0;
if (co2.length > 1) {
for (int i = 0; i < co2.length; i++) {
if (co2[i][j] == '0') {
c++;
}
}
if (c <= co2.length / 2) {
lookup = '0';
} else {
lookup = '1';
}
for (int i = 0; i < co2.length; i++) {
if (co2[i][j] == lookup) {
temp.add(co2[i]);
}
}
co2 = temp;
temp = <String>[];
}
}
// print(o2[0]);
// print(co2[0]);
int lsr =
int.parse(o2[0].toString(), radix: 2) * int.parse(co2[0].toString(), radix: 2);
// print(int.parse(o2[0].toString(), radix: 2));
// print(int.parse(co2[0].toString(), radix: 2));
print(lsr);
}