04
This commit is contained in:
parent
acbac89af1
commit
5fbee811d9
16
2022/04/Makefile
Normal file
16
2022/04/Makefile
Normal 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
1000
2022/04/input
Normal file
File diff suppressed because it is too large
Load Diff
56
2022/04/main.cpp
Normal file
56
2022/04/main.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user