Completed Day4 - Very happy with the solution, data ingest could be a little better
This commit is contained in:
parent
2cf4f06dd0
commit
7d0be1fec7
1000
input/day04
1000
input/day04
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
@ -1,11 +1,108 @@
|
||||
#include "../aoc2022.h"
|
||||
|
||||
enum EOverlapType {
|
||||
eContains,
|
||||
eOverlap
|
||||
};
|
||||
|
||||
struct SBound {
|
||||
int lower, upper;
|
||||
|
||||
SBound(const std::string &input) {
|
||||
std::vector<std::string> bounds;
|
||||
std::string temp;
|
||||
|
||||
for (int i = 0; i < input.size(); i++)
|
||||
{
|
||||
if (input[i] == '-')
|
||||
{//Check if we are going onto the upper bound
|
||||
bounds.push_back(temp);
|
||||
temp = "";
|
||||
continue;
|
||||
}
|
||||
temp += input[i];
|
||||
}
|
||||
this->lower = std::stoi(bounds[0]);
|
||||
this->upper = std::stoi(temp);
|
||||
}
|
||||
|
||||
//Check if the bounds contains another
|
||||
bool contains(const SBound &bound) {
|
||||
if (bound.lower >= this->lower && bound.upper <= this->upper)
|
||||
return true;
|
||||
if (this->lower >= bound.lower && this->upper <= bound.upper)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
//Check if the 2 bounds overlap eachother
|
||||
bool overlaps(const SBound& bound) {
|
||||
if (this->upper >= bound.lower && this->lower <= bound.upper)
|
||||
return true;
|
||||
if (this->lower <= bound.upper && this->upper >= bound.lower)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Output>
|
||||
struct SDay04 : public SDay<Output> {
|
||||
//Class constructor
|
||||
using SDay<Output>::SDay;
|
||||
|
||||
int readBounds(EOverlapType overlapType) {
|
||||
this->input.clear();
|
||||
this->input.seekg(0, std::ios::beg);
|
||||
std::string line{ "" };
|
||||
int total{ 0 };
|
||||
|
||||
while (std::getline(this->input, line))
|
||||
{
|
||||
std::vector<SBound> bounds = getBounds(line);
|
||||
|
||||
if (overlapType == eContains)
|
||||
{//Check if the bounds contain eachother
|
||||
if (bounds[0].contains(bounds[1]))
|
||||
total++;
|
||||
}
|
||||
else
|
||||
{//Check if the bounds overlap
|
||||
if (bounds[0].overlaps(bounds[1]))
|
||||
total++;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
//Takes the input string and should return 2 sets of bounds
|
||||
std::vector<SBound> getBounds(const std::string &input) {
|
||||
std::vector<SBound> bounds;
|
||||
std::string bound1, bound2;
|
||||
bool split{ false };
|
||||
|
||||
for (int i = 0; i < input.size(); i++)
|
||||
{
|
||||
if (input[i] == ',')
|
||||
{//Check if we are going onto the second bound
|
||||
split = true;
|
||||
continue;
|
||||
}
|
||||
if (!split)
|
||||
{//First Bound
|
||||
bound1 += input[i];
|
||||
}
|
||||
else
|
||||
{//Second Bound
|
||||
bound2 += input[i];
|
||||
}
|
||||
}
|
||||
bounds.push_back(SBound(bound1));
|
||||
bounds.push_back(SBound(bound2));
|
||||
|
||||
return bounds;
|
||||
}
|
||||
|
||||
//Solutions to both days
|
||||
Output part1() override final { return Output(); }
|
||||
Output part2() override final { return Output(); }
|
||||
Output part1() override final { return readBounds(eContains); }
|
||||
Output part2() override final { return readBounds(eOverlap); }
|
||||
};
|
Loading…
Reference in New Issue
Block a user