aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day22.cpp
blob: 7fdc40ee3737d9152d38daedd92dfdeb8e444f0a (plain)
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
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

vector<unsigned long>
read_file(string_view filename)
{
	fstream               input{ filename };
	vector<unsigned long> lines;

	for ( unsigned long value{}; input >> value; ) {
		lines.emplace_back(value);
	}

	return lines;
}

unsigned long
step(unsigned long value)
{
	// compiler will optimize these calculations
	value = (value ^ (value * 64)) % 16777216;
	value = (value ^ (value / 32)) % 16777216;
	value = (value ^ (value * 2048)) % 16777216;
	return value;
}

void
part1(const vector<unsigned long>& values)
{
	unsigned long sum = 0;
	for ( auto value: values ) {
		for ( int i = 0; i != 2000; ++i ) {
			value = step(value);
		}
		sum += value;
	}
	cout << sum << endl;
}

void
part2(const vector<unsigned long>& values)
{
	using seq_type = tuple<unsigned long, unsigned long, unsigned long, unsigned long>;

	map<seq_type, unsigned long> total;

	for ( auto value: values ) {
		vector<unsigned long> list{ value % 10 };

		for ( int i = 0; i != 2000; ++i ) {
			value = step(value);
			list.emplace_back(value % 10);
		}

		set<seq_type> seen;

		for ( size_t idx = 4; idx < list.size(); ++idx ) {
			const auto a = list[idx - 4];
			const auto b = list[idx - 3];
			const auto c = list[idx - 2];
			const auto d = list[idx - 1];
			const auto e = list[idx];

			seq_type seq{ b - a, c - b, d - c, e - d };

			if ( seen.contains(seq) ) {
				continue;
			}
			seen.insert(seq);

			total[seq] += e;
		}
	}

	cout << ranges::max_element(total, [](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; })->second << endl;
}

int
main()
{
	auto data = read_file("data/day22.txt");
	part1(data);
	part2(data);
}