Skip to content

Commit d2eb14e

Browse files
committed
added day 5
1 parent 86d2f18 commit d2eb14e

File tree

7 files changed

+1542
-0
lines changed

7 files changed

+1542
-0
lines changed

2024/05/.init

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
####init_done####

2024/05/c++/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/cmake-build-debug
2+
/.idea

2024/05/c++/main.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <unordered_set>
4+
#include <vector>
5+
#include "utils.h"
6+
7+
class Container {
8+
public:
9+
std::vector<int> data;
10+
bool valid;
11+
};
12+
13+
Container trialRun(std::vector<int>& input, const std::unordered_map<int, std::vector<int>>& rules) {
14+
std::vector<int> passedItems;
15+
bool valid = true;
16+
for (int i = 0; i < input.size(); i++) {
17+
auto item = input[i];
18+
if (rules.contains(item)) {
19+
std::vector<int> rulesForItem = rules.find(item)->second;
20+
for (auto& alreadyFoundItem : rulesForItem) {
21+
for (const auto& passedItem : passedItems) {
22+
if (passedItem == alreadyFoundItem) {
23+
valid = false;
24+
25+
for (int j = 0; j <= i; j++) {
26+
if (input[j] == passedItem) {
27+
passedItems.insert(passedItems.begin() + j, item);
28+
break;
29+
}
30+
}
31+
for (int j = i+1; j < input.size(); j++) {
32+
passedItems.push_back(input[j]);
33+
}
34+
return Container(passedItems, valid);
35+
}
36+
}
37+
}
38+
}
39+
passedItems.push_back(item);
40+
}
41+
return Container(passedItems, valid);
42+
}
43+
44+
int getAnswer(std::vector<std::vector<int>> input, const std::unordered_map<int, std::vector<int>>& rules, bool partA) {
45+
int sum = 0;
46+
47+
for (auto& inputItem : input) {
48+
bool valid;
49+
do {
50+
auto result = trialRun(inputItem, rules);
51+
valid = result.valid;
52+
53+
if (valid) {
54+
sum += result.data[result.data.size()/2];
55+
} else {
56+
inputItem = result.data;
57+
}
58+
} while (!valid && !partA);
59+
}
60+
61+
return sum;
62+
}
63+
64+
int main() {
65+
const std::vector<std::string> lines = readFile("../input/input");
66+
67+
bool firstParse = true;
68+
std::vector<std::vector<int>> input;
69+
std::unordered_map<int, std::vector<int>> rules;
70+
std::vector<std::string> result;
71+
for (const auto& line : lines) {
72+
if (line.empty()) {
73+
firstParse = false;
74+
continue;
75+
}
76+
if (firstParse) {
77+
result = split(line, '|');
78+
auto num = stoi(result[0]);
79+
if (rules.contains(num)) {
80+
rules.find(num)->second.push_back(stoi(result[1]));
81+
} else {
82+
rules.insert({num, {stoi(result[1])}});
83+
}
84+
} else {
85+
result = split(line, ',');
86+
std::vector<int> inputItem;
87+
for (const auto& item : result) {
88+
inputItem.push_back(std::stoi(item));
89+
}
90+
input.push_back(inputItem);
91+
}
92+
}
93+
94+
int answer = getAnswer(input, rules, true);
95+
std::cout << "Part A: " << answer << std::endl;
96+
std::cout << "Part B: " << getAnswer(input, rules, false) - answer << std::endl;
97+
98+
return 0;
99+
}

2024/05/c++/utils.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Created by Zino Holwerda on 04/12/2024.
3+
//
4+
5+
#include "utils.h"
6+
#include <sstream>
7+
8+
std::vector<std::string> readFile(const std::string& fileName) {
9+
std::vector<std::string> lines;
10+
std::ifstream file(fileName);
11+
12+
if (!file.is_open()) {
13+
std::cerr << "Error opening file " << fileName << std::endl;
14+
throw std::runtime_error("Error opening file");
15+
}
16+
17+
std::string line;
18+
19+
while (std::getline(file, line, '\n')) {
20+
lines.push_back(line);
21+
}
22+
23+
file.close();
24+
return lines;
25+
}
26+
27+
std::vector<std::string> split(const std::string& s, char delim) {
28+
std::vector<std::string> tokens;
29+
std::string token;
30+
std::stringstream ss(s);
31+
while (getline(ss, token, delim)) {
32+
tokens.push_back(token);
33+
}
34+
return tokens;
35+
}

2024/05/c++/utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Created by Zino Holwerda on 04/12/2024.
3+
//
4+
5+
#ifndef UTILS_H
6+
#define UTILS_H
7+
8+
#include <vector>
9+
#include <iostream>
10+
#include <fstream>
11+
12+
std::vector<std::string> readFile(const std::string& fileName);
13+
std::vector<std::string> split(const std::string& line, char delimiter);
14+
15+
#endif //UTILS_H

0 commit comments

Comments
 (0)