Skip to content

Commit

Permalink
2024 day3
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay committed Dec 4, 2024
1 parent 19a44cb commit 570f859
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/2024/03/data/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
1 change: 1 addition & 0 deletions src/2024/03/data/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
6 changes: 6 additions & 0 deletions src/2024/03/data/input.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/2024/03/makefile
1 change: 1 addition & 0 deletions src/2024/03/src/lib
75 changes: 75 additions & 0 deletions src/2024/03/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// adventofcode.com/2024/day/3

#include <algorithm>
#include <array>
#include <numeric>
#include <regex>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

#include <fmt/core.h>

#include "lib/io.h"
#include "lib/parse.h"
#include "lib/run.h"

std::string readFile(const std::string& path) {
return read(path);
}

size_t parse(const std::string& path, bool dodont = false) {
auto data = readFile(path);

std::smatch match;
std::regex regex{R"(mul\(([0-9]+),([0-9]+)\)|do\(\)|don\'t\(\))"};

size_t result = 0;
bool enabled = true;

std::string::const_iterator start(data.cbegin());
while (std::regex_search(start, data.cend(), match, regex)) {
// fmt::println("match: '{}' '{}' '{}' ({})", std::string{match[0]}, std::string{match[1]},
// std::string{match[2]}, match.size());

if (match.length(1) == 0 && match.length(2) == 0) {
if (dodont) {
if (match[0] == "do()") {
enabled = true;
} else if (match[0] == "don't()") {
enabled = false;
} else {
assert(false);
}
}
} else {
assert(match.size() == 3);
const auto num1 = to<size_t>(std::string{match[1]});
const auto num2 = to<size_t>(std::string{match[2]});
// fmt::println("match: {}, num1: {}, num2: {}", std::string{match[0]}, num1, num2);

if (enabled) {
result += (num1 * num2);
}
}
start = match.suffix().first;
}

return result;
}

size_t part1(const std::string& path) {
return parse(path);
}

size_t part2(const std::string& path) {
return parse(path, true);
}

int main() {
run(1, part1, true, 161UL);
run(1, part1, false, 178538786UL);
run(2, part2, true, 48UL, "data/example2.txt");
run(2, part2, false, 102467299UL);
}
2 changes: 2 additions & 0 deletions src/lib/io.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "lib/io.h"

#include <filesystem>
#include <fstream>

#include <boost/algorithm/string.hpp>

std::string read(const std::string& path, bool trim) {
assert(std::filesystem::exists(path));
std::ifstream file{path};

std::string data = {(std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()};
Expand Down

0 comments on commit 570f859

Please sign in to comment.