-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_15a.cpp
36 lines (35 loc) · 1.15 KB
/
day_15a.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
#include <fstream>
#include <iostream>
#include <unordered_map>
int main() {
std::fstream file{"../input/day_15_input"};
std::string line;
std::getline(file, line);
const std::string delimiter = ",";
size_t start = 0;
std::size_t end = line.find(delimiter);
std::unordered_map<int, int> spoken_numbers;
while (end != std::string::npos) {
spoken_numbers.insert(
{std::stoi(line.substr(start, end - start)), spoken_numbers.size()});
start = end + delimiter.size();
end = line.find(delimiter, start);
}
// spoken_numbers.insert({std::stoi(line.substr(start, end - start)),
// spoken_numbers.size()}); std::cout << std::stoi(line.substr(start, end -
// start)) << '\n';
long long new_number = std::stoi(line.substr(start, end - start));
size_t count = spoken_numbers.size();
while (count < 2019) {
if (auto it = spoken_numbers.find(new_number); it != spoken_numbers.end()) {
new_number = count - spoken_numbers[new_number];
it->second = count;
} else {
spoken_numbers.insert({new_number, count});
new_number = 0;
}
count++;
}
std::cout << new_number << '\n';
return new_number;
}