Skip to content

Commit

Permalink
solve(day 4): Ceres search part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-xero committed Dec 4, 2024
1 parent 7e52958 commit 725d374
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 6 deletions.
12 changes: 6 additions & 6 deletions day-3/01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

using namespace std;

int computeMuls(const string& line) {
int computeFlaggedMuls(const string& line) {
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++) {
for (size_t i = 0; i + 4 <= line.size(); i++) {
if (line.substr(i, 4) == "mul(") {
int a = 0;
int b = 0;
Expand All @@ -21,7 +21,7 @@ int computeMuls(const string& line) {

// Edge case: must be closed by a ')' character
if (ch != ')') continue;

subComputation += a * b;
}
}
Expand Down Expand Up @@ -50,11 +50,11 @@ int main() {

// The input is a long string where each line contains some `mul(a,b)`
// instructions
string line;
string token;
int computation = 0;

while (inputFile >> line) {
computation += computeMuls(line);
while (inputFile >> token) {
computation += computeFlaggedMuls(token);
}

cout << "computed: " << computation << '\n';
Expand Down
80 changes: 80 additions & 0 deletions day-3/02.cpp
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;
}
75 changes: 75 additions & 0 deletions day-4/01.cpp
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;
}
Loading

0 comments on commit 725d374

Please sign in to comment.