Compare commits
11 Commits
master
...
b73e91957d
| Author | SHA1 | Date | |
|---|---|---|---|
| b73e91957d | |||
| 719c1e1b69 | |||
| 8e30a8fea1 | |||
| 8fe274116c | |||
| e8b377e008 | |||
| 666fdea266 | |||
| b92b49fe98 | |||
| 78e28f6959 | |||
| 63db42f55a | |||
| cef11b36ff | |||
| 95db0b1fa8 |
@@ -0,0 +1,2 @@
|
|||||||
|
*/**
|
||||||
|
!*/*.cpp
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
int total = 0;
|
||||||
|
int max = 0;
|
||||||
|
while(std::getline(std::cin, line))
|
||||||
|
{
|
||||||
|
if (0 == line.size())
|
||||||
|
{
|
||||||
|
if (total > max)
|
||||||
|
{
|
||||||
|
max = total;
|
||||||
|
}
|
||||||
|
total = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
total += stoi(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << max << std::endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
int total = 0;
|
||||||
|
std::vector<int> sums;
|
||||||
|
while(std::getline(std::cin, line))
|
||||||
|
{
|
||||||
|
if (0 == line.size())
|
||||||
|
{
|
||||||
|
sums.push_back(total);
|
||||||
|
total = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
total += stoi(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(sums.begin(), sums.end(), std::greater<>());
|
||||||
|
std::cout << sums[0] + sums[1] + sums[2] << std::endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
string line;
|
||||||
|
int cycle = 1;
|
||||||
|
int accumulator = 1;
|
||||||
|
int answer = 0;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
int next_adden = 0;
|
||||||
|
if (line.substr(0,4).compare("addx") == 0)
|
||||||
|
{
|
||||||
|
next_adden = stoi(line.substr(5));
|
||||||
|
cycle++;
|
||||||
|
if ((cycle-20)%40 == 0)
|
||||||
|
{
|
||||||
|
answer += cycle*accumulator;
|
||||||
|
}
|
||||||
|
accumulator += next_adden;
|
||||||
|
}
|
||||||
|
|
||||||
|
cycle++;
|
||||||
|
if ((cycle-20)%40 == 0)
|
||||||
|
{
|
||||||
|
answer += cycle*accumulator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << answer << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
string line;
|
||||||
|
int cycle = 1;
|
||||||
|
int x = 0;
|
||||||
|
int accumulator = 1;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
if (accumulator >= x-1 && accumulator <= x+1)
|
||||||
|
{
|
||||||
|
cout << "#";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cycle++;
|
||||||
|
x++;
|
||||||
|
if (0 == x%40)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.substr(0,4).compare("addx") == 0)
|
||||||
|
{
|
||||||
|
if (accumulator >= x-1 && accumulator <= x+1)
|
||||||
|
{
|
||||||
|
cout << "#";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
accumulator += stoi(line.substr(5));
|
||||||
|
cycle++;
|
||||||
|
x++;
|
||||||
|
if (0 == x%40)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
string plays[] = {"A X", "A Y", "A Z", "B X", "B Y", "B Z", "C X", "C Y", "C Z"};
|
||||||
|
int play_scores[] = {1 + 3, 2 + 6, 3 + 0, 1 + 0, 2 + 3, 3 + 6, 1 + 6, 2 + 0, 3 + 3};
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int total_score = 0;
|
||||||
|
string line;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
int i = find(&plays[0], &plays[9], line)-&plays[0];
|
||||||
|
total_score += play_scores[i];
|
||||||
|
}
|
||||||
|
cout << total_score << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
string plays[] = {"A X", "A Y", "A Z", "B X", "B Y", "B Z", "C X", "C Y", "C Z"};
|
||||||
|
int play_scores[] = {3 + 0, 1 + 3, 2 + 6, 1 + 0, 2 + 3, 3 + 6, 2 + 0, 3 + 3, 1 + 6};
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int total_score = 0;
|
||||||
|
string line;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
int i = find(&plays[0], &plays[9], line)-&plays[0];
|
||||||
|
total_score += play_scores[i];
|
||||||
|
}
|
||||||
|
cout << total_score << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
string line;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
string half1 = line.substr(0, line.size()/2);
|
||||||
|
string half2 = line.substr(line.size()/2);
|
||||||
|
|
||||||
|
sort(half1.begin(), half1.end());
|
||||||
|
sort(half2.begin(), half2.end());
|
||||||
|
|
||||||
|
string remaining;
|
||||||
|
set_intersection(half1.begin(), half1.end(), half2.begin(), half2.end(), back_inserter(remaining));
|
||||||
|
|
||||||
|
int value;
|
||||||
|
if (remaining[0] >= 'a')
|
||||||
|
{
|
||||||
|
value = remaining[0]-'a'+1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = remaining[0]-'A'+27;
|
||||||
|
}
|
||||||
|
|
||||||
|
total += value;
|
||||||
|
}
|
||||||
|
cout << total << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
string line1,line2,line3;
|
||||||
|
while(getline(cin, line1))
|
||||||
|
{
|
||||||
|
getline(cin, line2);
|
||||||
|
getline(cin, line3);
|
||||||
|
|
||||||
|
sort(line1.begin(), line1.end());
|
||||||
|
sort(line2.begin(), line2.end());
|
||||||
|
sort(line3.begin(), line3.end());
|
||||||
|
|
||||||
|
string first_intersection, second_intersection;
|
||||||
|
set_intersection(line1.begin(), line1.end(), line2.begin(), line2.end(), back_inserter(first_intersection));
|
||||||
|
set_intersection(first_intersection.begin(), first_intersection.end(), line3.begin(), line3.end(), back_inserter(second_intersection));
|
||||||
|
|
||||||
|
int value;
|
||||||
|
if (second_intersection[0] >= 'a')
|
||||||
|
{
|
||||||
|
value = second_intersection[0]-'a'+1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = second_intersection[0]-'A'+27;
|
||||||
|
}
|
||||||
|
|
||||||
|
total += value;
|
||||||
|
}
|
||||||
|
cout << total << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
string line;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
string::size_type n = line.find(',');
|
||||||
|
string half1 = line.substr(0,n);
|
||||||
|
string half2 = line.substr(n+1);
|
||||||
|
|
||||||
|
n = half1.find('-');
|
||||||
|
int half1_start = stoi(half1.substr(0,n));
|
||||||
|
int half1_end = stoi(half1.substr(n+1));
|
||||||
|
|
||||||
|
n = half2.find('-');
|
||||||
|
int half2_start = stoi(half2.substr(0,n));
|
||||||
|
int half2_end = stoi(half2.substr(n+1));
|
||||||
|
|
||||||
|
if ((half1_start <= half2_start && half1_end >= half2_end) ||
|
||||||
|
(half2_start <= half1_start && half2_end >= half1_end))
|
||||||
|
{
|
||||||
|
total++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << total << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
string line;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
string::size_type n = line.find(',');
|
||||||
|
string half1 = line.substr(0,n);
|
||||||
|
string half2 = line.substr(n+1);
|
||||||
|
|
||||||
|
n = half1.find('-');
|
||||||
|
int half1_start = stoi(half1.substr(0,n));
|
||||||
|
int half1_end = stoi(half1.substr(n+1));
|
||||||
|
|
||||||
|
n = half2.find('-');
|
||||||
|
int half2_start = stoi(half2.substr(0,n));
|
||||||
|
int half2_end = stoi(half2.substr(n+1));
|
||||||
|
|
||||||
|
if (!(half1_end < half2_start || half2_end < half1_start))
|
||||||
|
{
|
||||||
|
total++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << total << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
bool instruction_mode = false;
|
||||||
|
string line;
|
||||||
|
unordered_map<int, string> stacks;
|
||||||
|
|
||||||
|
struct Instruction
|
||||||
|
{
|
||||||
|
int quantity;
|
||||||
|
int from;
|
||||||
|
int to;
|
||||||
|
};
|
||||||
|
|
||||||
|
vector<Instruction> instructions;
|
||||||
|
|
||||||
|
int stack_max = 0;
|
||||||
|
|
||||||
|
while (getline(cin, line))
|
||||||
|
{
|
||||||
|
if (0 == line.size())
|
||||||
|
{
|
||||||
|
instruction_mode = true;
|
||||||
|
|
||||||
|
for (auto &it : stacks)
|
||||||
|
{
|
||||||
|
it.second = it.second.substr(0, it.second.size()-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instruction_mode)
|
||||||
|
{
|
||||||
|
Instruction instruction;
|
||||||
|
line = line.substr(5);
|
||||||
|
string::size_type n = line.find(' ');
|
||||||
|
instruction.quantity = stoi(line.substr(0,n));
|
||||||
|
line = line.substr(n+6);
|
||||||
|
n = line.find(' ');
|
||||||
|
instruction.from = stoi(line.substr(0,n));
|
||||||
|
line = line.substr(n+4);
|
||||||
|
instruction.to = stoi(line);
|
||||||
|
|
||||||
|
instructions.push_back(instruction);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 1, s = 1; i < line.size(); i+=4, s++)
|
||||||
|
{
|
||||||
|
if (line[i] == ' ')
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stacks.contains(s))
|
||||||
|
{
|
||||||
|
stacks[s] = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
stacks[s] += line[i];
|
||||||
|
|
||||||
|
if (s > stack_max)
|
||||||
|
{
|
||||||
|
stack_max = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < instructions.size(); i++)
|
||||||
|
{
|
||||||
|
string taken = stacks[instructions[i].from].substr(0, instructions[i].quantity);
|
||||||
|
reverse(taken.begin(), taken.end());
|
||||||
|
stacks[instructions[i].from] = stacks[instructions[i].from].substr(instructions[i].quantity);
|
||||||
|
stacks[instructions[i].to] = taken + stacks[instructions[i].to];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int s = 1; s <= stack_max; s++)
|
||||||
|
{
|
||||||
|
cout << stacks[s][0];
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
bool instruction_mode = false;
|
||||||
|
string line;
|
||||||
|
unordered_map<int, string> stacks;
|
||||||
|
|
||||||
|
struct Instruction
|
||||||
|
{
|
||||||
|
int quantity;
|
||||||
|
int from;
|
||||||
|
int to;
|
||||||
|
};
|
||||||
|
|
||||||
|
vector<Instruction> instructions;
|
||||||
|
|
||||||
|
int stack_max = 0;
|
||||||
|
|
||||||
|
while (getline(cin, line))
|
||||||
|
{
|
||||||
|
if (0 == line.size())
|
||||||
|
{
|
||||||
|
instruction_mode = true;
|
||||||
|
|
||||||
|
for (auto &it : stacks)
|
||||||
|
{
|
||||||
|
it.second = it.second.substr(0, it.second.size()-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instruction_mode)
|
||||||
|
{
|
||||||
|
Instruction instruction;
|
||||||
|
line = line.substr(5);
|
||||||
|
string::size_type n = line.find(' ');
|
||||||
|
instruction.quantity = stoi(line.substr(0,n));
|
||||||
|
line = line.substr(n+6);
|
||||||
|
n = line.find(' ');
|
||||||
|
instruction.from = stoi(line.substr(0,n));
|
||||||
|
line = line.substr(n+4);
|
||||||
|
instruction.to = stoi(line);
|
||||||
|
|
||||||
|
instructions.push_back(instruction);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 1, s = 1; i < line.size(); i+=4, s++)
|
||||||
|
{
|
||||||
|
if (line[i] == ' ')
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stacks.contains(s))
|
||||||
|
{
|
||||||
|
stacks[s] = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
stacks[s] += line[i];
|
||||||
|
|
||||||
|
if (s > stack_max)
|
||||||
|
{
|
||||||
|
stack_max = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < instructions.size(); i++)
|
||||||
|
{
|
||||||
|
string taken = stacks[instructions[i].from].substr(0, instructions[i].quantity);
|
||||||
|
stacks[instructions[i].from] = stacks[instructions[i].from].substr(instructions[i].quantity);
|
||||||
|
stacks[instructions[i].to] = taken + stacks[instructions[i].to];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int s = 1; s <= stack_max; s++)
|
||||||
|
{
|
||||||
|
cout << stacks[s][0];
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
string signal;
|
||||||
|
cin >> signal;
|
||||||
|
|
||||||
|
for (int i = 0; i < signal.size()-4; i++)
|
||||||
|
{
|
||||||
|
string segment = signal.substr(i,4);
|
||||||
|
sort(segment.begin(), segment.end());
|
||||||
|
if (segment.end() == std::unique(segment.begin(), segment.end()))
|
||||||
|
{
|
||||||
|
cout << i+4 << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
string signal;
|
||||||
|
cin >> signal;
|
||||||
|
|
||||||
|
for (int i = 0; i < signal.size()-14; i++)
|
||||||
|
{
|
||||||
|
string segment = signal.substr(i,14);
|
||||||
|
sort(segment.begin(), segment.end());
|
||||||
|
if (segment.end() == std::unique(segment.begin(), segment.end()))
|
||||||
|
{
|
||||||
|
cout << i+14 << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+119
@@ -0,0 +1,119 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct Dir
|
||||||
|
{
|
||||||
|
Dir(string name) : name{name},subdir{vector<Dir>()},files{unordered_map<string,int>()},parent{nullptr} {}
|
||||||
|
|
||||||
|
string name;
|
||||||
|
vector<Dir> subdir;
|
||||||
|
unordered_map<string, int> files;
|
||||||
|
Dir* parent;
|
||||||
|
};
|
||||||
|
|
||||||
|
int get_sum_small_dirs(Dir dir);
|
||||||
|
|
||||||
|
int get_dir_size(Dir dir);
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
Dir root_dir("/");
|
||||||
|
|
||||||
|
Dir* current_dir = &root_dir;
|
||||||
|
|
||||||
|
string line;
|
||||||
|
getline(cin, line);
|
||||||
|
getline(cin, line);
|
||||||
|
while (line.size() != 0)
|
||||||
|
{
|
||||||
|
if (line.substr(2,2).compare("cd") == 0)
|
||||||
|
{
|
||||||
|
if (line.substr(5).compare("..") == 0)
|
||||||
|
{
|
||||||
|
current_dir = current_dir->parent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (vector<Dir>::iterator it = current_dir->subdir.begin(); it != current_dir->subdir.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->name.compare(line.substr(5)) == 0)
|
||||||
|
{
|
||||||
|
current_dir = &(*it);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getline(cin, line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (line.substr(2,2).compare("ls") == 0)
|
||||||
|
{
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
getline(cin, line);
|
||||||
|
|
||||||
|
if (line.size() == 0 || line[0] == '$')
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.substr(0,3).compare("dir") == 0)
|
||||||
|
{
|
||||||
|
Dir subdir(line.substr(4));
|
||||||
|
subdir.parent = current_dir;
|
||||||
|
current_dir->subdir.push_back(subdir);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string::size_type n = line.find(' ');
|
||||||
|
int file_size = stoi(line.substr(0,n));
|
||||||
|
string file_name = line.substr(n+1);
|
||||||
|
current_dir->files[file_name] = file_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << get_sum_small_dirs(root_dir) << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_sum_small_dirs(Dir dir)
|
||||||
|
{
|
||||||
|
int total_size = 0;
|
||||||
|
|
||||||
|
int dir_size = get_dir_size(dir);
|
||||||
|
|
||||||
|
if (dir_size < 100000)
|
||||||
|
{
|
||||||
|
total_size += dir_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < dir.subdir.size(); i++)
|
||||||
|
{
|
||||||
|
total_size += get_sum_small_dirs(dir.subdir[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_dir_size(Dir dir)
|
||||||
|
{
|
||||||
|
int size = 0;
|
||||||
|
for(auto &file_it : dir.files)
|
||||||
|
{
|
||||||
|
size += file_it.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < dir.subdir.size(); i++)
|
||||||
|
{
|
||||||
|
size += get_dir_size(dir.subdir[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
+112
@@ -0,0 +1,112 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct Dir
|
||||||
|
{
|
||||||
|
Dir(string name) : name{name},subdir{vector<Dir>()},files{unordered_map<string,int>()},parent{nullptr} {}
|
||||||
|
|
||||||
|
string name;
|
||||||
|
vector<Dir> subdir;
|
||||||
|
unordered_map<string, int> files;
|
||||||
|
Dir* parent;
|
||||||
|
};
|
||||||
|
|
||||||
|
vector<int> dir_sizes;
|
||||||
|
|
||||||
|
int get_dir_size(Dir dir);
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
Dir root_dir("/");
|
||||||
|
|
||||||
|
Dir* current_dir = &root_dir;
|
||||||
|
|
||||||
|
string line;
|
||||||
|
getline(cin, line);
|
||||||
|
getline(cin, line);
|
||||||
|
while (line.size() != 0)
|
||||||
|
{
|
||||||
|
if (line.substr(2,2).compare("cd") == 0)
|
||||||
|
{
|
||||||
|
if (line.substr(5).compare("..") == 0)
|
||||||
|
{
|
||||||
|
current_dir = current_dir->parent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (vector<Dir>::iterator it = current_dir->subdir.begin(); it != current_dir->subdir.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->name.compare(line.substr(5)) == 0)
|
||||||
|
{
|
||||||
|
current_dir = &(*it);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getline(cin, line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (line.substr(2,2).compare("ls") == 0)
|
||||||
|
{
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
getline(cin, line);
|
||||||
|
|
||||||
|
if (line.size() == 0 || line[0] == '$')
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.substr(0,3).compare("dir") == 0)
|
||||||
|
{
|
||||||
|
Dir subdir(line.substr(4));
|
||||||
|
subdir.parent = current_dir;
|
||||||
|
current_dir->subdir.push_back(subdir);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string::size_type n = line.find(' ');
|
||||||
|
int file_size = stoi(line.substr(0,n));
|
||||||
|
string file_name = line.substr(n+1);
|
||||||
|
current_dir->files[file_name] = file_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int space_needed = 30000000 - 70000000 + get_dir_size(root_dir);
|
||||||
|
|
||||||
|
sort(dir_sizes.begin(), dir_sizes.end());
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < dir_sizes.size(); i++)
|
||||||
|
{
|
||||||
|
if (dir_sizes[i] >= space_needed)
|
||||||
|
{
|
||||||
|
cout << dir_sizes[i] << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_dir_size(Dir dir)
|
||||||
|
{
|
||||||
|
int size = 0;
|
||||||
|
for(auto &file_it : dir.files)
|
||||||
|
{
|
||||||
|
size += file_it.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < dir.subdir.size(); i++)
|
||||||
|
{
|
||||||
|
size += get_dir_size(dir.subdir[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
dir_sizes.push_back(size);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
string line;
|
||||||
|
getline(cin, line);
|
||||||
|
|
||||||
|
int length = line.size();
|
||||||
|
|
||||||
|
int* forest = new int[length*length];
|
||||||
|
int in_y = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
for(int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
forest[i+in_y*length] = line[i]-'0';
|
||||||
|
}
|
||||||
|
++in_y;
|
||||||
|
} while(getline(cin, line));
|
||||||
|
|
||||||
|
bool* visible = new bool[length*length];
|
||||||
|
for(int i = 0; i < length*length; i++)
|
||||||
|
{
|
||||||
|
visible[i] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int x = 0; x < length; x++)
|
||||||
|
{
|
||||||
|
//top down
|
||||||
|
int vis_height = -1;
|
||||||
|
for(int y = 0; y < length; y++)
|
||||||
|
{
|
||||||
|
if (forest[x+y*length] > vis_height)
|
||||||
|
{
|
||||||
|
vis_height = forest[x+y*length];
|
||||||
|
visible[x+y*length] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//bottom up
|
||||||
|
vis_height = -1;
|
||||||
|
for(int y = length-1; y >= 0; y--)
|
||||||
|
{
|
||||||
|
if (forest[x+y*length] > vis_height)
|
||||||
|
{
|
||||||
|
vis_height = forest[x+y*length];
|
||||||
|
visible[x+y*length] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int y = 0; y < length; y++)
|
||||||
|
{
|
||||||
|
//Left right
|
||||||
|
int vis_height = -1;
|
||||||
|
for(int x = 0; x < length; x++)
|
||||||
|
{
|
||||||
|
if (forest[x+y*length] > vis_height)
|
||||||
|
{
|
||||||
|
vis_height = forest[x+y*length];
|
||||||
|
visible[x+y*length] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Right left
|
||||||
|
vis_height = -1;
|
||||||
|
for(int x = length-1; x >= 0; x--)
|
||||||
|
{
|
||||||
|
if (forest[x+y*length] > vis_height)
|
||||||
|
{
|
||||||
|
vis_height = forest[x+y*length];
|
||||||
|
visible[x+y*length] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int total_vis = 0;
|
||||||
|
for(int i = 0; i < length*length; i++)
|
||||||
|
{
|
||||||
|
if(visible[i])
|
||||||
|
{
|
||||||
|
++total_vis;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << total_vis << endl;
|
||||||
|
|
||||||
|
delete[] visible;
|
||||||
|
delete[] forest;
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
string line;
|
||||||
|
getline(cin, line);
|
||||||
|
|
||||||
|
int length = line.size();
|
||||||
|
|
||||||
|
int* forest = new int[length*length];
|
||||||
|
int in_y = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
for(int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
forest[i+in_y*length] = line[i]-'0';
|
||||||
|
}
|
||||||
|
++in_y;
|
||||||
|
} while(getline(cin, line));
|
||||||
|
|
||||||
|
int best_vis = 0;
|
||||||
|
for(int y = 0; y < length; y++)
|
||||||
|
{
|
||||||
|
for(int x = 0; x < length; x++)
|
||||||
|
{
|
||||||
|
int north_vis = 0;
|
||||||
|
int south_vis = 0;
|
||||||
|
int west_vis = 0;
|
||||||
|
int east_vis = 0;
|
||||||
|
int cur_vis = forest[x+y*length];;
|
||||||
|
|
||||||
|
for(int j = y-1; j >= 0; j--)
|
||||||
|
{
|
||||||
|
north_vis++;
|
||||||
|
if (forest[x+j*length] >= cur_vis)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int j = y+1; j < length; j++)
|
||||||
|
{
|
||||||
|
south_vis++;
|
||||||
|
if (forest[x+j*length] >= cur_vis)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = x-1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
west_vis++;
|
||||||
|
if (forest[i+y*length] >= cur_vis)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = x+1; i<length; i++)
|
||||||
|
{
|
||||||
|
east_vis++;
|
||||||
|
if(forest[i+y*length] >= cur_vis)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int total_vis = north_vis*south_vis*east_vis*west_vis;
|
||||||
|
|
||||||
|
if (total_vis > best_vis)
|
||||||
|
{
|
||||||
|
best_vis = total_vis;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << best_vis << endl;
|
||||||
|
|
||||||
|
delete[] forest;
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <set>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct Rope
|
||||||
|
{
|
||||||
|
Rope(pair<int, int> head, pair<int, int> tail) : head{head}, tail{tail} {}
|
||||||
|
pair<int, int> head, tail;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
Rope move_rope(Rope rope, int dx, int dy);
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
Rope rope({0,0}, {0,0});
|
||||||
|
set<pair<int, int>> visited;
|
||||||
|
|
||||||
|
string line;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
char dir = line[0];
|
||||||
|
int amount = stoi(line.substr(2));
|
||||||
|
int dx = (dir=='R')?1:((dir=='L')?-1:0);
|
||||||
|
int dy = (dir=='U')?1:((dir=='D')?-1:0);
|
||||||
|
|
||||||
|
for(int i = 0; i < amount; i++)
|
||||||
|
{
|
||||||
|
rope = move_rope(rope, dx, dy);
|
||||||
|
visited.insert(rope.tail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << visited.size() << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rope move_rope(Rope rope, int dx, int dy)
|
||||||
|
{
|
||||||
|
rope.head.first += dx;
|
||||||
|
rope.head.second += dy;
|
||||||
|
|
||||||
|
if (rope.head.first == rope.tail.first)
|
||||||
|
{ //same x
|
||||||
|
if (rope.head.second > rope.tail.second)
|
||||||
|
{
|
||||||
|
rope.tail.second = rope.head.second-1;
|
||||||
|
}
|
||||||
|
else if (rope.head.second < rope.tail.second)
|
||||||
|
{
|
||||||
|
rope.tail.second = rope.head.second+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (rope.head.second == rope.tail.second)
|
||||||
|
{ //same y
|
||||||
|
if (rope.head.first > rope.tail.first)
|
||||||
|
{
|
||||||
|
rope.tail.first = rope.head.first-1;
|
||||||
|
}
|
||||||
|
else if (rope.head.first < rope.tail.first)
|
||||||
|
{
|
||||||
|
rope.tail.first = rope.head.first+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ //diagonal
|
||||||
|
if (abs(rope.head.first-rope.tail.first) == 1 && abs(rope.head.second-rope.tail.second) == 1)
|
||||||
|
{
|
||||||
|
return rope;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rope.head.first-rope.tail.first > 0)
|
||||||
|
{
|
||||||
|
rope.tail.first++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rope.tail.first--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rope.head.second-rope.tail.second > 0)
|
||||||
|
{
|
||||||
|
rope.tail.second++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rope.tail.second--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rope;
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void move_rope(pair<int, int>* head, pair<int, int>* tail);
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
vector<pair<int,int>> rope;
|
||||||
|
for(int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
rope.push_back({0,0});
|
||||||
|
}
|
||||||
|
|
||||||
|
set<pair<int, int>> visited;
|
||||||
|
|
||||||
|
string line;
|
||||||
|
while(getline(cin, line))
|
||||||
|
{
|
||||||
|
char dir = line[0];
|
||||||
|
int amount = stoi(line.substr(2));
|
||||||
|
int dx = (dir=='R')?1:((dir=='L')?-1:0);
|
||||||
|
int dy = (dir=='U')?1:((dir=='D')?-1:0);
|
||||||
|
|
||||||
|
for(int i = 0; i < amount; i++)
|
||||||
|
{
|
||||||
|
rope[0].first += dx;
|
||||||
|
rope[0].second += dy;
|
||||||
|
|
||||||
|
vector<pair<int,int>>::iterator knot1 = rope.begin();
|
||||||
|
vector<pair<int,int>>::iterator knot2 = rope.begin()+1;
|
||||||
|
for(int j = 0; j < rope.size(); j++)
|
||||||
|
{
|
||||||
|
move_rope(&(*knot1), &(*knot2));
|
||||||
|
++knot1;
|
||||||
|
++knot2;
|
||||||
|
}
|
||||||
|
visited.insert(rope.back());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << visited.size() << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_rope(pair<int, int>* head, pair<int, int>* tail)
|
||||||
|
{
|
||||||
|
if (head->first == tail->first)
|
||||||
|
{ //same x
|
||||||
|
if (head->second > tail->second)
|
||||||
|
{
|
||||||
|
tail->second = head->second-1;
|
||||||
|
}
|
||||||
|
else if (head->second < tail->second)
|
||||||
|
{
|
||||||
|
tail->second = head->second+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (head->second == tail->second)
|
||||||
|
{ //same y
|
||||||
|
if (head->first > tail->first)
|
||||||
|
{
|
||||||
|
tail->first = head->first-1;
|
||||||
|
}
|
||||||
|
else if (head->first < tail->first)
|
||||||
|
{
|
||||||
|
tail->first = head->first+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ //diagonal
|
||||||
|
if (abs(head->first-tail->first) == 1 && abs(head->second-tail->second) == 1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (head->first-tail->first > 0)
|
||||||
|
{
|
||||||
|
tail->first++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tail->first--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (head->second-tail->second > 0)
|
||||||
|
{
|
||||||
|
tail->second++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tail->second--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,6 @@
|
|||||||
|
|
||||||
Advent of Code for 2022
|
Advent of Code for 2022
|
||||||
|
|
||||||
## How to contribute
|
# Branch format
|
||||||
|
|
||||||
To contribute to the Advent of Code 2022, please pull master and create a branch with your name.
|
Uploading code files at partX.cpp in each day's folder. Compile the part with `g++ partX.cpp -o partX`. Download your input file as `inputX` and run the program with `./partX < inputX`.
|
||||||
|
|
||||||
Recommended folder structure is `{Day}\{Language}\{Part}`
|
|
||||||
|
|
||||||
## Tips
|
|
||||||
|
|
||||||
It is recommended to include small document on how the program works and how to compile & run the program.
|
|
||||||
Reference in New Issue
Block a user