-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.h
42 lines (39 loc) · 1.17 KB
/
day16.h
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
#include <algorithm>
#include <fstream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
namespace {
using namespace std;
string part1(ifstream input) {
string signal;
input >> signal;
for (int phase = 0; phase < 100; phase++)
for (auto i = 0; i < signal.size(); i++) {
int output = 0;
for (auto j = 0; j < signal.size(); j++) {
int pattern[]{ 0, 1, 0, -1 };
output += pattern[(j + 1) / (i + 1) % size(pattern)] * (signal[j] - '0');
}
signal[i] = (abs(output) % 10) + '0';
}
return signal.substr(0, 8);
}
string part2(ifstream input) {
string signal;
input >> signal;
vector<int> slice;
for (auto offset = stoi(signal.substr(0, 7)); offset < signal.length() * 10000; offset++)
slice.push_back(signal[offset % signal.length()]);
for (int phase = 0; phase < 100; phase++)
inclusive_scan(rbegin(slice), rend(slice), rbegin(slice), [](auto a, auto b) {
return abs(a + b) % 10;
});