diff --git a/day6/Makefile b/day6/Makefile new file mode 100644 index 0000000..fcea070 --- /dev/null +++ b/day6/Makefile @@ -0,0 +1,15 @@ +CC=g++ +BIN_NAME=day6 +CCFLAGS=-o $(BIN_NAME) -g +LDFLAGS=-std=c++17 + +.PHONY: all, clean + +all: $(BIN_NAME) + +clean: + rm -f $(BIN_NAME) + +$(BIN_NAME): day6.cpp + $(CC) $(CCFLAGS) $(LDFLAGS) day6.cpp + diff --git a/day6/day6.cpp b/day6/day6.cpp new file mode 100644 index 0000000..378353f --- /dev/null +++ b/day6/day6.cpp @@ -0,0 +1,90 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +std::vector read_input(const std::string &filename) { + std::vector input; + std::string line; + std::ifstream file(filename); + while (std::getline(file, line)) { + input.push_back(line); + } + + return input; +} + +int part1(const std::vector &input) { + int total = 0; + std::set groups; + for (std::string_view line : input) { + if (line.length() == 0) { + total += groups.size(); + groups.clear(); + continue; + } + + for (char item : line) { + groups.insert(item); + } + } + + return total; +} + +int part2(const std::vector &input) { + int total = 0; + std::set commonAnswers; + for (char i = 'a'; i <= 'z'; i++) { + commonAnswers.insert(i); + } + + for (std::string_view line : input) { + if (line.length() == 0) { + total += commonAnswers.size(); + commonAnswers.clear(); + for (char i = 'a'; i <= 'z'; i++) { + commonAnswers.insert(i); + } + continue; + } + + std::set personAnswers; + for (char item : line) { + personAnswers.insert(item); + } + + std::set intersection; + std::set_intersection( + personAnswers.begin(), + personAnswers.end(), + commonAnswers.begin(), + commonAnswers.end(), + std::inserter(intersection, intersection.begin())); + + std::string out; + folly::join(" ", commonAnswers.begin(), commonAnswers.end(), out); + std::cout << out << std::endl; + commonAnswers = std::move(intersection); + } + + return total; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + std::cerr << argv[0] << " " << std::endl; + return 1; + } + + auto input = read_input(argv[1]); + input.push_back(""); + std::cout << part1(input) << std::endl; + std::cout << part2(input) << std::endl; +}