-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.cpp
62 lines (47 loc) · 1.26 KB
/
day4.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
#include "TextUtils.hpp"
#include <cmath>
#include <map>
int solve(const std::string& line)
{
auto split_line = TextUtils::Split(line, ":|");
auto winning_numbers = TextUtils::Split(split_line[1], " ");
auto my_numbers = TextUtils::Split(split_line[2], " ");
int exponent = 0;
for (auto winning_number : winning_numbers)
{
for (auto my_number : my_numbers)
{
if (my_number != "" && winning_number != "" && my_number == winning_number)
{
exponent += 1;
}
}
}
return exponent;
}
int main(int p_argv, char** p_argc)
{
std::fstream input_file;
input_file.open("input_day4.txt", std::ios::in);
std::cout << std::boolalpha;
std::cout << "Day 4, lets go!" << std::endl;
std::string line;
int resultPart1 = 0;
int resultPart2 = 0;
int line_index = 0;
std::map<int, int> numberOfCopies;
while (std::getline(input_file, line))
{
int match = solve(line);
resultPart1 += (match > 0) ? pow(2, match - 1) : 0;
numberOfCopies[line_index] += 1;
resultPart2 += numberOfCopies[line_index];
for (int i = line_index + 1; i <= line_index + match; i++)
{
numberOfCopies[i] += numberOfCopies[line_index];
}
line_index++;
}
std::cout << "Part 1: " << resultPart1 << std::endl;
std::cout << "Part 2: " << resultPart2 << std::endl;
}