aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

day19a.cpp (1190B)


      1 #include <algorithm>
      2 #include <cstdint>
      3 #include <iostream>
      4 #include <map>
      5 #include <queue>
      6 #include <ranges>
      7 #include <set>
      8 #include <sstream>
      9 #include <string>
     10 #include <string_view>
     11 #include <vector>
     12 using namespace std;
     13 
     14 vector<string> gettowels(const string& line) {
     15 	vector<string> ret;
     16 	int j = 0;
     17 	for (int i = 0; j != -1; i = j+2) {
     18 		j = line.find(",", i);
     19 		ret.push_back(line.substr(i, (j == -1 ? line.size() : j)-i));
     20 	}
     21 	return ret;
     22 }
     23 
     24 bool ispossible(const string& p, const vector<string>& towels, vector<int>& mem) {
     25 	if (p.size() == 0)
     26 		return true;
     27 	if (mem[p.size()] != -1)
     28 		return mem[p.size()];
     29 	for (auto t : towels) {
     30 		if (t == p.substr(0, t.size()))
     31 			if((mem[p.size()] = ispossible(p.substr(
     32 			    t.size(), p.size() - t.size()), towels, mem)))
     33 				return true;
     34 	}
     35 	return (mem[p.size()] = false);
     36 }
     37 
     38 int main() {
     39 	string line;
     40 	getline(cin, line);
     41 	auto towels = gettowels(line);
     42 	getline(cin, line);
     43 	vector<string> patterns;
     44 	while (getline(cin, line))
     45 		patterns.push_back(line);
     46 	vector<int> mem(1000);
     47 	int count = 0;
     48 	for (auto p : patterns) {
     49 		for (auto& m : mem) m = -1;
     50 		count += ispossible(p, towels, mem);
     51 	}
     52 	cout << count << endl;
     53 	return 0;
     54 }