This commit is contained in:
zv0n 2022-12-04 07:39:57 +01:00
parent acbac89af1
commit 5fbee811d9
3 changed files with 1072 additions and 0 deletions

16
2022/04/Makefile Normal file
View File

@ -0,0 +1,16 @@
CXX ?= c++
CXXFLAGS ?= -std=c++11 -Wall -Wextra -pedantic -O2
PROJECT = overlaps
all: ${PROJECT}
${PROJECT}: main.cpp
${CXX} ${CXXFLAGS} -o $@ $^
test: ${PROJECT}
./${PROJECT}
clean:
${RM} *.o ${PROJECT}
.PHONY: all clean test

1000
2022/04/input Normal file

File diff suppressed because it is too large Load Diff

56
2022/04/main.cpp Normal file
View File

@ -0,0 +1,56 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using ElfPair = std::pair<std::pair<int, int>, std::pair<int, int>>;
std::vector<ElfPair> getElfPairs( std::ifstream &file ) {
std::vector<ElfPair> ret{};
char tmp_c = 0;
std::string str;
while ( std::getline( file, str ) ) {
ret.emplace_back();
std::stringstream ss( str );
ss >> ret.back().first.first;
ss >> tmp_c;
ss >> ret.back().first.second;
ss >> tmp_c;
ss >> ret.back().second.first;
ss >> tmp_c;
ss >> ret.back().second.second;
}
return ret;
}
int getFullyContainedPairs(std::vector<ElfPair> &pairs) {
int overlaps = 0;
for(auto &pair : pairs) {
if((pair.first.first >= pair.second.first && pair.first.second <= pair.second.second) ||
(pair.second.first >= pair.first.first && pair.second.second <= pair.first.second)) {
overlaps++;
}
}
return overlaps;
}
int getOverlappingPairs(std::vector<ElfPair> &pairs) {
int overlaps = 0;
for(auto &pair : pairs) {
if((pair.first.first >= pair.second.first && pair.first.first <= pair.second.second) ||
(pair.second.first >= pair.first.first && pair.second.first <= pair.first.second)) {
overlaps++;
}
}
return overlaps;
}
int main() {
std::ifstream input_file( "input" );
auto pairs = getElfPairs( input_file );
int part1 = getFullyContainedPairs(pairs);
std::cout << "There are \033[91;1m" << part1
<< "\033[0m pairs that fully contain other elf's work." << std::endl;
int part2 = getOverlappingPairs(pairs);
std::cout << "There are \033[91;1m" << part2
<< "\033[0m pairs with overlapping work." << std::endl;
}