-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.dart
198 lines (151 loc) · 5.7 KB
/
day03.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import 'package:adventofcode2023/day.dart';
import 'package:adventofcode2023/helper/cell.dart';
import 'package:adventofcode2023/helper/string_extensions.dart';
import 'package:adventofcode2023/helper/table.dart';
import 'package:collection/collection.dart';
class Day3 implements Day {
@override
int get dayNumber => 3;
@override
String solveFirstPuzzle(String puzzleInput) {
var rows = puzzleInput.splitPerLine().map((e) => e.split(''));
var table = Table(rows);
var cellsWithSymbolsAsNeighbor = _getCellsWithSymbolsAsNeighbor(table);
var partNumbers = _getPartNumbers(cellsWithSymbolsAsNeighbor, table);
var result = partNumbers.sum;
return result.toString();
}
@override
String solveSecondPuzzle(String puzzleInput) {
var rows = puzzleInput.splitPerLine().map((e) => e.split(''));
var table = Table(rows);
List<(Cell<String>, Iterable<Cell<String>>)> gearCellsWithNeighborPartNumberCells =
_getGearCellsWithNeighborPartNumberCells(table);
List<int> gearValues = _getGearValues(gearCellsWithNeighborPartNumberCells, table);
var result = gearValues.sum;
return result.toString();
}
List<int> _getGearValues(
List<(Cell<String>, Iterable<Cell<String>>)> gearCellsWithNeighborPartNumberCells,
Table<String> table) {
List<int> gearValues = [];
for (var gear in gearCellsWithNeighborPartNumberCells) {
var gearPartNumbers = _getPartNumbers(gear.$2.toList(), table);
if (gearPartNumbers.length == 2) {
gearValues.add((gearPartNumbers.reduce((value, element) => value * element)));
}
}
return gearValues;
}
List<(Cell<String>, Iterable<Cell<String>>)> _getGearCellsWithNeighborPartNumberCells(
Table<String> table) {
var possibleGears = table.asIterableCells().where((element) => element.value == '*');
List<(Cell<String>, Iterable<Cell<String>>)> gearCellsWithNeighborPartNumberCells = [];
for (var cell in possibleGears) {
var neighbors = _getNeighborCells(cell, table);
var partNumbers = neighbors.where((element) => element.value.isNumeric());
if (partNumbers.length >= 2) {
gearCellsWithNeighborPartNumberCells.add((cell, partNumbers));
}
}
return gearCellsWithNeighborPartNumberCells;
}
List<Cell<String>> _getCellsWithSymbolsAsNeighbor(Table<String> table) {
List<Cell<String>> cellsWithSymbolsAsNeighbor = [];
for (var cell in table.asIterableCells()) {
if (!cell.value.isNumeric()) {
continue;
}
var neighbors = _getNeighborCells(cell, table);
if (neighbors.any((element) => element.value != '.' && !element.value.isNumeric())) {
cellsWithSymbolsAsNeighbor.add(cell);
}
}
return cellsWithSymbolsAsNeighbor;
}
Iterable<int> _getPartNumbers(
List<Cell<String>> cellsWithSymbolsAsNeighbor, Table<String> table) {
List<(int, Cell<String>)> partNumbersWithCells = [];
for (var cell in cellsWithSymbolsAsNeighbor) {
var partNumber = _getPartNumberFromCell(cell, table);
var partNumberWithCell = (partNumber, cell);
if (_partNumbersWithCellsAlreadyContainsPartNumber(
partNumbersWithCells, partNumberWithCell)) {
continue;
}
partNumbersWithCells.add(partNumberWithCell);
}
return partNumbersWithCells.map((e) => e.$1);
}
bool _partNumbersWithCellsAlreadyContainsPartNumber(
List<(int, Cell<String>)> partNumbersWithCells, (int, Cell<String>) partNumberWithCell) {
return partNumbersWithCells.any((element) {
return element.$1 == partNumberWithCell.$1 &&
element.$2.y == partNumberWithCell.$2.y &&
(element.$2.x - partNumberWithCell.$2.x).abs() < 4;
});
}
int _getPartNumberFromCell(Cell<String> cell, Table<String> table) {
String partNumber = cell.value;
var leftNeighborCells = _getLeftNeighborCells(cell, table);
var rightNeighborCells = _getRightNeighborCells(cell, table);
for (var element in leftNeighborCells) {
if (!element.value.isNumeric()) {
break;
}
partNumber = element.value + partNumber;
}
for (var element in rightNeighborCells) {
if (!element.value.isNumeric()) {
break;
}
partNumber = partNumber + element.value;
}
return int.parse(partNumber);
}
List<Cell<String>> _getNeighborCells(Cell<String> cell, Table<String> table) {
var x = cell.x;
var y = cell.y;
var neighborCoordinates = {
(y - 1, x),
(y + 1, x),
(y - 1, x + 1),
(y - 1, x - 1),
(y + 1, x - 1),
(y + 1, x + 1),
(y, x + 1),
(y, x - 1)
};
List<Cell<String>> neighborValues = [];
for (var element in neighborCoordinates) {
try {
neighborValues.add(Cell(element.$2, element.$1, table.getValue(element.$1, element.$2)));
} catch (e) {}
}
return neighborValues;
}
List<Cell<String>> _getLeftNeighborCells(Cell<String> cell, Table<String> table) {
var x = cell.x;
var y = cell.y;
var neighborCoordinates = {(y, x - 1), (y, x - 2)};
List<Cell<String>> neighborValues = [];
for (var element in neighborCoordinates) {
try {
neighborValues.add(Cell(element.$2, element.$1, table.getValue(element.$1, element.$2)));
} catch (e) {}
}
return neighborValues;
}
List<Cell<String>> _getRightNeighborCells(Cell<String> cell, Table<String> table) {
var x = cell.x;
var y = cell.y;
var neighborCoordinates = {(y, x + 1), (y, x + 2)};
List<Cell<String>> neighborValues = [];
for (var element in neighborCoordinates) {
try {
neighborValues.add(Cell(element.$2, element.$1, table.getValue(element.$1, element.$2)));
} catch (e) {}
}
return neighborValues;
}
}