aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day25.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-01-02 14:08:05 +0100
committerThomas Schmucker <ts@its1.de>2025-01-02 14:08:05 +0100
commit9098c9d0d9922f54b3ad9414151604402b8e9c44 (patch)
treed30acf9a4b755ab449c274112a696ddec4334b40 /2024/src/day25.cpp
parent870b007c75a3d3c37e89aa4074c2574ac6b5dd81 (diff)
downloadadvent-of-code-9098c9d0d9922f54b3ad9414151604402b8e9c44.tar.gz
advent-of-code-9098c9d0d9922f54b3ad9414151604402b8e9c44.tar.bz2
advent-of-code-9098c9d0d9922f54b3ad9414151604402b8e9c44.zip
aoc 2024, day 24, part 2 and day 25
Diffstat (limited to '2024/src/day25.cpp')
-rw-r--r--2024/src/day25.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/2024/src/day25.cpp b/2024/src/day25.cpp
new file mode 100644
index 0000000..6d43ddf
--- /dev/null
+++ b/2024/src/day25.cpp
@@ -0,0 +1,87 @@
1#include <fstream>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <tuple>
6#include <vector>
7using namespace std;
8
9using pattern_type = vector<string>;
10
11vector<string>
12split(string_view line, string_view delimiter)
13{
14 size_t pos_start = 0;
15 size_t pos_end = 0;
16
17 vector<string> res;
18
19 while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) {
20 auto token = line.substr(pos_start, pos_end - pos_start);
21 pos_start = pos_end + delimiter.length();
22
23 res.emplace_back(token);
24 }
25
26 res.emplace_back(line.substr(pos_start));
27 return res;
28}
29
30tuple<vector<pattern_type>, vector<pattern_type>>
31read_file(string_view filename)
32{
33 fstream input{ filename };
34 const auto patterns{ split(string{ istreambuf_iterator<char>{ input }, {} }, "\n\n") };
35
36 vector<pattern_type> keys;
37 vector<pattern_type> locks;
38
39 for ( const auto& pattern: patterns ) {
40 const pattern_type& lines = split(pattern, "\n");
41
42 if ( lines[0] == "#####" && lines[6] == "....." ) {
43 locks.push_back(lines);
44 }
45 else if ( lines[0] == "....." && lines[6] == "#####" ) {
46 keys.push_back(lines);
47 }
48 }
49
50 return { keys, locks };
51}
52
53bool
54fits(const pattern_type& key, const pattern_type& lock)
55{
56 for ( size_t row = 0; row != key.size(); ++row ) {
57 for ( size_t col = 0; col != key[row].size(); ++col ) {
58 if ( key[row][col] == '#' && lock[row][col] == '#' ) {
59 return false;
60 }
61 }
62 }
63 return true;
64}
65
66void
67part1(const tuple<vector<pattern_type>, vector<pattern_type>>& data)
68{
69 const auto [keys, locks] = data;
70
71 long count = 0;
72 for ( const auto& key: keys ) {
73 for ( const auto& lock: locks ) {
74 if ( fits(key, lock) ) {
75 ++count;
76 }
77 }
78 }
79 cout << count << endl;
80}
81
82int
83main()
84{
85 const auto data = read_file("data/day25.txt");
86 part1(data);
87}