about summary refs log tree commit diff
path: root/day10.cc
blob: c2604873f8dc3188d399843f57b044a74424bded (plain) (blame)
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
#include <algorithm>
#include <iomanip>
#include <iterator>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

struct CommaInts : string {
	friend istream& operator>>(istream& is, CommaInts& line) {
		return getline(is, line, ',');
	}
	operator int() const { return stoi(*this); }
};
auto begin(istream_iterator<CommaInts>& l) { return l; }
auto end(istream_iterator<CommaInts>&) { return istream_iterator<CommaInts>{}; }

template <typename T>
auto knothash(T iter, int n)
{
	vector<int> v(256);
	iota(begin(v), end(v), 0);

	uint8_t skip = 0, pos = 0, delta;

	for (int j = 0; j < n; j++)
		for (auto i : iter) {
			reverse(begin(v), begin(v) + i);
			delta = i + skip++;
			rotate(begin(v), begin(v) + delta, end(v));
			pos += delta;
		}
	rotate(begin(v), end(v) - pos, end(v));

	return move(v);
}

int
main() {
	string line;
	cin >> line;

	istringstream iss{line};
	vector<int> v1{knothash(istream_iterator<CommaInts>(iss), 1)};
	cout << v1[0] * v1[1] << endl;

	line.append({17,31,73,47,23});
	vector<int> v2{knothash(line, 64)};
	for (auto b = begin(v2); b != end(v2); advance(b, 16))
		cout << setfill('0') << setw(2) << hex <<
		    accumulate(b, b + 16, 0, bit_xor<uint8_t>());
	cout << endl;
}