advent-of-code/2024/22/main-2.cpp
2024-12-22 03:17:19 -08:00

56 lines
943 B
C++

#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
size_t h(vector<char> &v) {
size_t ret = 0;
for (int i = 0; i < 4; i++) {
ret += v[v.size() - 1 - i] << (i * 8);
}
return ret;
}
ll mp(ll n, ll o) { return (n ^ o) % 16777216; }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<int> secrets;
ll n;
while (cin >> n) {
secrets.push_back(n);
}
unordered_map<size_t, int> m;
for (ll s : secrets) {
char sp = s % 10;
vector<char> ss;
unordered_set<size_t> done;
for (int i = 0; i < 2000; i++) {
char oldsp = sp;
s = mp(s * 64, s);
s = mp(s / 32, s);
s = mp(s * 2048, s);
sp = s % 10;
ss.push_back(sp - oldsp);
if (ss.size() >= 4) {
auto hss = h(ss);
if (done.contains(hss)) {
continue;
}
done.insert(hss);
m[hss] += sp;
}
}
}
int mx = 0;
for (auto n : m) {
if (n.second > mx) {
mx = n.second;
}
}
cout << mx << '\n';
}