aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day19.cpp
blob: 43995b111e0567f4bd521163c64b7ac4db56e8a1 (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
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

vector<string>
split(string_view line, string_view delimiter)
{
	size_t pos_start = 0;
	size_t pos_end   = 0;

	vector<string> res;

	while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) {
		auto token = line.substr(pos_start, pos_end - pos_start);
		pos_start  = pos_end + delimiter.length();

		res.emplace_back(token);
	}

	res.emplace_back(line.substr(pos_start));
	return res;
}

tuple<set<string>, vector<string>>
read_file(string_view filename)
{
	fstream input{ filename };

	vector<string> lines;
	for ( string line; getline(input, line); ) {
		lines.emplace_back(line);
	}

	const auto parts = split(lines[0], ", ");

	return { { parts.begin(), parts.end() }, { lines.begin() + 2, lines.end() } };
}

void
part1(const tuple<set<string>, vector<string>>& data)
{
	const auto& parts = get<0>(data);
	const auto& lines = get<1>(data);

	map<string, bool> cache;

	function<bool(const string&)> match = [&](const string& line) {
		if ( line.empty() ) {
			return true;
		}

		if ( cache.contains(line) ) {
			return cache.at(line);
		}

		for ( size_t i = 0; i != line.size(); ++i ) {
			if ( parts.contains(line.substr(0, i + 1)) ) {
				if ( match(line.substr(i + 1)) ) {
					return cache[line] = true;
				}
			}
		}

		return cache[line] = false;
	};

	cout << ranges::count_if(lines, match) << endl;
}

void
part2(const tuple<set<string>, vector<string>>& data)
{
	const auto& parts = get<0>(data);
	const auto& lines = get<1>(data);

	map<string, long> cache;

	function<long(const string&)> match = [&](const string& line) -> long {
		if ( line.empty() ) {
			return 1;
		}

		if ( cache.contains(line) ) {
			return cache.at(line);
		}

		long counter = 0;
		for ( size_t i = 0; i != line.size(); ++i ) {
			auto lhs = line.substr(0, i + 1);
			auto rhs = line.substr(i + 1);

			if ( parts.contains(lhs) ) {
				counter += match(rhs);
			}
		}

		return cache[line] = counter;
	};

	cout << accumulate(lines.begin(), lines.end(), 0L, [&](auto init, const auto& line) { return init + match(line); }) << endl;
}

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