-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.cpp
63 lines (47 loc) · 1.72 KB
/
Day3.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
#include "Day3.h"
void Day3::part1()
{
vector<string> data;
Utils::read_lines_from_file("../data/day3.txt", data);
int gama = 0;
int epsilon = 0;
for (int i = 0; i < data[0].size(); i++)
{
int* counter = new int[] {0, 0};
for (string row : data)
counter[row.at(i) - '0']++;
gama = (gama << 1) + (counter[0] > counter[1] ? 0 : 1);
epsilon = (epsilon << 1) + (counter[0] > counter[1] ? 1 : 0);
}
cout << "Day: 3 | Part: 1 | Result: " << gama * epsilon << endl;
}
void Day3::part2()
{
vector<string> data_og;
Utils::read_lines_from_file("../data/day3.txt", data_og);
vector<string> data_co2s(data_og);
for (int i = 0; i < data_og[0].size(); i++)
{
if (data_og.size() > 1) {
int* counter = new int[] {0, 0};
for (string row : data_og)
counter[row.at(i) - '0']++;
data_og.erase(
remove_if(data_og.begin(), data_og.end(), [i, counter](string row) { return row.at(i) == (counter[1] >= counter[0] ? '1' : '0'); }),
data_og.end()
);
}
if (data_co2s.size() > 1) {
int* counter = new int[] {0, 0};
for (string row : data_co2s)
counter[row.at(i) - '0']++;
data_co2s.erase(
remove_if(data_co2s.begin(), data_co2s.end(), [i, counter](string row) { return row.at(i) == (counter[1] >= counter[0] ? '0' : '1'); }),
data_co2s.end()
);
}
if (data_og.size() == 1 && data_co2s.size() == 1)
break;
}
cout << "Day: 3 | Part: 2 | Result: " << stoi(data_og[0], nullptr, 2) * stoi(data_co2s[0], nullptr, 2) << endl;
}