1
0

Compare commits

..

7 Commits

Author SHA1 Message Date
Marcus Penate 14453cba1f Day 14 2022-12-07 09:10:20 -05:00
Marcus Penate aa4a1076a2 Day 13 2022-12-07 09:09:48 -05:00
Marcus Penate c432d3f381 Day 12 2022-12-07 09:07:06 -05:00
Marcus Penate 426e2f5001 Day 11 2022-12-07 09:06:57 -05:00
MPenate0 27cedade38 Day 10 submission 2021-12-10 00:31:06 -05:00
MPenate0 dfcc3b4f76 Day 9 submission 2021-12-09 01:11:44 -05:00
MPenate0 533aae2f1c Day 5-8 submissions 2021-12-08 22:14:58 -05:00
26 changed files with 1793 additions and 171 deletions
+2
View File
@@ -1 +1,3 @@
.vscode .vscode
*/*
!**/*.cpp
+43
View File
@@ -0,0 +1,43 @@
#include <iostream>
#include <stack>
using namespace std;
int main(int argc, char* argv[])
{
string str_in;
string openers = "([{<";
string closers = ")]}>";
int result = 0;
int scores[4] = {3,57,1197,25137};
while(cin >> str_in)
{
stack<char> parser;
for(unsigned int i = 0; i < str_in.size(); i++)
{
size_t found_pos;
if (openers.find(str_in[i])!=string::npos)
{
parser.push(str_in[i]);
}
else if ((found_pos=closers.find(str_in[i]))!=string::npos)
{
if (parser.top() != openers[found_pos])
{
result += scores[found_pos];
break;
}
else
{
parser.pop();
}
}
}
}
cout << result << endl;
return 0;
}
+62
View File
@@ -0,0 +1,62 @@
#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
string str_in;
string openers = "([{<";
string closers = ")]}>";
vector<unsigned long long> all_results;
while(cin >> str_in)
{
stack<char> parser;
unsigned long long result = 0;
bool bl_is_corrupted = false;
for(unsigned int i = 0; i < str_in.size(); i++)
{
size_t found_pos;
if (openers.find(str_in[i])!=string::npos)
{
parser.push(str_in[i]);
}
else if ((found_pos=closers.find(str_in[i]))!=string::npos)
{
if (parser.top() != openers[found_pos])
{
bl_is_corrupted = true;
break;
}
else
{
parser.pop();
}
}
}
if (bl_is_corrupted)
{
continue;
}
while(!parser.empty())
{
result*=5;
result+=1+openers.find(parser.top());
parser.pop();
}
all_results.push_back(result);
}
sort(all_results.begin(),all_results.end());
cout << all_results[all_results.size()/2] << endl;
return 0;
}
+112
View File
@@ -0,0 +1,112 @@
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
constexpr int grid_size{10};
int grid[grid_size][grid_size] = {0};
void output_grid();
std::ostream& bold_on(std::ostream& os);
std::ostream& bold_off(std::ostream& os);
int main(int argc, char* argv[])
{
string line = "";
for (int y = 0; y < grid_size; y++)
{
getline(cin, line);
for (int x = 0; x < grid_size; x++)
{
grid[x][y] = (int)(line[x]-'0');
}
}
int total_flashes = 0;
for (int t = 0; t < 100; t++)
{
//Step 1, increment
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
++grid[x][y];
}
}
//Step 2, flash
bool flashed[grid_size][grid_size] = {false};
bool flash_occurred = false;
do
{
flash_occurred = false;
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
if (!flashed[x][y] && grid[x][y] > 9)
{
flashed[x][y] = true;
flash_occurred = true;
++total_flashes;
for(int fy = max(0,y-1); fy < min(grid_size, y+2); fy++)
{
for(int fx = max(0,x-1); fx < min(grid_size,x+2); fx++)
{
grid[fx][fy]++;
}
}
}
}
}
} while (flash_occurred);
//Step 3, flashed are cleared
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
if (flashed[x][y])
{
grid[x][y] = 0;
}
}
}
}
cout << total_flashes << endl;
}
void output_grid()
{
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
if (0 == grid[x][y])
{
cout << bold_on << grid[x][y] << bold_off;
}
else
{
cout << grid[x][y];
}
}
cout << endl;
}
cout << endl;
}
std::ostream& bold_on(std::ostream& os)
{
return os << "\e[1m";
}
std::ostream& bold_off(std::ostream& os)
{
return os << "\e[0m";
}
+119
View File
@@ -0,0 +1,119 @@
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
constexpr int grid_size{10};
int grid[grid_size][grid_size] = {0};
void output_grid();
std::ostream& bold_on(std::ostream& os);
std::ostream& bold_off(std::ostream& os);
int main(int argc, char* argv[])
{
string line = "";
for (int y = 0; y < grid_size; y++)
{
getline(cin, line);
for (int x = 0; x < grid_size; x++)
{
grid[x][y] = (int)(line[x]-'0');
}
}
int t = 0;
bool all_flashed = false;
while(!all_flashed)
{
all_flashed = true;
//Step 1, increment
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
++grid[x][y];
}
}
//Step 2, flash
bool flashed[grid_size][grid_size] = {false};
bool flash_occurred = false;
do
{
flash_occurred = false;
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
if (!flashed[x][y] && grid[x][y] > 9)
{
flashed[x][y] = true;
flash_occurred = true;
for(int fy = max(0,y-1); fy < min(grid_size, y+2); fy++)
{
for(int fx = max(0,x-1); fx < min(grid_size,x+2); fx++)
{
grid[fx][fy]++;
}
}
}
}
}
} while (flash_occurred);
//Step 3, flashed are cleared
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
if (flashed[x][y])
{
grid[x][y] = 0;
}
else
{
all_flashed = false;
}
}
}
t++;
}
cout << t << endl;
}
void output_grid()
{
for (int y = 0; y < grid_size; y++)
{
for (int x = 0; x < grid_size; x++)
{
if (0 == grid[x][y])
{
cout << bold_on << grid[x][y] << bold_off;
}
else
{
cout << grid[x][y];
}
}
cout << endl;
}
cout << endl;
}
std::ostream& bold_on(std::ostream& os)
{
return os << "\e[1m";
}
std::ostream& bold_off(std::ostream& os)
{
return os << "\e[0m";
}
+72
View File
@@ -0,0 +1,72 @@
#include <iostream>
#include <cstring>
#include <string>
#include <unordered_map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
string start = "start";
string finish = "end";
string lowercase = "qwertyuiopasdfghjklzxcvbnm";
int find_all_paths(unordered_map<string, set<string>> graph, vector<string> current_path);
int main(int argc, char* argv[])
{
unordered_map<string, set<string>> graph;
string line;
while(getline(cin, line))
{
string::size_type n = line.find("-",0);
string node1 = line.substr(0,n);
string node2 = line.substr(n+1);
if (!graph.contains(node1))
{
graph.emplace(node1, set<string>());
}
if (!graph.contains(node2))
{
graph.emplace(node2, set<string>());
}
graph[node1].insert(node2);
graph[node2].insert(node1);
}
vector<string> beginning_path;
beginning_path.push_back(start);
cout << find_all_paths(graph, beginning_path) << endl;
}
int find_all_paths(unordered_map<string, set<string>> graph, vector<string> current_path)
{
int found_paths = 0;
set<string> connected_nodes = graph[current_path.back()];
for (set<string>::iterator it = connected_nodes.begin(); it != connected_nodes.end(); ++it)
{
string current_node = *it;
if (current_node.compare(finish) == 0)
{
++found_paths;
}
else if (string::npos == current_node.find_first_of(lowercase) ||
current_path.end() == std::find(current_path.begin(), current_path.end(), current_node))
{ //Big cave or small cave not in path
vector<string> next_path(current_path);
next_path.push_back(current_node);
found_paths += find_all_paths(graph, next_path);
}
}
return found_paths;
}
+80
View File
@@ -0,0 +1,80 @@
#include <iostream>
#include <cstring>
#include <string>
#include <unordered_map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
string start = "start";
string finish = "end";
string lowercase = "qwertyuiopasdfghjklzxcvbnm";
int find_all_paths(unordered_map<string, set<string>> graph, vector<string> current_path, bool double_small_taken);
int main(int argc, char* argv[])
{
unordered_map<string, set<string>> graph;
string line;
while(getline(cin, line))
{
string::size_type n = line.find("-",0);
string node1 = line.substr(0,n);
string node2 = line.substr(n+1);
if (!graph.contains(node1))
{
graph.emplace(node1, set<string>());
}
if (!graph.contains(node2))
{
graph.emplace(node2, set<string>());
}
if (0 != node2.compare(start))
graph[node1].insert(node2);
if (0 != node1.compare(start))
graph[node2].insert(node1);
}
vector<string> beginning_path;
beginning_path.push_back(start);
cout << find_all_paths(graph, beginning_path, false) << endl;
}
int find_all_paths(unordered_map<string, set<string>> graph, vector<string> current_path, bool double_small_taken)
{
int found_paths = 0;
set<string> connected_nodes = graph[current_path.back()];
for (set<string>::iterator it = connected_nodes.begin(); it != connected_nodes.end(); ++it)
{
string current_node = *it;
if (current_node.compare(finish) == 0)
{
++found_paths;
}
else if (string::npos == current_node.find_first_of(lowercase) ||
current_path.end() == std::find(current_path.begin(), current_path.end(), current_node))
{ //Big cave or small cave not in path
vector<string> next_path(current_path);
next_path.push_back(current_node);
found_paths += find_all_paths(graph, next_path, double_small_taken);
}
else if (!double_small_taken)
{ //Small cave and in path, double is not taken though
vector<string> next_path(current_path);
next_path.push_back(current_node);
found_paths += find_all_paths(graph, next_path, true);
}
}
return found_paths;
}
+79
View File
@@ -0,0 +1,79 @@
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <utility>
using namespace std;
set<pair<int,int>> flip_about(set<pair<int,int>> input_points, bool x_flip, int value);
int main(int argc, char* argv[])
{
string line;
bool coord_input = true;
set<pair<int,int>> coords;
vector<pair<bool,int>> folds;
while(getline(cin, line))
{
if (0 == line.size())
{
coord_input = false;
continue;
}
if (coord_input)
{
string::size_type n = line.find(',');
pair<int,int> coord(stoi(line.substr(0,n)), stoi(line.substr(n+1)));
coords.insert(coord);
}
else
{
string::size_type n = line.find_last_of(' ');
line = line.substr(n+1);
pair<bool,int> fold(line[0]=='x', stoi(line.substr(2)));
folds.push_back(fold);
}
}
coords = flip_about(coords, folds[0].first, folds[0].second);
cout << coords.size() << endl;
}
set<pair<int,int>> flip_about(set<pair<int,int>> input_points, bool x_flip, int value)
{
set<pair<int,int>> output_points;
set<pair<int,int>>::iterator it = input_points.begin();
for(; it != input_points.end(); ++it)
{
if (x_flip)
{
if (it->first > value)
{
pair<int,int> flipped_coord(2*value-it->first, it->second);
output_points.insert(flipped_coord);
}
else
{
output_points.insert(*it);
}
}
else
{
if (it->second > value)
{
pair<int,int> flipped_coord(it->first, 2*value-it->second);
output_points.insert(flipped_coord);
}
else
{
output_points.insert(*it);
}
}
}
return output_points;
}
+124
View File
@@ -0,0 +1,124 @@
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <utility>
using namespace std;
set<pair<int,int>> flip_about(set<pair<int,int>> input_points, bool x_flip, int value);
int main(int argc, char* argv[])
{
string line;
bool coord_input = true;
set<pair<int,int>> coords;
vector<pair<bool,int>> folds;
while(getline(cin, line))
{
if (0 == line.size())
{
coord_input = false;
continue;
}
if (coord_input)
{
string::size_type n = line.find(',');
pair<int,int> coord(stoi(line.substr(0,n)), stoi(line.substr(n+1)));
coords.insert(coord);
}
else
{
string::size_type n = line.find_last_of(' ');
line = line.substr(n+1);
pair<bool,int> fold(line[0]=='x', stoi(line.substr(2)));
folds.push_back(fold);
}
}
for (unsigned int i = 0; i < folds.size(); i++)
{
coords = flip_about(coords, folds[i].first, folds[i].second);
}
int xmax = 0, ymax = 0;
for (set<pair<int,int>>::iterator it = coords.begin(); it != coords.end(); ++it)
{
if (it->first > xmax)
{
xmax = it->first;
}
if (it->second > ymax)
{
ymax = it->second;
}
}
++xmax;
++ymax;
bool** marked = new bool*[xmax];
for(int x = 0; x < xmax; x++)
{
marked[x] = new bool[ymax];
for(int y = 0; y < ymax; y++)
{
marked[x][y] = false;
}
}
for (set<pair<int,int>>::iterator it = coords.begin(); it != coords.end(); ++it)
{
marked[it->first][it->second] = true;
}
for (int y = 0; y < ymax; y++)
{
for (int x = 0; x < xmax; x++)
{
cout << (marked[x][y]?"#":" ");
}
cout << endl;
}
for(int x = 0; x < xmax; x++)
{
delete[] marked[x];
}
delete[] marked;
}
set<pair<int,int>> flip_about(set<pair<int,int>> input_points, bool x_flip, int value)
{
set<pair<int,int>> output_points;
set<pair<int,int>>::iterator it = input_points.begin();
for(; it != input_points.end(); ++it)
{
if (x_flip)
{
if (it->first > value)
{
pair<int,int> flipped_coord(2*value-it->first, it->second);
output_points.insert(flipped_coord);
}
else
{
output_points.insert(*it);
}
}
else
{
if (it->second > value)
{
pair<int,int> flipped_coord(it->first, 2*value-it->second);
output_points.insert(flipped_coord);
}
else
{
output_points.insert(*it);
}
}
}
return output_points;
}
+125
View File
@@ -0,0 +1,125 @@
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<char, unsigned long long> get_count_after_reaction(unordered_map<string, char> reactions, string pair, int reaction_iterations);
int main(int argc, char* argv[])
{
string polymer;
getline(cin, polymer);
unordered_map<string, char> reactions;
string line;
getline(cin, line);
while(getline(cin, line))
{
reactions[line.substr(0,2)] = line[line.size()-1];
}
unordered_map<char, unsigned long long> counts;
for(int i = 0; i < polymer.size(); i++)
{
counts[polymer[i]] = 0;
}
for(int i = 0; i < polymer.size(); i++)
{
counts[polymer[i]]++;
}
for(int i = 0; i < polymer.size()-1; i++)
{
string pair = polymer.substr(i,2);
unordered_map<char, unsigned long long> pair_counts = get_count_after_reaction(reactions, pair, 10);
for(auto &it : pair_counts)
{
if (!counts.contains(it.first))
{
counts[it.first] = 0;
}
counts[it.first] += it.second;
}
}
vector<unsigned long long> counts_list;
for (auto &it : counts)
{
cout << it.first << " " << it.second << endl;
counts_list.push_back(it.second);
}
sort(counts_list.begin(), counts_list.end());
cout << counts_list[counts_list.size()-1]-counts_list[0] << endl;
}
unordered_map<char, unsigned long long> get_count_after_reaction(unordered_map<string, char> reactions, string pair, int reaction_iterations)
{
static unordered_map<string, vector<unordered_map<char, unsigned long long>>> reaction_cache;
if (0 == reaction_iterations)
{
return unordered_map<char, unsigned long long>();
}
if (!reaction_cache.contains(pair))
{
reaction_cache[pair] = vector<unordered_map<char, unsigned long long>>();
}
if (reaction_cache[pair].size() >= reaction_iterations)
{
if (reaction_cache[pair][reaction_iterations-1].size() != 0)
{
return reaction_cache[pair][reaction_iterations-1];
}
}
else
{
unsigned int old_size = reaction_cache[pair].size();
for (unsigned int current_size = old_size; current_size < reaction_iterations; current_size++)
{
reaction_cache[pair].push_back(unordered_map<char,unsigned long long>());
}
}
unordered_map<char, unsigned long long> out_counts;
out_counts[reactions[pair]] = 1;
string left_pair = pair.substr(0,1) + reactions[pair];
string right_pair = reactions[pair] + pair.substr(1,1);
unordered_map<char, unsigned long long> left_counts = get_count_after_reaction(reactions, left_pair, reaction_iterations-1);
unordered_map<char, unsigned long long> right_counts = get_count_after_reaction(reactions, right_pair, reaction_iterations-1);
for (auto &it : left_counts)
{
if (!out_counts.contains(it.first))
{
out_counts[it.first] = 0;
}
out_counts[it.first] += it.second;
}
for (auto &it : right_counts)
{
if (!out_counts.contains(it.first))
{
out_counts[it.first] = 0;
}
out_counts[it.first] += it.second;
}
reaction_cache[pair][reaction_iterations-1] = out_counts;
return out_counts;
}
+125
View File
@@ -0,0 +1,125 @@
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<char, unsigned long long> get_count_after_reaction(unordered_map<string, char> reactions, string pair, int reaction_iterations);
int main(int argc, char* argv[])
{
string polymer;
getline(cin, polymer);
unordered_map<string, char> reactions;
string line;
getline(cin, line);
while(getline(cin, line))
{
reactions[line.substr(0,2)] = line[line.size()-1];
}
unordered_map<char, unsigned long long> counts;
for(int i = 0; i < polymer.size(); i++)
{
counts[polymer[i]] = 0;
}
for(int i = 0; i < polymer.size(); i++)
{
counts[polymer[i]]++;
}
for(int i = 0; i < polymer.size()-1; i++)
{
string pair = polymer.substr(i,2);
unordered_map<char, unsigned long long> pair_counts = get_count_after_reaction(reactions, pair, 40);
for(auto &it : pair_counts)
{
if (!counts.contains(it.first))
{
counts[it.first] = 0;
}
counts[it.first] += it.second;
}
}
vector<unsigned long long> counts_list;
for (auto &it : counts)
{
cout << it.first << " " << it.second << endl;
counts_list.push_back(it.second);
}
sort(counts_list.begin(), counts_list.end());
cout << counts_list[counts_list.size()-1]-counts_list[0] << endl;
}
unordered_map<char, unsigned long long> get_count_after_reaction(unordered_map<string, char> reactions, string pair, int reaction_iterations)
{
static unordered_map<string, vector<unordered_map<char, unsigned long long>>> reaction_cache;
if (0 == reaction_iterations)
{
return unordered_map<char, unsigned long long>();
}
if (!reaction_cache.contains(pair))
{
reaction_cache[pair] = vector<unordered_map<char, unsigned long long>>();
}
if (reaction_cache[pair].size() >= reaction_iterations)
{
if (reaction_cache[pair][reaction_iterations-1].size() != 0)
{
return reaction_cache[pair][reaction_iterations-1];
}
}
else
{
unsigned int old_size = reaction_cache[pair].size();
for (unsigned int current_size = old_size; current_size < reaction_iterations; current_size++)
{
reaction_cache[pair].push_back(unordered_map<char,unsigned long long>());
}
}
unordered_map<char, unsigned long long> out_counts;
out_counts[reactions[pair]] = 1;
string left_pair = pair.substr(0,1) + reactions[pair];
string right_pair = reactions[pair] + pair.substr(1,1);
unordered_map<char, unsigned long long> left_counts = get_count_after_reaction(reactions, left_pair, reaction_iterations-1);
unordered_map<char, unsigned long long> right_counts = get_count_after_reaction(reactions, right_pair, reaction_iterations-1);
for (auto &it : left_counts)
{
if (!out_counts.contains(it.first))
{
out_counts[it.first] = 0;
}
out_counts[it.first] += it.second;
}
for (auto &it : right_counts)
{
if (!out_counts.contains(it.first))
{
out_counts[it.first] = 0;
}
out_counts[it.first] += it.second;
}
reaction_cache[pair][reaction_iterations-1] = out_counts;
return out_counts;
}
+59
View File
@@ -0,0 +1,59 @@
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
struct Line
{
int x1,y1,x2,y2;
friend istream &operator>>( istream &input, Line &l ) {
input >> l.x1 >> l.y1 >> l.x2 >> l.y2;
return input;
}
};
inline constexpr int num_lines{500};
inline constexpr int grid_size{1000};
int main()
{
int grid[grid_size*grid_size] = {0};
Line line;
set<int> dangerous_locations;
for(int i = 0; i < num_lines; i++)
{
cin >> line;
if (line.x1 == line.x2)
{
for(int y = min(line.y1, line.y2); y <= max(line.y1, line.y2); y++)
{
grid[line.x1 + grid_size*y]++;
if (grid[line.x1+y*grid_size] >= 2)
{
dangerous_locations.insert(line.x1+y*grid_size);
}
}
}
else if (line.y1 == line.y2)
{
for(int x = min(line.x1, line.x2); x <= max(line.x1, line.x2); x++)
{
grid[x + line.y1*grid_size]++;
if (grid[x+line.y1*grid_size] >= 2)
{
dangerous_locations.insert(x+line.y1*grid_size);
}
}
}
}
cout << dangerous_locations.size() << endl;
return 0;
}
+86
View File
@@ -0,0 +1,86 @@
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
struct Line
{
int x1,y1,x2,y2;
friend istream &operator>>( istream &input, Line &l ) {
input >> l.x1 >> l.y1 >> l.x2 >> l.y2;
return input;
}
};
inline constexpr int num_lines{500};
inline constexpr int grid_size{1000};
int main()
{
int grid[grid_size*grid_size] = {0};
Line line;
set<int> dangerous_locations;
for(int i = 0; i < num_lines; i++)
{
cin >> line;
if (line.x1 == line.x2)
{
for(int y = min(line.y1, line.y2); y <= max(line.y1, line.y2); y++)
{
grid[line.x1 + grid_size*y]++;
if (grid[line.x1+y*grid_size] >= 2)
{
dangerous_locations.insert(line.x1+y*grid_size);
}
}
}
else if (line.y1 == line.y2)
{
for(int x = min(line.x1, line.x2); x <= max(line.x1, line.x2); x++)
{
grid[x + line.y1*grid_size]++;
if (grid[x+line.y1*grid_size] >= 2)
{
dangerous_locations.insert(x+line.y1*grid_size);
}
}
}
else
{
if ((line.y2-line.y1)/(line.x2-line.x1) > 0)
{
for(int x = min(line.x1, line.x2), y = min(line.y1, line.y2); x <= max(line.x1, line.x2); x++, y++)
{
grid[x + y*grid_size]++;
if (grid[x+y*grid_size] >= 2)
{
dangerous_locations.insert(x+y*grid_size);
}
}
}
else
{
for(int x = min(line.x1, line.x2), y = max(line.y1, line.y2); x <= max(line.x1, line.x2); x++, y--)
{
grid[x + y*grid_size]++;
if (grid[x+y*grid_size] >= 2)
{
dangerous_locations.insert(x+y*grid_size);
}
}
}
}
}
cout << dangerous_locations.size() << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> fish(300);
for(int i = 0; i < 300; i++) cin >> fish[i];
for(int i = 0; i < 80; i++)
{
int new_fish_count = 0;
for(int j = 0; j < fish.size(); j++)
{
if (--fish[j] < 0)
{
fish[j] = 6;
new_fish_count++;
}
}
vector<int> new_fish(new_fish_count, 8);
fish.insert(fish.end(), new_fish.begin(), new_fish.end());
}
cout << fish.size() << endl;
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include <iostream>
using namespace std;
int main()
{
int next_fish;
unsigned long long fish[9] = {0};
for(int i = 0; i < 300; i++)
{
cin >> next_fish;
fish[next_fish]++;
}
unsigned long long total_fish = 300;
for(int i = 0; i < 256; i++)
{
unsigned long long spawning_fish = fish[0];
total_fish += spawning_fish;
for(int j = 1; j < 9; j++)
{
fish[j-1] = fish[j];
}
fish[6] += spawning_fish;
fish[8] = spawning_fish;
}
cout << total_fish << endl;
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
vector<int> crabs(1000);
for(int i = 0; i < 1000; i++) cin >> crabs[i];
sort(crabs.begin(), crabs.end());
int final_position = round(((float)crabs[499]+(float)crabs[500])/2.0f);
int fuel = 0;
for(int i = 0; i < 1000; i++) fuel += (int)abs((float)final_position-crabs[i]);
cout << fuel << endl;
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
vector<int> crabs(1000);
for(int i = 0; i < 1000; i++) cin >> crabs[i];
sort(crabs.begin(), crabs.end());
vector<int> costs(crabs[999],0);
for(int i = 0; i < costs.size(); i++)
{
for(int j = 0; j < 1000; j++)
{
int diff = (int)abs((float)i-crabs[j]);
costs[i] += (diff*(diff+1))/2;
}
}
sort(costs.begin(), costs.end());
cout << costs[0] << endl;
return 0;
}
+51
View File
@@ -0,0 +1,51 @@
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
using namespace std;
struct Note
{
string patterns[10];
string outputs[4];
friend istream &operator>>( istream &input, Note &n ) {
for(int i = 0; i < 10; i++)
{
input >> n.patterns[i];
}
for(int i = 0; i < 4; i++)
{
input >> n.outputs[i];
}
return input;
}
};
int main()
{
Note note;
int count = 0;
while (cin >> note)
{
for(int i = 0; i < 4; i++)
{
switch(note.outputs[i].size())
{
case 2:
case 3:
case 4:
case 7:
count++;
break;
default:
break;
}
}
}
cout << count << endl;
return 0;
}
+236
View File
@@ -0,0 +1,236 @@
#include <iostream>
#include <algorithm>
#include <string>
#include <array>
#include <set>
#include <vector>
#include <cassert>
using namespace std;
struct compare_length {
inline bool operator()(const std::string& first,
const std::string& second) const
{
return first.size() < second.size();
}
};
struct Note
{
array<string,10> patterns;
array<string,4> outputs;
friend istream &operator>>( istream &input, Note &n ) {
for(int i = 0; i < 10; i++)
{
input >> n.patterns[i];
sort(n.patterns[i].begin(), n.patterns[i].end());
}
compare_length c;
sort(n.patterns.begin(), n.patterns.end(), c);
sort(n.patterns.begin()+3,n.patterns.begin()+6);
sort(n.patterns.begin()+6,n.patterns.begin()+9);
for(int i = 0; i < 4; i++)
{
input >> n.outputs[i];
sort(n.outputs[i].begin(), n.outputs[i].end());
}
return input;
}
};
int main()
{
Note note;
int result = 0;
for(int total_count = 0; total_count < 200; total_count++)
{
cin >> note;
int pattern_map[10] = {1,7,4,9,9,9,9,9,9,8};
vector<set<char>> pattern_sets(note.patterns.size());
for(int i = 0; i < note.patterns.size(); i++)
{
set<char> cur_set;
for(int j = 0; j < note.patterns[i].size(); j++)
{
cur_set.insert(note.patterns[i][j]);
}
pattern_sets[i] = cur_set;
}
set<char> top = pattern_sets[1];
for(set<char>::iterator it = pattern_sets[0].begin(); it != pattern_sets[0].end(); ++it) top.erase(*it);
set<char> middle = pattern_sets[2];
for(set<char>::iterator it = pattern_sets[0].begin(); it != pattern_sets[0].end(); ++it) middle.erase(*it);
set<char> left_top = middle;
for(set<char>::iterator it = middle.begin(); it != middle.end(); ++it)
{
while (middle.end() != it && (pattern_sets[3].end() == pattern_sets[3].find(*it) ||
pattern_sets[4].end() == pattern_sets[4].find(*it) ||
pattern_sets[5].end() == pattern_sets[5].find(*it)))
{
it = middle.erase(it);
}
}
left_top.erase(*middle.begin());
set<char> bottom = pattern_sets[9];
for(set<char>::iterator it = pattern_sets[1].begin(); it != pattern_sets[1].end(); ++it) bottom.erase(*it);
for(set<char>::iterator it = pattern_sets[2].begin(); it != pattern_sets[2].end(); ++it) bottom.erase(*it);
set<char> left_bottom = bottom;
for(set<char>::iterator it = bottom.begin(); it != bottom.end(); ++it)
{
while (bottom.end() != it && (pattern_sets[3].end() == pattern_sets[3].find(*it) ||
pattern_sets[4].end() == pattern_sets[4].find(*it) ||
pattern_sets[5].end() == pattern_sets[5].find(*it)))
{
it = bottom.erase(it);
}
}
left_bottom.erase(*bottom.begin());
set<char> x;
for(int i = 6; i <= 8; i++)
{
if (pattern_sets[i].find(*middle.begin()) != pattern_sets[i].end() &&
pattern_sets[i].find(*left_bottom.begin()) != pattern_sets[i].end())
{
x = pattern_sets[i];
break;
}
}
set<char> right_top = pattern_sets[0];
for(set<char>::iterator it = x.begin(); it != x.end(); ++it)
right_top.erase(*it);
set<char> right_bottom = pattern_sets[0];
right_bottom.erase(*right_top.begin());
string zero = "";
zero.append(1,*top.begin());
zero.append(1,*left_top.begin());
zero.append(1,*right_top.begin());
zero.append(1,*left_bottom.begin());
zero.append(1,*right_bottom.begin());
zero.append(1,*bottom.begin());
sort(zero.begin(),zero.end());
for(int i = 6; i <= 8; i++)
{
if (zero.compare(note.patterns[i]) == 0)
{
pattern_map[i] = 0;
}
}
string two = "";
two.append(1,*top.begin());
two.append(1,*right_top.begin());
two.append(1,*middle.begin());
two.append(1,*left_bottom.begin());
two.append(1,*bottom.begin());
sort(two.begin(), two.end());
for(int i = 3; i <= 5; i++)
{
if (two.compare(note.patterns[i]) == 0)
{
pattern_map[i] = 2;
}
}
string three = "";
three.append(1,*top.begin());
three.append(1,*right_top.begin());
three.append(1,*middle.begin());
three.append(1,*right_bottom.begin());
three.append(1,*bottom.begin());
sort(three.begin(), three.end());
for(int i = 3; i <= 5; i++)
{
if (three.compare(note.patterns[i]) == 0)
{
pattern_map[i] = 3;
}
}
string five = "";
five.append(1,*top.begin());
five.append(1,*left_top.begin());
five.append(1,*middle.begin());
five.append(1,*right_bottom.begin());
five.append(1,*bottom.begin());
sort(five.begin(), five.end());
for(int i = 3; i <= 5; i++)
{
if (five.compare(note.patterns[i]) == 0)
{
pattern_map[i] = 5;
}
}
string six = "";
six.append(1,*top.begin());
six.append(1,*left_top.begin());
six.append(1,*middle.begin());
six.append(1,*right_bottom.begin());
six.append(1,*left_bottom.begin());
six.append(1,*bottom.begin());
sort(six.begin(), six.end());
for(int i = 6; i <= 8; i++)
{
if (six.compare(note.patterns[i]) == 0)
{
pattern_map[i] = 6;
}
}
for(int i = 0; i < 10; i++)
{
bool found = false;
for(int j = 0; j < 10; j++)
{
if (pattern_map[j] == i)
{
found = true;
break;
}
}
if (!found)
{
cout << total_count << endl;
}
}
for(int i = 0, m = 1000; i < 4; i++, m/=10)
{
for(int j = 0; j < 10; j++)
{
if (0 == note.outputs[i].compare(note.patterns[j]))
{
result += m*pattern_map[j];
break;
}
}
}
}
cout << result << endl;
return 0;
}
+45
View File
@@ -0,0 +1,45 @@
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int height_map[100*100];
for(int i = 0; i < 100; i++)
{
string in;
cin >> in;
for(int j = 0; j < 100; j++)
{
height_map[j+i*100] = (int)in[j]-(int)'0';
}
}
int result = 0;
for(int i = 0; i < 100*100; i++)
{
if (i-100 >= 0 && height_map[i-100] <= height_map[i])
{
continue;
}
if (i+100 < 100*100 && height_map[i+100] <= height_map[i])
{
continue;
}
if (i-1 >= 0 && height_map[i-1] <= height_map[i])
{
continue;
}
if (i+1 < 100*100 && height_map[i+1] <= height_map[i])
{
continue;
}
result += height_map[i]+1;
}
cout << result << endl;
return 0;
}
+82
View File
@@ -0,0 +1,82 @@
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <set>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
int height_map[100*100];
for(int i = 0; i < 100; i++)
{
string in;
cin >> in;
for(int j = 0; j < 100; j++)
{
height_map[j+i*100] = (int)in[j]-(int)'0';
}
}
vector<int> basins(3, 0);
for(int i = 0; i < 100*100; i++)
{
if (i-100 >= 0 && height_map[i-100] <= height_map[i])
{
continue;
}
if (i+100 < 100*100 && height_map[i+100] <= height_map[i])
{
continue;
}
if (i-1 >= 0 && height_map[i-1] <= height_map[i])
{
continue;
}
if (i+1 < 100*100 && height_map[i+1] <= height_map[i])
{
continue;
}
queue<pair<int,int>> to_explore;
to_explore.push(pair<int,int>(i-100,i));
to_explore.push(pair<int,int>(i+100,i));
to_explore.push(pair<int,int>(i-1,i));
to_explore.push(pair<int,int>(i+1,i));
set<int> basin;
basin.insert(i);
while(!to_explore.empty())
{
pair<int,int> cur = to_explore.front();
to_explore.pop();
if (cur.first < 0 || cur.first >= 100*100)
{
continue;
}
if (height_map[cur.first] != 9 && height_map[cur.first] > height_map[cur.second])
{
pair<set<int>::iterator,bool> res = basin.insert(cur.first);
if (res.second)
{
to_explore.push(pair<int,int>(cur.first-100,cur.first));
to_explore.push(pair<int,int>(cur.first+100,cur.first));
to_explore.push(pair<int,int>(cur.first-1,cur.first));
to_explore.push(pair<int,int>(cur.first+1,cur.first));
}
}
}
basins.push_back(basin.size());
sort(basins.begin(), basins.end());
basins.erase(basins.begin());
}
cout << basins[0]*basins[1]*basins[2] << endl;
return 0;
}