-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.cpp
More file actions
91 lines (80 loc) · 2.33 KB
/
day4.cpp
File metadata and controls
91 lines (80 loc) · 2.33 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
#include "day4.hpp"
#include <cstddef>
#include <utility>
#include <vector>
namespace Day4 {
int solve_day4_pt1(const vector<string> &input) {
int result = 0;
for (int row = 0; row < input.size(); row++) {
for (int column = 0; column < input.size(); column++) {
if (input[row][column] != '@') {
continue;
}
int count = 0;
for (int column_shift = -1; column_shift <= 1; column_shift++) {
for (int row_shift = -1; row_shift <= 1; row_shift++) {
if ((column_shift == 0) && (row_shift == 0)) {
continue;
}
const int column_adj = column + column_shift;
const int row_adj = row + row_shift;
if ((column_adj < 0) || (row_adj < 0) ||
(column_adj >= input[0].length()) || (row_adj >= input.size())) {
continue;
}
if (input[row_adj][column_adj] == '@') {
count++;
}
}
}
if (count < 4) {
result++;
}
}
}
return result;
}
vector<pair<size_t, size_t>> find_removable(const vector<string> &input) {
vector<pair<size_t, size_t>> result;
for (int row = 0; row < input.size(); row++) {
for (int column = 0; column < input.size(); column++) {
if (input[row][column] != '@') {
continue;
}
int count = 0;
for (int column_shift = -1; column_shift <= 1; column_shift++) {
for (int row_shift = -1; row_shift <= 1; row_shift++) {
if ((column_shift == 0) && (row_shift == 0)) {
continue;
}
const int column_adj = column + column_shift;
const int row_adj = row + row_shift;
if ((column_adj < 0) || (row_adj < 0) ||
(column_adj >= input[0].length()) || (row_adj >= input.size())) {
continue;
}
if (input[row_adj][column_adj] == '@') {
count++;
}
}
}
if (count < 4) {
result.push_back({row, column});
}
}
}
return result;
}
int solve_day4_pt2(vector<string> &input) {
vector<pair<size_t, size_t>> removable;
int result = 0;
do {
removable = find_removable(input);
result += removable.size();
for (const auto &[row, column] : removable) {
input[row][column] = '.';
}
} while (!removable.empty());
return result;
}
} // namespace Day4