-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_1.dart
56 lines (49 loc) · 1.02 KB
/
part_1.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
import 'dart:io';
int scratchcardsWorth() {
try {
final file = File('Dart/Day4/input.txt');
final lines = file.readAsLinesSync();
var t = 0;
for (final line in lines) {
final parts = line.split(":");
if (parts.length < 2) {
continue;
}
var x = parts[1].trim();
final arrays = x.split(" | ");
if (arrays.length != 2) {
continue;
}
final a = parseIntArray(arrays[0]);
final b = parseIntArray(arrays[1]);
var j = 0;
for (final q in b) {
for (final value in a) {
if (q == value) {
j++;
break;
}
}
}
if (j > 0) {
t += 1 << (j - 1);
}
}
print(t);
return t;
} catch (e) {
print('Error: $e');
return 0;
}
}
List<int> parseIntArray(String s) {
final result = <int>[];
final parts = s.split(' ');
for (final part in parts) {
final num = int.tryParse(part);
if (num != null) {
result.add(num);
}
}
return result;
}