aboutsummaryrefslogtreecommitdiff
path: root/2025
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-12-10 15:12:37 +0100
committerThomas Schmucker <ts@its1.de>2025-12-10 15:12:37 +0100
commit3f7af92f6ddb727312530783f10d67cbd8525548 (patch)
treef6fc8fa3124bd47074d2ba6f3e887f426bc76ff5 /2025
parent3483f7d1806e63eb6ee7723702a8065106f651f6 (diff)
downloadadvent-of-code-3f7af92f6ddb727312530783f10d67cbd8525548.tar.gz
advent-of-code-3f7af92f6ddb727312530783f10d67cbd8525548.tar.bz2
advent-of-code-3f7af92f6ddb727312530783f10d67cbd8525548.zip
aoc 2025, day 10, part 1
Diffstat (limited to '2025')
-rw-r--r--2025/src/day10.cpp180
1 files changed, 180 insertions, 0 deletions
diff --git a/2025/src/day10.cpp b/2025/src/day10.cpp
new file mode 100644
index 0000000..f8e903d
--- /dev/null
+++ b/2025/src/day10.cpp
@@ -0,0 +1,180 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <sstream>
5#include <string>
6#include <vector>
7
8using namespace std;
9
10namespace {
11
12class Machine {
13public:
14 Machine(unsigned long dest, const vector<unsigned long>& buttons)
15 : dest_{ dest }
16 , buttons_{ buttons }
17 {
18 }
19
20 void push(size_t button)
21 {
22 curr_ ^= buttons_.at(button);
23 ++pushes_;
24 }
25
26 [[nodiscard]]
27 bool test() const
28 {
29 return curr_ == dest_;
30 }
31
32 void reset()
33 {
34 curr_ = 0;
35 pushes_ = 0;
36 }
37
38 [[nodiscard]]
39 unsigned long button_count() const
40 {
41 return buttons_.size();
42 }
43
44 [[nodiscard]]
45 unsigned long get_pushes() const
46 {
47 return pushes_;
48 }
49
50private:
51 unsigned long dest_;
52 unsigned long curr_{ 0 };
53 unsigned long pushes_{ 0 };
54
55 vector<unsigned long> buttons_;
56};
57
58vector<string>
59split(const string& str, const string& delims)
60{
61 vector<string> result;
62
63 size_t start = str.find_first_not_of(delims);
64 while ( start != string::npos ) {
65 size_t end = str.find_first_of(delims, start);
66
67 if ( end == string::npos ) {
68 // letzter Teil
69 result.push_back(str.substr(start));
70 break;
71 }
72
73 result.push_back(str.substr(start, end - start));
74
75 start = str.find_first_not_of(delims, end);
76 }
77
78 return result;
79}
80
81vector<unsigned long>
82split(const string& str)
83{
84 stringstream strm{ str };
85 vector<unsigned long> values;
86
87 for ( string part; getline(strm, part, ','); ) {
88 values.emplace_back(stoul(part));
89 }
90
91 return values;
92}
93
94unsigned long
95from_pattern(string pattern)
96{
97 unsigned long value = 0;
98 ranges::reverse(pattern);
99 for ( const char chr: pattern ) {
100 value <<= 1UL;
101 value |= (chr == '#') ? 1U : 0U;
102 }
103 return value;
104}
105
106unsigned long
107from_button_string(const string& pattern)
108{
109 unsigned long value = 0;
110 for ( const auto pos: split(pattern) ) {
111 value |= (1UL << pos);
112 }
113 return value;
114}
115
116vector<Machine>
117read_file(const filesystem::path& filename)
118{
119 ifstream file{ filename };
120
121 vector<Machine> machines;
122
123 for ( string line; getline(file, line); ) {
124 const auto parts = split(line, "[] (){}");
125
126 const auto dest = from_pattern(parts[0]);
127
128 vector<unsigned long> buttons;
129 for ( size_t i = 1; i < parts.size() - 1; ++i ) {
130 buttons.emplace_back(from_button_string(parts[i]));
131 }
132
133 machines.emplace_back(dest, buttons);
134 }
135
136 return machines;
137}
138
139unsigned long
140simulate(Machine machine)
141{
142 auto min_so_far = numeric_limits<unsigned long>::max();
143
144 const auto button_count = machine.button_count();
145
146 for ( unsigned long i = 0; i != (1UL << button_count); ++i ) {
147 machine.reset();
148
149 for ( unsigned long button = 0; button != button_count; ++button ) {
150 if ( (i & (1UL << button)) != 0 ) {
151 machine.push(button);
152 }
153 }
154
155 if ( machine.test() ) {
156 min_so_far = min(min_so_far, machine.get_pushes());
157 }
158 }
159
160 return min_so_far;
161}
162
163void
164part1(const vector<Machine>& machines)
165{
166 unsigned long count = 0;
167 for ( const auto& machine: machines ) {
168 count += simulate(machine);
169 }
170 cout << "Part 1: " << count << '\n';
171}
172
173} // namespace
174
175int
176main()
177{
178 auto machines = read_file("data/day10.txt");
179 part1(machines);
180}