parent
676006891d
commit
3f65baeccb
1 changed files with 72 additions and 2 deletions
@ -1,9 +1,79 @@ |
||||
#include "Day01.h" |
||||
|
||||
Result Day01::Task1() { |
||||
return Day::Task1(); |
||||
uint32_t sum = 0; |
||||
for (const string& line : input){ |
||||
uint8_t left = *std::find_if(line.begin(), line.end(), [](const char c){ |
||||
int diff = c - '0'; |
||||
return diff >= 0 && diff < 10; |
||||
}) - '0'; |
||||
uint8_t right = *std::find_if(std::make_reverse_iterator(line.end()), std::make_reverse_iterator(line.begin()), [](const char c){ |
||||
int diff = c - '0'; |
||||
return diff >= 0 && diff < 10; |
||||
}) - '0'; |
||||
sum += left * 10 + right; |
||||
} |
||||
return to_string(sum); |
||||
} |
||||
|
||||
Result Day01::Task2() { |
||||
return Day::Task2(); |
||||
uint32_t sum = 0; |
||||
|
||||
std::map<string, char> convert = { |
||||
{"one", '1'}, |
||||
{"two", '2'}, |
||||
{"three", '3'}, |
||||
{"four", '4'}, |
||||
{"five", '5'}, |
||||
{"six", '6'}, |
||||
{"seven", '7'}, |
||||
{"eight", '8'}, |
||||
{"nine", '9'}, |
||||
}; |
||||
|
||||
for (const string &line : input){ |
||||
|
||||
uint8_t left = 0, right = 0; |
||||
for (size_t i = 0; i < line.length(); i++){ |
||||
auto current = line.substr(0, i + 1); |
||||
auto diff = current.back() - '0'; |
||||
if (diff > 0 && diff < 10){ |
||||
left = diff; |
||||
break; |
||||
} |
||||
bool found = false; |
||||
for (const auto& [key, value] : convert){ |
||||
auto pos = current.find(key); |
||||
if (pos != string::npos){ |
||||
left = value - '0'; |
||||
found = true; |
||||
break; |
||||
} |
||||
} |
||||
if (found) break; |
||||
} |
||||
|
||||
for (size_t i = 0; i < line.length(); i++){ |
||||
auto current = line.substr(line.length() - i - 1); |
||||
auto diff = current.front() - '0'; |
||||
if (diff > 0 && diff < 10){ |
||||
right = diff; |
||||
break; |
||||
} |
||||
bool found = false; |
||||
for (const auto& [key, value] : convert){ |
||||
auto pos = current.find(key); |
||||
if (pos != string::npos){ |
||||
right = value - '0'; |
||||
found = true; |
||||
break; |
||||
} |
||||
} |
||||
if (found) break; |
||||
} |
||||
|
||||
sum += left * 10 + right; |
||||
} |
||||
|
||||
return to_string(sum); |
||||
} |
Loading…
Reference in new issue