Compare commits
4 Commits
96775c800b
...
07af61effb
Author | SHA1 | Date | |
---|---|---|---|
07af61effb | |||
63a8929425 | |||
7f4d2d5acb | |||
deafcf3662 |
69
day2/day.cpp
69
day2/day.cpp
@ -1,16 +1,13 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <errno.h>
|
||||||
#include <string>
|
#include <fstream>
|
||||||
#include <vector>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <vector>
|
||||||
#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);
|
||||||
std::vector<std::string> words;
|
std::vector<std::string> words;
|
||||||
std::string word;
|
std::string word;
|
||||||
@ -23,14 +20,11 @@ std::vector<std::string> splitString(const std::string& str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::ifstream inputfile("example");
|
std::ifstream inputfile("input");
|
||||||
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;
|
||||||
@ -38,58 +32,49 @@ 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) {
|
|
||||||
return std::stoi(s);
|
// 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;
|
bool safe = true;
|
||||||
int old_n = -1;
|
for (size_t i = 1; i < num.size(); ++i) {
|
||||||
for (int n : num) {
|
int diff = num[i - 1] - num[i];
|
||||||
if(old_n == -1) {
|
if (diff < 1 || diff > 3) {
|
||||||
old_n = n;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ((old_n - n) > 2 || (old_n - n) > 0) {
|
|
||||||
safe = false;
|
safe = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
old_n = n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (safe) {
|
if (safe) {
|
||||||
res++;
|
res++;
|
||||||
|
std::clog << line << std::endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// increasing
|
||||||
safe = true;
|
safe = true;
|
||||||
old_n = -1;
|
for (size_t i = 1; i < num.size(); ++i) {
|
||||||
for (int n : num) {
|
int diff = num[i] - num[i - 1];
|
||||||
if(old_n == -1) {
|
if (diff < 1 || diff > 3) {
|
||||||
old_n = n;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ((old_n - n) <= -3 && (old_n - n) < 0) {
|
|
||||||
safe = false;
|
safe = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
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;
|
||||||
}
|
}
|
83
day2/second.cpp
Normal file
83
day2/second.cpp
Normal 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
23
day3/day.py
Normal 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
2
day3/parser-second.sh
Executable 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
2
day3/parser.sh
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
grep -o 'mul([0-9]*[0-9],[0-9]*[0-9])' $1
|
Loading…
x
Reference in New Issue
Block a user