Fixed indentation

main
Benjamin Kraft 9 months ago
parent 3223948f51
commit bfa9abdc5c
  1. 25
      src/days/01/Day01.cpp
  2. 12
      src/days/02/Day02.cpp
  3. 40
      src/days/03/Day03.cpp
  4. 22
      src/days/04/Day04.cpp
  5. 24
      src/days/05/Day05.cpp
  6. 13
      src/days/05/Day05.h
  7. 9
      src/main.cpp
  8. 18
      src/util.cpp
  9. 8
      src/util.h

@ -2,12 +2,13 @@
Result Day01::Task1() {
uint32_t sum = 0;
for (const string& line : input){
uint8_t left = *std::find_if(line.begin(), line.end(), [](const char c){
for (const string &line: input) {
uint8_t left = *std::find_if(line.begin(), line.end(), [](const char c) {
int diff = c - '0';
return diff >= 0 && diff < 10;
}) - '0';
uint8_t right = *std::find_if(std::make_reverse_iterator(line.end()), std::make_reverse_iterator(line.begin()), [](const char c){
uint8_t right = *std::find_if(std::make_reverse_iterator(line.end()), std::make_reverse_iterator(line.begin()),
[](const char c) {
int diff = c - '0';
return diff >= 0 && diff < 10;
}) - '0';
@ -31,20 +32,20 @@ Result Day01::Task2() {
{"nine", '9'},
};
for (const string &line : input){
for (const string &line: input) {
uint8_t left = 0, right = 0;
for (size_t i = 0; i < line.length(); i++){
for (size_t i = 0; i < line.length(); i++) {
auto current = line.substr(0, i + 1);
auto diff = current.back() - '0';
if (diff > 0 && diff < 10){
if (diff > 0 && diff < 10) {
left = diff;
break;
}
bool found = false;
for (const auto& [key, value] : convert){
for (const auto &[key, value]: convert) {
auto pos = current.find(key);
if (pos != string::npos){
if (pos != string::npos) {
left = value - '0';
found = true;
break;
@ -53,17 +54,17 @@ Result Day01::Task2() {
if (found) break;
}
for (size_t i = 0; i < line.length(); i++){
for (size_t i = 0; i < line.length(); i++) {
auto current = line.substr(line.length() - i - 1);
auto diff = current.front() - '0';
if (diff > 0 && diff < 10){
if (diff > 0 && diff < 10) {
right = diff;
break;
}
bool found = false;
for (const auto& [key, value] : convert){
for (const auto &[key, value]: convert) {
auto pos = current.find(key);
if (pos != string::npos){
if (pos != string::npos) {
right = value - '0';
found = true;
break;

@ -7,9 +7,9 @@ Result Day02::Task1() {
uint32_t sum = 0;
uint32_t id = 1;
for (const Game &game : parseGames()){
for (const Game &game: parseGames()) {
bool possible = true;
for (const auto &set : game)
for (const auto &set: game)
if (set[0] > r || set[1] > g || set[2] > b)
possible = false;
if (possible)
@ -22,9 +22,9 @@ Result Day02::Task1() {
Result Day02::Task2() {
uint32_t sum = 0;
for (const Game &game : parseGames()){
for (const Game &game: parseGames()) {
size_t r = 0, g = 0, b = 0;
for (const auto &set : game){
for (const auto &set: game) {
r = std::max(r, set[0]);
g = std::max(g, set[1]);
b = std::max(b, set[2]);
@ -36,14 +36,14 @@ Result Day02::Task2() {
vector<Day02::Game> Day02::parseGames() const {
vector<Game> games;
for (const string &line : input){
for (const string &line: input) {
auto record = line.substr(line.find(": ") + 1);
record.push_back(';');
auto setIndices = findAll(record, ";");
Game game;
size_t start = 0;
for (size_t i : setIndices){
for (size_t i: setIndices) {
auto set = record.substr(start, i - start);
auto findNumber = [set](const string &type) -> size_t {

@ -5,21 +5,21 @@ Result Day03::Task1() {
auto isSymbol = [this](int32_t x, int32_t y) -> bool {
if (x < 0 || x > input[0].length() - 1 ||
y < 0 || y > input.size() - 1){
y < 0 || y > input.size() - 1) {
return false;
}
char c = input[y][x];
return !isDigit(c) && c != '.';
};
for (const auto &[number, position] : parseEngineNumbers()){
for (const auto &[number, position]: parseEngineNumbers()) {
auto &[value, size] = number;
auto &[x, y] = position;
bool symbolFound = false;
for (int32_t sx = x - 1; sx < x + size + 1; sx++){
for (int32_t sy = y - 1; sy < y + 2; sy++){
if (isSymbol(sx, sy)){
for (int32_t sx = x - 1; sx < x + size + 1; sx++) {
for (int32_t sy = y - 1; sy < y + 2; sy++) {
if (isSymbol(sx, sy)) {
symbolFound = true;
break;
}
@ -39,20 +39,20 @@ Result Day03::Task2() {
auto isStar = [this](int32_t x, int32_t y) -> bool {
if (x < 0 || x > input[0].length() - 1 ||
y < 0 || y > input.size() - 1){
y < 0 || y > input.size() - 1) {
return false;
}
return input[y][x] == '*';
};
for (const auto &[number, position] : parseEngineNumbers()){
for (const auto &[number, position]: parseEngineNumbers()) {
auto &[value, size] = number;
auto &[x, y] = position;
for (int32_t sx = x - 1; sx < x + size + 1; sx++){
for (int32_t sy = y - 1; sy < y + 2; sy++){
if (isStar(sx, sy)){
if (stars.contains({sx, sy})){
for (int32_t sx = x - 1; sx < x + size + 1; sx++) {
for (int32_t sy = y - 1; sy < y + 2; sy++) {
if (isStar(sx, sy)) {
if (stars.contains({sx, sy})) {
stars[{sx, sy}].insert({number, position});
} else {
stars[{sx, sy}] = {{number, position}};
@ -62,8 +62,8 @@ Result Day03::Task2() {
}
}
for (auto &[position, numbers] : stars){
if (numbers.size() == 2){
for (auto &[position, numbers]: stars) {
if (numbers.size() == 2) {
sum += numbers.begin()->first.first * (++numbers.begin())->first.first;
}
}
@ -74,23 +74,25 @@ Result Day03::Task2() {
vector<Day03::EngineNumber> Day03::parseEngineNumbers() const {
vector<EngineNumber> numbers;
for (size_t y = 0; y < input.size(); y++){
const string& line = input[y];
for (size_t y = 0; y < input.size(); y++) {
const string &line = input[y];
uint32_t value = 0;
uint8_t valueLength = 0;
bool readingValue = false;
for (size_t x = 0; x < line.length(); x++){
for (size_t x = 0; x < line.length(); x++) {
uint8_t digit;
if (isDigit(line[x], digit)){
if (isDigit(line[x], digit)) {
value = value * 10 + digit;
valueLength++;
readingValue = true;
if (x == line.length() - 1)
numbers.push_back({{value, valueLength}, {x - valueLength, y}});
numbers.push_back({{value, valueLength},
{x - valueLength, y}});
} else {
if (readingValue)
numbers.push_back({{value, valueLength}, {x - valueLength, y}});
numbers.push_back({{value, valueLength},
{x - valueLength, y}});
readingValue = false;
valueLength = 0;
value = 0;

@ -4,10 +4,11 @@
Result Day04::Task1() {
uint64 sum = 0;
for (const auto &[winning, having] : parseCards()){
for (const auto &[winning, having]: parseCards()) {
std::set<uint8_t> result;
std::set_intersection(winning.begin(), winning.end(), having.begin(), having.end(), std::inserter(result, result.begin()));
if (!result.empty()){
std::set_intersection(winning.begin(), winning.end(), having.begin(), having.end(),
std::inserter(result, result.begin()));
if (!result.empty()) {
sum += uint64(powl(2, result.size() - 1));
}
}
@ -18,23 +19,24 @@ Result Day04::Task1() {
Result Day04::Task2() {
vector<uint64> matches;
for (const auto &[winning, having] : parseCards()){
for (const auto &[winning, having]: parseCards()) {
std::set<uint8_t> result;
std::set_intersection(winning.begin(), winning.end(), having.begin(), having.end(), std::inserter(result, result.begin()));
std::set_intersection(winning.begin(), winning.end(), having.begin(), having.end(),
std::inserter(result, result.begin()));
matches.push_back(result.size());
}
vector<uint64> counts(matches.size(), 1);
for (size_t i = 0; i < matches.size(); i++){
for (size_t instance = 0; instance < counts[i]; instance++){
for (size_t k = 0; k < matches[i]; k++){
for (size_t i = 0; i < matches.size(); i++) {
for (size_t instance = 0; instance < counts[i]; instance++) {
for (size_t k = 0; k < matches[i]; k++) {
counts[i + k + 1]++;
}
}
}
uint64 sum = std::reduce(counts.begin(), counts.end(), 0, [](uint64 a, uint64 b){return a + b;});
uint64 sum = std::reduce(counts.begin(), counts.end(), 0, [](uint64 a, uint64 b) { return a + b; });
return to_string(sum);
}
@ -42,7 +44,7 @@ Result Day04::Task2() {
vector<Day04::Card> Day04::parseCards() const {
vector<Card> cards;
for (string line : input){
for (string line: input) {
line = line.substr(line.find(':') + 1);
string left = line.substr(0, line.find(" |"));
string right = line.substr(line.find(" |") + 2);

@ -6,9 +6,9 @@ Result Day05::Task1() {
uint64 min = UINT64_MAX;
for (uint64 seed : seeds){
for (uint64 seed: seeds) {
uint64 result = seed;
for (const Map &map : maps)
for (const Map &map: maps)
result = map.get(result);
min = std::min(min, result);
}
@ -22,17 +22,17 @@ Result Day05::Task2() {
uint64 min = UINT64_MAX;
for (size_t i = 0; i < seeds.size(); i += 2){
vector<Range> resultRanges {Range(seeds[i], seeds[i] + seeds[i + 1] - 1)};
for (const Map &map : maps){
for (size_t i = 0; i < seeds.size(); i += 2) {
vector<Range> resultRanges{Range(seeds[i], seeds[i] + seeds[i + 1] - 1)};
for (const Map &map: maps) {
vector<Range> newRanges;
for (const Range &range : resultRanges) {
for (const Range &range: resultRanges) {
auto add = map.get(range);
std::copy(add.begin(), add.end(), std::back_inserter(newRanges));
}
resultRanges = newRanges;
}
for (const Range &range : resultRanges)
for (const Range &range: resultRanges)
min = std::min(min, range.start);
}
@ -43,7 +43,7 @@ vector<uint64> Day05::parseSeeds() const {
vector<uint64> seeds;
auto list = (input[0] + " ").substr(7);
size_t start = 0;
for (size_t i : findAll(list, " ")){
for (size_t i: findAll(list, " ")) {
seeds.push_back(std::stoul(list.substr(start, i - start)));
start = i;
}
@ -56,9 +56,9 @@ std::array<Day05::Map, 7> Day05::parseMaps() const {
size_t map = 0;
for (size_t i = 3; i < input.size(); i++){
for (size_t i = 3; i < input.size(); i++) {
string line = input[i];
if (line.empty()){
if (line.empty()) {
i++;
map++;
continue;
@ -91,7 +91,7 @@ bool Day05::Mapping::touchesRange(Day05::Range src) const {
uint64 Day05::Map::get(uint64 src) const {
for (const Mapping &mapping : mappings)
for (const Mapping &mapping: mappings)
if (mapping.containsKey(src))
return mapping.get(src);
return src;
@ -109,7 +109,7 @@ vector<Day05::Range> Day05::Map::get(Day05::Range src) const {
if (src.end > maxSource)
ranges.emplace_back(maxSource + 1, src.end);
for (const Mapping &mapping : mappings)
for (const Mapping &mapping: mappings)
if (mapping.touchesRange(src))
ranges.emplace_back(mapping.get(src));

@ -7,26 +7,37 @@ class Day05 : public Day {
uint64 start;
uint64 end;
};
struct Mapping {
uint64 destination;
uint64 source;
uint64 range;
bool containsKey(uint64 src) const;
uint64 get(uint64 src) const;
bool touchesRange(Range src) const;
Range get(Range src) const;
bool operator <(const Mapping &other) const;
bool operator<(const Mapping &other) const;
};
struct Map {
set<Mapping> mappings;
uint64 get(uint64 src) const;
vector<Range> get(Range src) const;
};
protected:
Result Task1() override;
Result Task2() override;
vector<uint64> parseSeeds() const;
std::array<Map, 7> parseMaps() const;
};

@ -14,8 +14,8 @@ Input getInput(int day, string key, bool useTestInput, int testFetchIndex) {
string dayStr = std::to_string(day);
string url = "https://adventofcode.com/2023/day/" + dayStr;
auto cookies = cpr::Cookies{{"session", key}};
if (!useTestInput){
if (key.empty()){
if (!useTestInput) {
if (key.empty()) {
cout << "Session key Cookie is missing, cannot fetch Input" << endl;
return input;
}
@ -25,7 +25,8 @@ Input getInput(int day, string key, bool useTestInput, int testFetchIndex) {
file << cpr::Get(cpr::Url{url}, cookies).text;
file.close();
} else {
cout << "Test Input does not exist. Fetching from " + url << " from <code>-Tag with index " << testFetchIndex << endl;
cout << "Test Input does not exist. Fetching from " + url << " from <code>-Tag with index "
<< testFetchIndex << endl;
std::ofstream file(localFilePath);
string res = cpr::Get(cpr::Url{url}).text;
size_t i1 = findAll(res, "<code>")[testFetchIndex];
@ -59,7 +60,7 @@ string getSessionKey() {
return key;
}
void parseArgument(const string& arg, int &dayNum, bool &useTestInput, int &testFetchIndex){
void parseArgument(const string &arg, int &dayNum, bool &useTestInput, int &testFetchIndex) {
size_t tIndex = arg.find('T');
if (tIndex == string::npos) {
dayNum = stoi(arg);

@ -1,11 +1,11 @@
#include "util.h"
vector<size_t> findAll(const string& data, const string& toSearch){
vector<size_t> findAll(const string &data, const string &toSearch) {
vector<size_t> indices;
size_t pos = data.find(toSearch);
while (pos != string::npos){
while (pos != string::npos) {
indices.push_back(pos);
pos = data.find(toSearch, pos + toSearch.size());
@ -14,17 +14,17 @@ vector<size_t> findAll(const string& data, const string& toSearch){
return indices;
}
void removeAll(string& data, const string& toRemove) {
void removeAll(string &data, const string &toRemove) {
vector<size_t> indices = findAll(data, toRemove);
std::sort(indices.rbegin(), indices.rend());
for (size_t &i : indices)
for (size_t &i: indices)
data.erase(i, toRemove.size());
}
vector<string> split(const string& data, const char c){
vector<string> split(const string &data, const char c) {
vector<string> parts;
size_t start = 0;
for (size_t i : findAll(data, std::string(1, c))){
for (size_t i: findAll(data, std::string(1, c))) {
parts.push_back(data.substr(start, i - start));
start = i + 1;
}
@ -32,16 +32,16 @@ vector<string> split(const string& data, const char c){
return parts;
}
bool isDigit(char c, uint8_t& result){
bool isDigit(char c, uint8_t &result) {
auto val = c - '0';
if (val >= 0 && val < 10){
if (val >= 0 && val < 10) {
result = val;
return true;
}
return false;
}
bool isDigit(char c){
bool isDigit(char c) {
auto val = c - '0';
return val >= 0 && val < 10;
}

@ -20,12 +20,12 @@ using std::string, std::vector, std::pair, std::map;
using std::list, std::set, std::unordered_set;
using std::priority_queue;
vector<size_t> findAll(const string& data, const string& toSearch);
vector<size_t> findAll(const string &data, const string &toSearch);
void removeAll(string& data, const string& toRemove);
void removeAll(string &data, const string &toRemove);
vector<string> split(const string& data, char c);
vector<string> split(const string &data, char c);
bool isDigit(char c, uint8_t& result);
bool isDigit(char c, uint8_t &result);
bool isDigit(char c);
Loading…
Cancel
Save