-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.cpp
More file actions
201 lines (164 loc) · 5.25 KB
/
day13.cpp
File metadata and controls
201 lines (164 loc) · 5.25 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
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
199
200
201
#include "day13.h"
#include "helpers.h"
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
namespace day13
{
long long solvePart1(std::ifstream& file);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 13 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day13_example.txt" : "inputs/day13_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file) << '\n';
file.close();
file.open(fileName);
std::cout << "Part 2 answer: " << solvePart2(file) << '\n';
}
struct Field
{
std::vector<std::vector<bool>> field{};
[[nodiscard]] bool horizontalReflectionIsValid(const size_t nrLeft) const
{
return horizontalReflectionIsSmudgedValid(nrLeft, 0);
}
[[nodiscard]] bool verticalReflectionIsValid(const size_t nrTop) const
{
return verticalReflectionIsSmudgedValid(nrTop, 0);
}
[[nodiscard]] bool horizontalReflectionIsSmudgedValid(const size_t nrLeft, int nrOfSmudges) const
{
const auto width = field[0].size();
const auto height = field.size();
const size_t nrToCompare = std::min(nrLeft, width - nrLeft);
int diffCount{};
for (size_t y = 0; y < height; y++)
{
const std::vector<bool>& row{ field[y] };
for (size_t dx = 0; dx < nrToCompare; dx++)
{
if (row[nrLeft - 1 - dx] != row[nrLeft + dx])
{
if (diffCount > nrOfSmudges)
{
return false;
}
diffCount++;
}
}
}
return diffCount == nrOfSmudges;
}
[[nodiscard]] bool verticalReflectionIsSmudgedValid(const size_t nrTop, int nrOfSmudges) const
{
const auto width = field[0].size();
const auto height = field.size();
const size_t nrToCompare = std::min(nrTop, height - nrTop);
int diffCount{};
for (size_t x = 0; x < width; x++)
{
for (size_t dy = 0; dy < nrToCompare; dy++)
{
if (field[nrTop - 1 - dy][x] != field[nrTop + dy][x])
{
if (diffCount > nrOfSmudges)
{
return false;
}
diffCount++;
}
}
}
return diffCount == nrOfSmudges;
}
};
std::vector<Field> parseInput(std::ifstream& file)
{
std::vector<Field> fields{};
Field field{ std::vector<std::vector<bool>>{} };
while(!file.eof())
{
std::string line;
std::getline(file, line);
if (line.empty())
{
fields.push_back(field);
field = Field{ std::vector<std::vector<bool>>{} };
}
else
{
std::vector<bool> rowInField;
for (char c : line)
{
rowInField.push_back(c == '#');
}
field.field.push_back(rowInField);
}
}
// Add the last field that's not ended by empty line:
fields.push_back(field);
return fields;
}
long long findReflectionScore(const Field& field)
{
for (size_t nrLeft = 1; nrLeft < field.field[0].size(); nrLeft++)
{
if (field.horizontalReflectionIsValid(nrLeft))
{
return static_cast<long long>(nrLeft);
}
}
for (size_t nrTop = 1; nrTop < field.field.size(); nrTop++)
{
if (field.verticalReflectionIsValid(nrTop))
{
return 100LL * static_cast<long long>(nrTop);
}
}
return 0;
}
long long findSmudgedReflectionScore(const Field& field)
{
for (size_t nrLeft = 1; nrLeft < field.field[0].size(); nrLeft++)
{
if (field.horizontalReflectionIsSmudgedValid(nrLeft, 1))
{
return static_cast<long long>(nrLeft);
}
}
for (size_t nrTop = 1; nrTop < field.field.size(); nrTop++)
{
if (field.verticalReflectionIsSmudgedValid(nrTop, 1))
{
return 100LL * static_cast<long long>(nrTop);
}
}
return 0;
}
long long solvePart1(std::ifstream& file)
{
const auto fields = parseInput(file);
long long sum{};
for (auto& field : fields)
{
sum += findReflectionScore(field);
}
return sum;
}
long long solvePart2(std::ifstream& file)
{
const auto fields = parseInput(file);
long long sum{};
for (auto& field : fields)
{
sum += findSmudgedReflectionScore(field);
}
return sum;
}
}