-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
311 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int computeFlaggedMuls(const string& line) { | ||
bool isEnabled = true; | ||
int subComputation = 0; | ||
|
||
// We're going to read the substring of each (idx, idx+4) to match a | ||
// `mult(` sequence | ||
for (size_t i = 0; i + 4 <= line.size(); i++) { | ||
// Scan the line for do() / don't() sequences | ||
if (line.substr(i, 4) == "do()") { | ||
cout << "'do' detected at idx: " << i << '\n'; | ||
isEnabled = true; | ||
i += 3; | ||
continue; | ||
} | ||
|
||
if (i + 7 <= line.size() && line.substr(i, 7) == "don't()") { | ||
cout << "'do' detected at idx: " << i << '\n'; | ||
isEnabled = false; | ||
i += 6; | ||
continue; | ||
} | ||
|
||
cout << "enabled: " << isEnabled << '\n'; | ||
|
||
if (line.substr(i, 4) == "mul(") { | ||
int a = 0; | ||
int b = 0; | ||
|
||
// If we can scan out an offset of the line with two integers, | ||
// attempt to compute it | ||
if (sscanf(line.c_str() + i, "mul(%d,%d)", &a, &b) == 2) { | ||
char ch; | ||
|
||
sscanf(line.c_str() + i, "mul(%d,%d%c", &a, &b, &ch); | ||
|
||
if (ch != ')') continue; | ||
|
||
if (isEnabled) subComputation += a * b; | ||
} | ||
} | ||
} | ||
|
||
// This is the result of the mul() computations for this line | ||
return subComputation; | ||
} | ||
|
||
int main() { | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
cout << "Input File: "; | ||
string filename; | ||
|
||
cout.flush(); | ||
getline(cin, filename); | ||
|
||
ifstream inputFile("day-3/" + filename); | ||
|
||
if (!inputFile.is_open()) { | ||
cerr << "Unable to open this file.\n"; | ||
return 1; | ||
} | ||
|
||
// The input is a long string where each line contains some `mul(a,b)` | ||
// instructions | ||
string line; | ||
int computation = 0; | ||
|
||
while (getline(inputFile, line)) { | ||
computation += computeFlaggedMuls(line); | ||
} | ||
|
||
cout << "computed flagged muls: " << computation << '\n'; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int rowDir[] = {0, 1, 0, -1, 1, 1, -1, -1}; | ||
int colDir[] = {1, 0, -1, 0, 1, -1, 1, -1}; | ||
|
||
bool isSafe(int x, int y, int rows, int cols) { | ||
return x >= 0 && x < rows && y >= 0 && y < cols; | ||
} | ||
|
||
int countXMAS(const vector<string>& grid, const string& word = "XMAS") { | ||
int rows = grid.size(); | ||
int cols = grid[0].size(); | ||
int wordLen = word.size(); | ||
int count = 0; | ||
|
||
for (int i = 0; i < rows; ++i) { | ||
for (int j = 0; j < cols; ++j) { | ||
if (grid[i][j] == word[0]) { | ||
// Check all 8 directions | ||
for (int dir = 0; dir < 8; ++dir) { | ||
int x = i, y = j; | ||
bool found = true; | ||
|
||
for (int k = 1; k < wordLen; ++k) { | ||
x += rowDir[dir]; | ||
y += colDir[dir]; | ||
|
||
if (!isSafe(x, y, rows, cols) || | ||
grid[x][y] != word[k]) { | ||
found = false; | ||
break; | ||
} | ||
} | ||
|
||
if (found) { | ||
++count; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
int main() { | ||
ios::sync_with_stdio(0); | ||
cin.tie(nullptr); | ||
|
||
string filename; | ||
cout << "Input file: "; | ||
cout.flush(); | ||
|
||
getline(cin, filename); | ||
|
||
ifstream inputFile("day-4/" + filename); | ||
if (!inputFile.is_open()) { | ||
cerr << "Unable to read input file."; | ||
return 1; | ||
} | ||
|
||
vector<string> grid = {}; | ||
string line; | ||
|
||
while (getline(inputFile, line)) { | ||
grid.push_back(line); | ||
} | ||
|
||
int result = countXMAS(grid); | ||
cout << "XMAS count: " << result << '\n'; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.