-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_05_hydrothermal_venture_part_2.dart
More file actions
139 lines (127 loc) · 3.29 KB
/
day_05_hydrothermal_venture_part_2.dart
File metadata and controls
139 lines (127 loc) · 3.29 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// dart day_05_hydrothermal_venture_part_2.dart
import 'dart:io';
//read txt
Future<void> main() async {
final File file = File('inputs/day_05.txt');
final String contents = await file.readAsString();
List<String> inputList = contents.split(RegExp(' -> |,|\n'));
// // show inputList
// for (int i = 0; i < inputList.length; i++) {
// print(inputList[i]);
// }
// create and fill cords array
List<List<int>> cords = List<List<int>>.generate(
inputList.length ~/ 4, (int i) => List<int>.generate(4, (int j) => 0));
int c = 0;
int tableSize = 0;
for (int i = 0; i < (inputList.length ~/ 4); i++) {
for (int j = 0; j < 4; j++) {
cords[i][j] = int.parse(inputList[c]);
if (int.parse(inputList[c]) > tableSize) {
tableSize = int.parse(inputList[c]);
}
c++;
}
}
// // show variables
// print(c);
// print(inputList.length);
// print(tableSize);
// generate table of vents
List<List<int>> vents = List<List<int>>.generate(tableSize + 1,
(int i) => List<int>.generate(tableSize + 1, (int j) => 0));
// // show cords array
// for (int i = 0; i < inputList.length ~/ 4; i++) {
// print(cords[i]);
// }
// draw diagonal lines
int x = 0;
int y = 0;
for (int i = 0; i < inputList.length ~/ 4; i++) {
if ((cords[i][0] - cords[i][2]).abs() ==
(cords[i][1] - cords[i][3]).abs()) {
// if |x1 - x2| = |y1 - y2|
x = cords[i][0];
y = cords[i][1];
if (cords[i][1] < cords[i][3]) {
// y1 < y2
if (cords[i][0] < cords[i][2]) {
// x1 < x2
while (x <= cords[i][2]) {
vents[y][x]++;
x++;
y++;
}
} else {
// x1 > x2
while (x >= cords[i][2]) {
vents[y][x]++;
x--;
y++;
}
}
} else {
// if y1 > y2
if (cords[i][0] < cords[i][2]) {
// x1 < x2
while (x <= cords[i][2]) {
vents[y][x]++;
x++;
y--;
}
} else {
// x1 > x2
while (x >= cords[i][2]) {
vents[y][x]++;
x--;
y--;
}
}
}
}
}
// draw horizontal and vertical lines
for (int i = 0; i < inputList.length ~/ 4; i++) {
if (cords[i][0] == cords[i][2]) {
// if x1 = x2
if (cords[i][1] > cords[i][3]) {
// if y1 > y2
for (int y = cords[i][3]; y <= cords[i][1]; y++) {
vents[y][cords[i][0]]++;
}
} else {
for (int y = cords[i][1]; y <= cords[i][3]; y++) {
vents[y][cords[i][0]]++;
}
}
}
if (cords[i][1] == cords[i][3]) {
// if y1 = y2
if (cords[i][0] > cords[i][2]) {
// if x1 > x2
for (int x = cords[i][2]; x <= cords[i][0]; x++) {
vents[cords[i][1]][x]++;
}
} else {
for (int x = cords[i][0]; x <= cords[i][2]; x++) {
vents[cords[i][1]][x]++;
}
}
}
}
// calculate result
c = 0;
for (int x = 0; x < tableSize; x++) {
for (int y = 0; y < tableSize; y++) {
if (vents[y][x] >= 2) {
c++;
}
}
}
// // print vents array
// for (int i = 0; i <= tableSize; i++) {
// print(vents[i]);
// }
//print result
print(c);
}