aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day25.cpp
blob: 6d43ddf3c015372fe66cbf3cce9a2c0a260abbdc (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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using pattern_type = vector<string>;

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<vector<pattern_type>, vector<pattern_type>>
read_file(string_view filename)
{
	fstream    input{ filename };
	const auto patterns{ split(string{ istreambuf_iterator<char>{ input }, {} }, "\n\n") };

	vector<pattern_type> keys;
	vector<pattern_type> locks;

	for ( const auto& pattern: patterns ) {
		const pattern_type& lines = split(pattern, "\n");

		if ( lines[0] == "#####" && lines[6] == "....." ) {
			locks.push_back(lines);
		}
		else if ( lines[0] == "....." && lines[6] == "#####" ) {
			keys.push_back(lines);
		}
	}

	return { keys, locks };
}

bool
fits(const pattern_type& key, const pattern_type& lock)
{
	for ( size_t row = 0; row != key.size(); ++row ) {
		for ( size_t col = 0; col != key[row].size(); ++col ) {
			if ( key[row][col] == '#' && lock[row][col] == '#' ) {
				return false;
			}
		}
	}
	return true;
}

void
part1(const tuple<vector<pattern_type>, vector<pattern_type>>& data)
{
	const auto [keys, locks] = data;

	long count = 0;
	for ( const auto& key: keys ) {
		for ( const auto& lock: locks ) {
			if ( fits(key, lock) ) {
				++count;
			}
		}
	}
	cout << count << endl;
}

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