-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_06_lanternfish_part_2.dart
57 lines (49 loc) · 1.17 KB
/
day_06_lanternfish_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
// dart day_06_lanternfish_part_2.dart
import 'dart:io';
// read txt
Future<void> main() async {
final File file = File('inputs/day_06.txt');
final String contents = await file.readAsString();
List<String> inputList = contents.split(',');
List<int> intList = List<int>.generate(inputList.length, (int i) => 0);
List<int> map = List<int>.generate(9, (int i) => 0);
// // show input list
// for (int i = 0; i < inputList.length; i++) {
// print(inputList[i]);
// }
// copy to list of int
for (int i = 0; i < inputList.length; i++) {
intList[i] = int.parse(inputList[i]);
}
// initial map
int c = 0;
for (int j = 0; j <= 8; j++) {
for (int i = 0; i < intList.length; i++) {
if (intList[i] == j) {
c++;
}
}
map[j] = c;
c = 0;
}
// // show int list
// for (int i = 0; i < intList.length; i++) {
// print(intList[i]);
// }
// population growth
int t = 0;
for (int j = 0; j < 256; j++) {
t = map[0];
for (int i = 0; i < 8; i++) {
map[i] = map[i + 1];
}
map[8] = t;
map[6] += t;
}
// result
int sum = 0;
for (int i = 0; i <= 8; i++) {
sum += map[i];
}
print(sum);
}