Add Day7A.cpp and day 8

This commit is contained in:
Sijmen 2018-12-08 23:31:29 +01:00
parent 2992dd60b2
commit f909300c36
5 changed files with 182 additions and 0 deletions

67
Day7/Day7A.cpp Normal file
View file

@ -0,0 +1,67 @@
#include <iostream>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <set>
#include <tuple>
typedef std::unordered_multimap<char, char> graph_type;
std::tuple<graph_type, std::set<char>> build_graph()
{
graph_type graph;
std::ifstream file("Day7.in");
std::string line;
std::set<char> nodes;
while (std::getline(file, line)) {
char a = line[5];
char b = line[36];
std::cout << "a: " << a << ", b:" << b << std::endl;
graph.insert({ b, a });
nodes.insert(a);
nodes.insert(b);
}
return std::make_tuple(graph, nodes);
}
bool node_blocked(graph_type &graph, std::set<char> &to_consider, char node)
{
auto [start, end] = graph.equal_range(node);
for (auto pair = start; pair != end; ++pair) {
char parent = pair->second;
if (to_consider.count(parent) > 0) {
return true;
}
}
return false;
}
int main(int argc, char **argv)
{
auto [graph, to_consider] = build_graph();
std::vector<char> order;
while (!to_consider.empty()) {
for (auto node = to_consider.begin(); node != to_consider.end(); ++node) {
bool blocked = node_blocked(graph, to_consider, *node);
if (blocked)
continue;
order.push_back(*node);
std::cout << *node;
to_consider.erase(node);
break;
}
}
std::cout << std::endl;
return 0;
}

39
Day8/Day8A.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
std::vector<int> read_input()
{
std::vector<int> input;
input.reserve(20000);
int i = 0;
while (std::cin >> i) {
input.push_back(i);
}
return input;
}
int read_node(std::vector<int>::iterator &it)
{
int child_count = *it++;
int meta_count = *it++;
int value = 0;
for (int i = 0; i < child_count; i++) {
value += read_node(it);
}
for (int i = 0; i < meta_count; i++) {
value += *it++;
}
return value;
}
int main(int argc, char **argv)
{
std::vector<int> input = read_input();
auto iterator = input.begin();
std::cout << read_node(iterator) << std::endl;
}

10
Day8/Day8A.py Normal file
View file

@ -0,0 +1,10 @@
def read_node(it):
child_count, meta_count = next(it), next(it)
child_sum = sum(read_node(it) for _ in range(child_count))
own_sum = sum(next(it) for _ in range(meta_count))
return child_sum + own_sum
it = (int(x) for x in input().split())
print(read_node(it))

48
Day8/Day8B.cpp Normal file
View file

@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
std::vector<int> read_input()
{
std::vector<int> input;
input.reserve(20000);
int i = 0;
while (std::cin >> i)
input.push_back(i);
return input;
}
int read_node(std::vector<int>::iterator &it)
{
int child_count = *it++;
int meta_count = *it++;
int value = 0;
if (child_count == 0) {
for (int i = 0; i < meta_count; i++)
value += *it++;
return value;
}
std::vector<int> children;
for (int i = 0; i < child_count; i++) {
children.push_back(read_node(it));
}
for (int i = 0; i < meta_count; i++) {
int index = *it++;
if (index > 0 && index <= children.size())
value += children[index - 1];
}
return value;
}
int main(int argc, char **argv)
{
std::vector<int> input = read_input();
auto iterator = input.begin();
std::cout << read_node(iterator) << std::endl;
}

18
Day8/Day8B.py Normal file
View file

@ -0,0 +1,18 @@
def read_node(it):
child_count, meta_count = next(it), next(it)
if child_count == 0:
return sum(next(it) for _ in range(meta_count))
children = [read_node(it) for _ in range(child_count)]
value = 0
for _ in range(meta_count):
index = next(it)
if index > 0 and index <= len(children):
value += children[index - 1]
return value
it = (int(x) for x in input().split())
print(read_node(it))