aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
Diffstat (limited to '2024')
-rw-r--r--2024/src/day22.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/2024/src/day22.cpp b/2024/src/day22.cpp
new file mode 100644
index 0000000..b103986
--- /dev/null
+++ b/2024/src/day22.cpp
@@ -0,0 +1,47 @@
1#include <cstdlib>
2#include <fstream>
3#include <iostream>
4#include <vector>
5using namespace std;
6
7vector<unsigned long>
8read_file(string_view filename)
9{
10 fstream input{ filename };
11 vector<unsigned long> lines;
12
13 for ( unsigned long value{}; input >> value; ) {
14 lines.emplace_back(value);
15 }
16
17 return lines;
18}
19
20unsigned long
21step(unsigned long value)
22{
23 value = (value ^ (value * 64)) % 16777216;
24 value = (value ^ (value / 32)) % 16777216;
25 value = (value ^ (value * 2048)) % 16777216;
26 return value;
27}
28
29void
30part1(const vector<unsigned long>& values)
31{
32 unsigned long sum = 0;
33 for ( auto value: values ) {
34 for ( int i = 0; i != 2000; ++i ) {
35 value = step(value);
36 }
37 sum += value;
38 }
39 cout << sum << endl;
40}
41
42int
43main()
44{
45 auto data = read_file("data/day22.txt");
46 part1(data);
47}