-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
92 lines (79 loc) · 3.22 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
#include "file.h"
#include "utilities.h"
#include <iostream>
#include <vector>
namespace aoc2021_day03 {
int part_1(std::string_view path) {
std::vector<std::string> input = file::readFileAsArrayString(path);
std::vector<int> instances(input[0].size(), 0);
for (const auto& line : input) {
for (int i = 0; i < line.size(); ++i) {
if (line[i] == '1') {
instances[i]++;
}
}
}
std::string bits(input[0].size(), '0');
for (int i = 0; i < input[0].size(); ++i) {
if (instances[i] > input.size() / 2) {
bits[i] = '1';
}
}
return utils::decimalToInt(bits) * utils::decimalToInt(bits, '0');
}
int part_2(std::string_view path) {
std::vector<std::string> input = file::readFileAsArrayString(path);
std::vector<bool> oxygenIndex(input.size(), true);
for (int i = 0; i < input[0].size(); ++i) {
int size = std::count(oxygenIndex.begin(), oxygenIndex.end(), true);
if (size > 1) {
int instances = 0;
for (int j = 0; j < input.size(); ++j) {
if (input[j][i] == '1' && oxygenIndex[j]) {
instances++;
}
}
for (int j = 0; j < oxygenIndex.size(); ++j) {
if (input[j][i] == '1' && instances >= size - instances && oxygenIndex[j]) {
oxygenIndex[j] = true;
} else if (input[j][i] == '0' && instances < size - instances && oxygenIndex[j]) {
oxygenIndex[j] = true;
} else {
oxygenIndex[j] = false;
}
}
}
}
int oxygen = utils::decimalToInt(input[std::find(oxygenIndex.begin(), oxygenIndex.end(), true) - oxygenIndex.begin()]);
std::vector<bool> co2Index(input.size(), true);
for (int i = 0; i < input[0].size(); ++i) {
int size = std::count(co2Index.begin(), co2Index.end(), true);
if (size > 1) {
int instances = 0;
for (int j = 0; j < input.size(); ++j) {
if (input[j][i] == '1' && co2Index[j]) {
instances++;
}
}
for (int j = 0; j < co2Index.size(); ++j) {
if (input[j][i] == '1' && instances < size - instances && co2Index[j]) {
co2Index[j] = true;
} else if (input[j][i] == '0' && instances >= size - instances && co2Index[j]) {
co2Index[j] = true;
} else {
co2Index[j] = false;
}
}
}
}
int co2 = utils::decimalToInt(input[std::find(co2Index.begin(), co2Index.end(), true) - co2Index.begin()]);
return co2 * oxygen;
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2021_day03::part_1("../2021/day03/input.in") << std::endl;
std::cout << "Part 2: " << aoc2021_day03::part_2("../2021/day03/input.in") << std::endl;
return 0;
}
#endif