-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
146 lines (132 loc) · 4.52 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "file.h"
#include "utilities.h"
#include <iostream>
#include <vector>
#include <numeric>
#include <map>
namespace aoc2021_day16 {
std::map<char, std::string> mask {{'0', "0000"}, {'1', "0001"}, {'2', "0010"}, {'3', "0011"},
{'4', "0100"}, {'5', "0101"}, {'6', "0110"}, {'7', "0111"},
{'8', "1000"}, {'9', "1001"}, {'A', "1010"}, {'B', "1011"},
{'C', "1100"}, {'D', "1101"}, {'E', "1110"}, {'F', "1111"}};
unsigned long long binaryToInt(std::string_view bits, int start, int size) {
if (start + size < bits.size()) {
return utils::decimalToInt(bits.substr(start, size));
}
return 0;
}
unsigned long long calculateSumVersion(std::string_view bits, int& index) {
unsigned long long versionSum = binaryToInt(bits, index, 3);
index += 3;
unsigned long long type = binaryToInt(bits, index, 3);
index += 3;
if (type == 4) {
while (bits[index] == '1') {
index += 5;
}
index += 5;
return versionSum;
}
if (bits[index] == '0') {
index++;
unsigned long long length = binaryToInt(bits, index, 15);
index += 15;
int start = index;
while (index < start + length) {
versionSum += calculateSumVersion(bits, index);
}
}
else {
index++;
unsigned long long nr = binaryToInt(bits, index, 11);
index += 11;
for (int i = 0; i < nr; ++i) {
versionSum += calculateSumVersion(bits, index);
}
}
return versionSum;
}
unsigned long long calculateSumValue(std::string_view bits, int& index) {
std::string str;
index += 3;
unsigned long long typeValue = binaryToInt(bits, index, 3);
index += 3;
if (typeValue == 4) {
while (index < bits.size() && bits[index] == '1') {
str += bits.substr(index + 1, 4);
index += 5;
}
if (index < bits.size()) {
str += bits.substr(index + 1, 4);
index += 5;
}
return utils::decimalToInt(str);
}
std::vector<unsigned long long> values;
if (bits[index] == '0') {
index++;
unsigned long long length = binaryToInt(bits, index, 15);
index += 15;
int start = index;
while (index < start + length) {
values.push_back(calculateSumValue(bits, index));
}
}
else {
index++;
unsigned long long nr = binaryToInt(bits, index, 11);
index += 11;
for (int i = 0; i < nr; ++i) {
values.push_back(calculateSumValue(bits, index));
}
}
if (typeValue == 0) {
return std::accumulate(values.begin(), values.end(), 0ULL);
}
if (typeValue == 1) {
return std::accumulate(values.begin(), values.end(), 1ULL, [](const auto& a, const auto&b) {
return a * b;
});
}
if (typeValue == 2) {
return *std::min_element(values.begin(), values.end());
}
if (typeValue == 3) {
return *std::max_element(values.begin(), values.end());
}
if (typeValue == 5) {
return values[0] > values[1];
}
if (typeValue == 6) {
return values[0] < values[1];
}
if (typeValue == 7) {
return values[0] == values[1];
}
}
unsigned long long part_1(std::string_view path) {
std::string input = file::readFileAsString(path);
std::string bits;
for (const auto& letter : input) {
bits += mask[letter];
}
int index = 0;
return calculateSumVersion(bits, index);
}
unsigned long long part_2(std::string_view path) {
std::string input = file::readFileAsString(path);
std::string bits;
for (const auto& letter : input) {
bits += mask[letter];
}
int index = 0;
return calculateSumValue(bits, index);
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2021_day16::part_1("../2021/day16/input.in") << std::endl;
std::cout << "Part 2: " << aoc2021_day16::part_2("../2021/day16/input.in") << std::endl;
return 0;
}
#endif