From 4b6c1b0e539d9fc37b646af5865b75aaa5b49bfc Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 13 Nov 2024 22:20:30 +0100 Subject: aoc 2015, days 5, 6 and 7 --- 2015/src/day05.cpp | 56 ++++++++++++++++++++++++++ 2015/src/day06.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2015/src/day07.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 2015/src/day05.cpp create mode 100644 2015/src/day06.cpp create mode 100644 2015/src/day07.cpp (limited to '2015') diff --git a/2015/src/day05.cpp b/2015/src/day05.cpp new file mode 100644 index 0000000..b88ecf5 --- /dev/null +++ b/2015/src/day05.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +auto +read_file(string_view filename) +{ + fstream input{ filename }; + vector lines; + + for ( string line; getline(input, line); ) { + lines.emplace_back(line); + } + + return lines; +} + +void +part1(const vector& lines) +{ + const regex re1("(.*[aeiou]){3}"); + const regex re2("(.)\\1"); + const regex re3("(ab|cd|pq|xy)"); + + auto count = count_if(begin(lines), end(lines), [&](auto line) { + return regex_search(line, re1) && regex_search(line, re2) && !regex_search(line, re3); + }); + + cout << count << endl; +} + +void +part2(const vector& lines) +{ + const regex re1("(..).*\\1"); + const regex re2("(.).\\1"); + + auto count = count_if(begin(lines), end(lines), [&](auto line) { + return regex_search(line, re1) && regex_search(line, re2); + }); + + cout << count << endl; +} + +int +main() +{ + auto puzzle = read_file("data/day05.txt"); + part1(puzzle); + part2(puzzle); +} diff --git a/2015/src/day06.cpp b/2015/src/day06.cpp new file mode 100644 index 0000000..b7f15b5 --- /dev/null +++ b/2015/src/day06.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +enum Operation { + On, + Off, + Toggle +}; + +using instruction = tuple; + +auto +read_file(string_view filename) +{ + fstream input{ filename }; + vector instructions; + + for ( string line; getline(input, line); ) { + int x0, y0, x1, y1; // NOLINT + + if ( sscanf(line.c_str(), "turn on %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT + instructions.emplace_back(Operation::On, x0, y0, x1, y1); + } + else if ( sscanf(line.c_str(), "toggle %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT + instructions.emplace_back(Operation::Toggle, x0, y0, x1, y1); + } + else if ( sscanf(line.c_str(), "turn off %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT + instructions.emplace_back(Operation::Off, x0, y0, x1, y1); + } + } + + return instructions; +} + +// clang-format off +static void turn_on(int& value) { value = 1; } +static void turn_off(int& value) { value = 0; } +static void toggle(int& value) { value ^= 1; } +// clang-format on + +void +part1(const vector& instructions) +{ + map> ops = { + { Operation::On, turn_on }, + { Operation::Toggle, toggle }, + { Operation::Off, turn_off } + }; + + map, int> lights; + + for ( const auto& [operation, x0, y0, x1, y1]: instructions ) { + for ( int x = x0; x <= x1; ++x ) { + for ( int y = y0; y <= y1; ++y ) { + ops[operation](lights[{ x, y }]); + } + } + } + + auto sum = count_if(begin(lights), end(lights), [](const auto& light) { + return light.second == 1; + }); + + cout << sum << endl; +} + +// clang-format off +static void increase(int& value) { ++value; } +static void decrease(int& value) { if (value > 0) { --value; } } +static void increase_by_2(int& value) { value += 2; } +// clang-format on + +void +part2(const vector& instructions) +{ + map> ops = { + { Operation::On, increase }, + { Operation::Toggle, increase_by_2 }, + { Operation::Off, decrease } + }; + + map, int> lights; + + for ( const auto& [operation, x0, y0, x1, y1]: instructions ) { + for ( int x = x0; x <= x1; ++x ) { + for ( int y = y0; y <= y1; ++y ) { + ops[operation](lights[{ x, y }]); + } + } + } + + auto sum = accumulate(begin(lights), end(lights), 0, [](int value, const auto& light) { + return value + light.second; + }); + + cout << sum << endl; +} + +int +main() +{ + auto instructions = read_file("data/day06.txt"); + part1(instructions); + part2(instructions); +} diff --git a/2015/src/day07.cpp b/2015/src/day07.cpp new file mode 100644 index 0000000..f24d5af --- /dev/null +++ b/2015/src/day07.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +auto +read_file(string_view filename) +{ + fstream input{ filename }; + map deps; + + for ( string line; getline(input, line); ) { + const auto pos = line.find(" -> "); + if ( pos == string::npos ) { + continue; + } + + deps[line.substr(pos + 4)] = line.substr(0, pos); + } + + return deps; +} + +// NOLINTBEGIN +uint16_t +solve(map& deps, const string& dep) +{ + map cache; + + function solve = [&](const string& dep) -> uint16_t { + if (cache.contains(dep)) { + return cache[dep]; + } + + uint16_t value = 0; + if (sscanf(dep.c_str(), "%" SCNu16, &value) == 1) { + return cache[dep] = value; + } + + const auto& line = deps[dep]; + + static const size_t MAX = 11; + char lhs[MAX], rhs[MAX]; + + if ( sscanf(line.c_str(), "%10s AND %10s", lhs, rhs) == 2 ) { + value = solve(lhs) & solve(rhs); + } + else if ( sscanf(line.c_str(), "%10s OR %10s", lhs, rhs) == 2 ) { + value = solve(lhs) | solve(rhs); + } + else if ( sscanf(line.c_str(), "%10s LSHIFT %10s", lhs, rhs) == 2 ) { + value = static_cast(solve(lhs) << solve(rhs)); + } + else if ( sscanf(line.c_str(), "%10s RSHIFT %10s", lhs, rhs) == 2 ) { + value = static_cast(solve(lhs) >> solve(rhs)); + } + else if ( sscanf(line.c_str(), "NOT %10s", lhs) == 1 ) { + value = ~solve(lhs); + } + else { + value = solve(line); + } + + return cache[dep] = value; + }; + + return solve(dep); +} +// NOLINTEND + +auto +part1(map& deps) +{ + auto result = solve(deps, "a"); + cout << result << endl; + return result; +} + +void +part2(map& deps, uint16_t result) +{ + deps["b"] = to_string(result); + cout << solve(deps, "a") << endl; +} + +int +main() +{ + auto deps = read_file("data/day07.txt"); + auto result = part1(deps); + part2(deps, result); +} -- cgit v1.3