Skip to content

Commit

Permalink
solve(day 2): Red nosed reports part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-xero committed Dec 2, 2024
1 parent 9b06409 commit 2378688
Show file tree
Hide file tree
Showing 4 changed files with 1,093 additions and 3 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Specify the compiler
CXX = g++

# Compilation flags
CXXFLAGS = -std=c++20 -Wall -O2

# Target to compile and run a specific file
Expand Down
87 changes: 87 additions & 0 deletions day-2/01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <bits/stdc++.h>

using namespace std;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);

string filename = "";

// Prompt for filename
cout << "Input File: ";
cout.flush();

// Read in file name
getline(cin, filename);

// Read input from file
ifstream inputFile("day-2/" + filename);
if (!inputFile.is_open()) {
cerr << "Unable to read file\n";

return 1;
}

// We are reading an n x m input list
string line = "";
int safeLevels = 0;

while (getline(inputFile, line)) {
vector<int> levels;
stringstream stream(line);

int num;

// Read each num into the vector
while (stream >> num) {
levels.push_back(num);
}

int isInc = 0; // false by default

uint lPointer = 0;
uint rPointer = 1;

for (int num : levels) {
cout << num << ' ';
}

cout << '\n';

// We know each level has m numbers
while (rPointer < levels.size()) {
int diff = levels.at(lPointer) - levels.at(rPointer);

// If in the first iteration and diff is positive, means we're
// increasing
if (lPointer == 0) {
isInc = diff > 0;
}

// If we've determined it to be increasing, but diff shows
// otherwise, means this level will never be safe.
// Same for decreasing.
if ((isInc && diff < 0) || (!isInc && diff > 0)) {
break;
}

// If the diff is not in the range (1 - 3), it is unsafe
if (abs(diff) < 1 || abs(diff) > 3) {
break;
}

lPointer++;
rPointer++;
}

// Means the levels stayed safe
if (rPointer == levels.size()) {
safeLevels++;
}
}

cout << "safe levels: " << safeLevels << '\n';

return 0;
}
Loading

0 comments on commit 2378688

Please sign in to comment.