aboutsummaryrefslogtreecommitdiff
path: root/2025/src/day10.cpp
blob: f8e903de883f792ab44a8a4b08837b9530aa06db (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

namespace {

class Machine {
public:
	Machine(unsigned long dest, const vector<unsigned long>& buttons)
	    : dest_{ dest }
	    , buttons_{ buttons }
	{
	}

	void push(size_t button)
	{
		curr_ ^= buttons_.at(button);
		++pushes_;
	}

	[[nodiscard]]
	bool test() const
	{
		return curr_ == dest_;
	}

	void reset()
	{
		curr_   = 0;
		pushes_ = 0;
	}

	[[nodiscard]]
	unsigned long button_count() const
	{
		return buttons_.size();
	}

	[[nodiscard]]
	unsigned long get_pushes() const
	{
		return pushes_;
	}

private:
	unsigned long dest_;
	unsigned long curr_{ 0 };
	unsigned long pushes_{ 0 };

	vector<unsigned long> buttons_;
};

vector<string>
split(const string& str, const string& delims)
{
	vector<string> result;

	size_t start = str.find_first_not_of(delims);
	while ( start != string::npos ) {
		size_t end = str.find_first_of(delims, start);

		if ( end == string::npos ) {
			// letzter Teil
			result.push_back(str.substr(start));
			break;
		}

		result.push_back(str.substr(start, end - start));

		start = str.find_first_not_of(delims, end);
	}

	return result;
}

vector<unsigned long>
split(const string& str)
{
	stringstream          strm{ str };
	vector<unsigned long> values;

	for ( string part; getline(strm, part, ','); ) {
		values.emplace_back(stoul(part));
	}

	return values;
}

unsigned long
from_pattern(string pattern)
{
	unsigned long value = 0;
	ranges::reverse(pattern);
	for ( const char chr: pattern ) {
		value <<= 1UL;
		value |= (chr == '#') ? 1U : 0U;
	}
	return value;
}

unsigned long
from_button_string(const string& pattern)
{
	unsigned long value = 0;
	for ( const auto pos: split(pattern) ) {
		value |= (1UL << pos);
	}
	return value;
}

vector<Machine>
read_file(const filesystem::path& filename)
{
	ifstream file{ filename };

	vector<Machine> machines;

	for ( string line; getline(file, line); ) {
		const auto parts = split(line, "[] (){}");

		const auto dest = from_pattern(parts[0]);

		vector<unsigned long> buttons;
		for ( size_t i = 1; i < parts.size() - 1; ++i ) {
			buttons.emplace_back(from_button_string(parts[i]));
		}

		machines.emplace_back(dest, buttons);
	}

	return machines;
}

unsigned long
simulate(Machine machine)
{
	auto min_so_far = numeric_limits<unsigned long>::max();

	const auto button_count = machine.button_count();

	for ( unsigned long i = 0; i != (1UL << button_count); ++i ) {
		machine.reset();

		for ( unsigned long button = 0; button != button_count; ++button ) {
			if ( (i & (1UL << button)) != 0 ) {
				machine.push(button);
			}
		}

		if ( machine.test() ) {
			min_so_far = min(min_so_far, machine.get_pushes());
		}
	}

	return min_so_far;
}

void
part1(const vector<Machine>& machines)
{
	unsigned long count = 0;
	for ( const auto& machine: machines ) {
		count += simulate(machine);
	}
	cout << "Part 1: " << count << '\n';
}

} // namespace

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