Compare commits

...

4 Commits

Author SHA1 Message Date
07af61effb day3 done 2024-12-12 22:11:26 +01:00
63a8929425 second also done chatgpt helped 2024-12-12 21:32:28 +01:00
7f4d2d5acb day2 part one done 2024-12-12 21:31:11 +01:00
deafcf3662 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2024-12-12 17:29:05 +01:00
5 changed files with 174 additions and 79 deletions

View File

@ -1,95 +1,80 @@
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream>
#include <errno.h>
#include <vector>
std::vector<std::string> splitString(const std::string& str) {
std::istringstream iss(str);
std::vector<std::string> words;
std::string word;
std::vector<std::string> splitString(const std::string &str) {
std::istringstream iss(str);
std::vector<std::string> words;
std::string word;
while (iss >> word) {
words.push_back(word);
}
while (iss >> word) {
words.push_back(word);
}
return words;
return words;
}
int main() {
std::ifstream inputfile("example");
std::string input;
int res = 0;
if (inputfile.is_open()) {
std::ifstream inputfile("input");
std::string input;
// Read each line from the file and store it in the
// 'line' variable.
std::string line;
std::vector<std::string> num_s;
std::vector<int> num;
while (std::getline(inputfile, line)) {
num_s = splitString(line);
num.clear();
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), [](const std::string& s) {
return std::stoi(s);
});
bool safe = true;
int old_n = -1;
for (int n : num) {
if(old_n == -1) {
old_n = n;
continue;
}
if ((old_n - n) > 2 || (old_n - n) > 0) {
safe = false;
}
old_n = n;
}
if (safe) {
res++;
continue;
}
safe = true;
old_n = -1;
for (int n : num) {
if(old_n == -1) {
old_n = n;
continue;
}
if ((old_n - n) <= -3 && (old_n - n) < 0) {
safe = false;
}
old_n = n;
}
if (safe) {
res++;
}
}
int res = 0;
if (inputfile.is_open()) {
std::string line;
std::vector<std::string> num_s;
std::vector<int> num;
while (std::getline(inputfile, line)) {
num_s = splitString(line);
num.clear();
// Close the file stream once all lines have been
// read.
inputfile.close();
}
else {
// Print an error message to the standard error
// stream if the file cannot be opened.
std::cerr << "Unable to open file!" << std::endl;
return ENOENT;
// Convert strings to integers
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
[](const std::string &s) { return std::stoi(s); });
// decreasing
bool safe = true;
for (size_t i = 1; i < num.size(); ++i) {
int diff = num[i - 1] - num[i];
if (diff < 1 || diff > 3) {
safe = false;
break;
}
}
if (safe) {
res++;
std::clog << line << std::endl;
continue;
}
// increasing
safe = true;
for (size_t i = 1; i < num.size(); ++i) {
int diff = num[i] - num[i - 1];
if (diff < 1 || diff > 3) {
safe = false;
break;
}
}
if (safe) {
std::clog << line << std::endl;
res++;
}
}
std::cout << "\n\n" << res << "\n\n";
return 0;
}
inputfile.close();
} else {
std::cerr << "Unable to open file!" << std::endl;
return ENOENT;
}
std::cout << res << std::endl;
return 0;
}

83
day2/second.cpp Normal file
View File

@ -0,0 +1,83 @@
#include <algorithm>
#include <cstddef>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
std::vector<std::string> splitString(const std::string &str) {
std::istringstream iss(str);
std::vector<std::string> words;
std::string word;
while (iss >> word) {
words.push_back(word);
}
return words;
}
bool isSafe(const std::vector<int> &num, bool checkIncreasing) {
for (size_t i = 1; i < num.size(); ++i) {
int diff = checkIncreasing ? (num[i] - num[i - 1]) : (num[i - 1] - num[i]);
if (diff < 1 || diff > 3) {
return false;
}
}
return true;
}
bool isSafeWithDampener(const std::vector<int> &num) {
// Check if already safe
if (isSafe(num, false) || isSafe(num, true)) {
return true;
}
// Try removing each level and check for safety
for (size_t i = 0; i < num.size(); ++i) {
std::vector<int> temp = num;
temp.erase(temp.begin() + i); // Remove the current level
if (isSafe(temp, false) || isSafe(temp, true)) {
return true;
}
}
return false;
}
int main() {
std::ifstream inputfile("input");
std::string input;
int res = 0; // Counter for safe reports
if (inputfile.is_open()) {
std::string line;
std::vector<std::string> num_s;
std::vector<int> num;
while (std::getline(inputfile, line)) {
num_s = splitString(line);
num.clear();
// Convert strings to integers
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
[](const std::string &s) { return std::stoi(s); });
// Check safety with Problem Dampener support
if (isSafeWithDampener(num)) {
res++;
std::clog << line << std::endl;
}
}
inputfile.close();
} else {
std::cerr << "Unable to open file!" << std::endl;
return ENOENT;
}
std::cout << res << std::endl;
return 0;
}

23
day3/day.py Normal file
View File

@ -0,0 +1,23 @@
res = 0
write = True
def mul(a, b):
global write
if write:
global res
res += (a * b)
def do():
global write
write = True
def dont():
global write
write = False
with open('parsed_sec', 'r') as file:
# Read each line in the file
for line in file:
# Print each line
exec(line)
print(res)

2
day3/parser-second.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
grep -o "mul([0-9]*[0-9],[0-9]*[0-9])\|do()\|don't()" $1 | tr -d "'"

2
day3/parser.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
grep -o 'mul([0-9]*[0-9],[0-9]*[0-9])' $1