From f909300c3619503a25475d46a53664dbee0b0432 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sat, 8 Dec 2018 23:31:29 +0100 Subject: [PATCH] Add Day7A.cpp and day 8 --- Day7/Day7A.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ Day8/Day8A.cpp | 39 +++++++++++++++++++++++++++++ Day8/Day8A.py | 10 ++++++++ Day8/Day8B.cpp | 48 ++++++++++++++++++++++++++++++++++++ Day8/Day8B.py | 18 ++++++++++++++ 5 files changed, 182 insertions(+) create mode 100644 Day7/Day7A.cpp create mode 100644 Day8/Day8A.cpp create mode 100644 Day8/Day8A.py create mode 100644 Day8/Day8B.cpp create mode 100644 Day8/Day8B.py diff --git a/Day7/Day7A.cpp b/Day7/Day7A.cpp new file mode 100644 index 0000000..577fa65 --- /dev/null +++ b/Day7/Day7A.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include + +#include + +typedef std::unordered_multimap graph_type; + +std::tuple> build_graph() +{ + graph_type graph; + std::ifstream file("Day7.in"); + std::string line; + + std::set 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 &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 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; +} diff --git a/Day8/Day8A.cpp b/Day8/Day8A.cpp new file mode 100644 index 0000000..f03e994 --- /dev/null +++ b/Day8/Day8A.cpp @@ -0,0 +1,39 @@ +#include +#include + +std::vector read_input() +{ + std::vector input; + input.reserve(20000); + + int i = 0; + while (std::cin >> i) { + input.push_back(i); + } + + return input; +} + +int read_node(std::vector::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 input = read_input(); + auto iterator = input.begin(); + std::cout << read_node(iterator) << std::endl; +} diff --git a/Day8/Day8A.py b/Day8/Day8A.py new file mode 100644 index 0000000..a4fad1f --- /dev/null +++ b/Day8/Day8A.py @@ -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)) diff --git a/Day8/Day8B.cpp b/Day8/Day8B.cpp new file mode 100644 index 0000000..b87caf1 --- /dev/null +++ b/Day8/Day8B.cpp @@ -0,0 +1,48 @@ +#include +#include + +std::vector read_input() +{ + std::vector input; + input.reserve(20000); + + int i = 0; + while (std::cin >> i) + input.push_back(i); + + return input; +} + +int read_node(std::vector::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 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 input = read_input(); + auto iterator = input.begin(); + std::cout << read_node(iterator) << std::endl; +} diff --git a/Day8/Day8B.py b/Day8/Day8B.py new file mode 100644 index 0000000..46e133b --- /dev/null +++ b/Day8/Day8B.py @@ -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))