Compare commits
No commits in common. "07af61effb2a03025adfe7411a4b61a584a2f734" and "96775c800bad2fc29dbc2e5dbc80e2a4cffe00d0" have entirely different histories.
07af61effb
...
96775c800b
79
day2/day.cpp
79
day2/day.cpp
@ -1,11 +1,14 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <errno.h>
|
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
std::vector<std::string> splitString(const std::string& str) {
|
std::vector<std::string> splitString(const std::string& str) {
|
||||||
std::istringstream iss(str);
|
std::istringstream iss(str);
|
||||||
@ -20,11 +23,14 @@ std::vector<std::string> splitString(const std::string &str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::ifstream inputfile("input");
|
std::ifstream inputfile("example");
|
||||||
std::string input;
|
std::string input;
|
||||||
|
|
||||||
int res = 0;
|
int res = 0;
|
||||||
if (inputfile.is_open()) {
|
if (inputfile.is_open()) {
|
||||||
|
|
||||||
|
// Read each line from the file and store it in the
|
||||||
|
// 'line' variable.
|
||||||
std::string line;
|
std::string line;
|
||||||
std::vector<std::string> num_s;
|
std::vector<std::string> num_s;
|
||||||
std::vector<int> num;
|
std::vector<int> num;
|
||||||
@ -32,49 +38,58 @@ int main() {
|
|||||||
while (std::getline(inputfile, line)) {
|
while (std::getline(inputfile, line)) {
|
||||||
num_s = splitString(line);
|
num_s = splitString(line);
|
||||||
num.clear();
|
num.clear();
|
||||||
|
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), [](const std::string& s) {
|
||||||
// Convert strings to integers
|
return std::stoi(s);
|
||||||
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
|
});
|
||||||
[](const std::string &s) { return std::stoi(s); });
|
|
||||||
|
|
||||||
// decreasing
|
|
||||||
bool safe = true;
|
bool safe = true;
|
||||||
for (size_t i = 1; i < num.size(); ++i) {
|
int old_n = -1;
|
||||||
int diff = num[i - 1] - num[i];
|
for (int n : num) {
|
||||||
if (diff < 1 || diff > 3) {
|
if(old_n == -1) {
|
||||||
safe = false;
|
old_n = n;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (safe) {
|
|
||||||
res++;
|
|
||||||
std::clog << line << std::endl;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if ((old_n - n) > 2 || (old_n - n) > 0) {
|
||||||
// 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;
|
safe = false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
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) {
|
if (safe) {
|
||||||
std::clog << line << std::endl;
|
|
||||||
res++;
|
res++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Close the file stream once all lines have been
|
||||||
|
// read.
|
||||||
inputfile.close();
|
inputfile.close();
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
|
// Print an error message to the standard error
|
||||||
|
// stream if the file cannot be opened.
|
||||||
std::cerr << "Unable to open file!" << std::endl;
|
std::cerr << "Unable to open file!" << std::endl;
|
||||||
return ENOENT;
|
return ENOENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << res << std::endl;
|
|
||||||
|
|
||||||
|
std::cout << "\n\n" << res << "\n\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -1,83 +0,0 @@
|
|||||||
#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
23
day3/day.py
@ -1,23 +0,0 @@
|
|||||||
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)
|
|
@ -1,2 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
grep -o "mul([0-9]*[0-9],[0-9]*[0-9])\|do()\|don't()" $1 | tr -d "'"
|
|
@ -1,2 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
grep -o 'mul([0-9]*[0-9],[0-9]*[0-9])' $1
|
|
Loading…
x
Reference in New Issue
Block a user