-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.cpp
More file actions
220 lines (175 loc) · 5.51 KB
/
day02.cpp
File metadata and controls
220 lines (175 loc) · 5.51 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "day02.h"
#include <charconv>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
namespace day02
{
// Ugly solutions below, but trying to avoid any type of list/array to 'stick' to progress in learn cpp :D
constexpr int maxRed{ 12 };
constexpr int maxGreen{ 13 };
constexpr int maxBlue{ 14 };
bool part1LineIsValid(const std::string_view line);
int part2LinePower(std::string_view line);
void run_day(bool example)
{
std::cout << "Running day 02 \n";
const std::string fileName{ example ? "inputs/day02_example.txt" : "inputs/day02_real.txt" };
std::ifstream file{ fileName };
int sum{};
int lineId{1};
std::string line;
while (std::getline(file, line))
{
if(part1LineIsValid(line))
{
sum += lineId;
}
lineId++;
}
std::cout << "Part 1 answer: " << sum << '\n';
file.close();
file.open(fileName);
sum = 0;
while (std::getline(file, line))
{
sum += part2LinePower(line);
}
std::cout << "Part 2 answer: " << sum << '\n';
}
bool singleColorRevealFits(std::string_view colorReveal)
{
if (colorReveal[0] == ' ')
{
colorReveal.remove_prefix(1);
}
const size_t spacePos{ colorReveal.find(' ') };
int number;
std::from_chars(colorReveal.data(), colorReveal.data() + colorReveal.size(), number);
if (colorReveal[spacePos + 1] == 'b')
{
return number <= maxBlue;
}
if (colorReveal[spacePos + 1] == 'g')
{
return number <= maxGreen;
}
if (colorReveal[spacePos + 1] == 'r')
{
return number <= maxRed;
}
std::cout << "Found unexpected letter" << colorReveal[spacePos + 1];
throw;
}
bool fullRevealFitsColors(std::string_view reveal)
{
bool valid{ true };
// Loop over seperate color reveals
size_t commaPos;
while ((commaPos = reveal.find(',')) != std::string::npos)
{
const std::string_view colorReveal = reveal.substr(0, commaPos);
valid &= singleColorRevealFits(colorReveal);
reveal.remove_prefix(commaPos + 1);
}
// Handle reveal after last comma
valid &= singleColorRevealFits(reveal);
return valid;
}
bool part1LineIsValid(std::string_view line)
{
bool valid{ true };
// Parse of game id header
line.remove_prefix(line.find(":") + 1);
// Check each reveal for being valid.
size_t semicolonPos;
while ((semicolonPos = line.find(';')) != std::string::npos)
{
valid &= fullRevealFitsColors(line.substr(0, semicolonPos));
line.remove_prefix(semicolonPos + 1);
}
// Handle the last game after semicolons (no ending semicolon):
valid &= fullRevealFitsColors(line);
return valid;
}
void updateMinimaForColorReveal(std::string_view colorReveal, int& minRed, int& minBlue, int& minGreen)
{
if (colorReveal[0] == ' ')
{
colorReveal.remove_prefix(1);
}
const size_t spacePos{ colorReveal.find(' ') };
int number;
std::from_chars(colorReveal.data(), colorReveal.data() + colorReveal.size(), number);
if (colorReveal[spacePos + 1] == 'b')
{
if (number > minBlue)
{
minBlue = number;
}
return;
}
if (colorReveal[spacePos + 1] == 'g')
{
if (number > minGreen)
{
minGreen = number;
}
return;
}
if (colorReveal[spacePos + 1] == 'r')
{
if (number > minRed)
{
minRed = number;
}
return;
}
std::cout << "Found unexpected letter" << colorReveal[spacePos + 1];
throw;
}
void updateMinimaForFullReveal(std::string_view reveal, int& minRed, int& minBlue, int& minGreen)
{
// Loop over seperate color reveals
size_t commaPos;
while ((commaPos = reveal.find(',')) != std::string::npos)
{
const std::string_view colorReveal = reveal.substr(0, commaPos);
updateMinimaForColorReveal(colorReveal,
minRed,
minBlue,
minGreen);
reveal.remove_prefix(commaPos + 1);
}
// Handle reveal after last comma
updateMinimaForColorReveal(reveal,
minRed,
minBlue,
minGreen);
}
int part2LinePower(std::string_view line)
{
int minRed{};
int minBlue{};
int minGreen{};
// Parse of game id header
line.remove_prefix(line.find(":") + 1);
// Check each reveal for updating minima.
size_t semicolonPos;
while ((semicolonPos = line.find(';')) != std::string::npos)
{
updateMinimaForFullReveal(line.substr(0, semicolonPos),
minRed,
minBlue,
minGreen);
line.remove_prefix(semicolonPos + 1);
}
// Handle the last game after semicolons (no ending semicolon):
updateMinimaForFullReveal(line,
minRed,
minBlue,
minGreen);
return minRed * minBlue * minGreen;
}
}