-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
109 lines (99 loc) · 2.39 KB
/
main.cpp
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
#include "../Utils/Utils.h"
auto Part1(const std::vector<std::string>& input)
{
Utils::Timer timer("Part 1");
int result = 0;
for (std::string line : input)
{
line = std::regex_replace(line, std::regex(R"([\D])"), "");
result += 10 * (line.front() - '0');
result += line.back() - '0';
}
return result;
}
auto Part2(const std::vector<std::string>& input)
{
Utils::Timer timer("Part 2");
int result = 0;
std::array<std::string, 10> numbers{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
for (std::string line : input)
{
std::cout << "Line: " << line << "\n";
for (int i = 0; i < line.size(); i++)
{
if (isdigit(line[i]))
{
result += 10 * (line[i] - '0');
std::cout << "Found number: " << line[i] - '0' << "\n";
break;
}
else
{
bool found = false;
for (int j = 0; j < numbers.size(); j++)
{
if (line.substr(i, numbers[j].size()) == numbers[j])
{
result += 10 * j;
std::cout << "Found word: " << numbers[j] << "\n";
found = true;
break;
}
}
if (found)
break;
}
}
// Now in reverse
for (int i = line.size(); i >= 0; i--)
{
if (isdigit(line[i]))
{
result += (line[i] - '0');
std::cout << "Found reverse number: " << line[i] - '0' << "\n";
break;
}
else
{
bool found = false;
for (int j = 0; j < numbers.size(); j++)
{
if (line.substr(i, numbers[j].size()) == numbers[j]) // Could improve by looking in reverse. Right now we're going in reverse for each char in the line, but looking forwards for the word.
{
result += j;
std::cout << "Found reverse word: " << numbers[j] << "\n";
found = true;
break;
}
}
if (found)
break;
}
}
}
return result;
}
int main(int argc, char** argv)
{
Utils::Timer timer("Day 01");
try
{
#ifdef INPUT_TESTING
std::vector<std::string> part1Input = Utils::ReadFile("PuzzleTestInput1.txt");
std::vector<std::string> part2Input = Utils::ReadFile("PuzzleTestInput2.txt");
#else
std::vector<std::string> part1Input = Utils::ReadFile("PuzzleInput.txt");
std::vector<std::string> part2Input = part1Input;
#endif
auto result1 = Part1(part1Input);
Utils::CheckResult(result1, 142);
auto result2 = Part2(part2Input);
Utils::CheckResult(result2, 281);
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}
return 0;
}