Skip to content

Instantly share code, notes, and snippets.

@vbe0201
Last active December 3, 2021 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbe0201/6260af21f51b820a7458609b66e8f73e to your computer and use it in GitHub Desktop.
Save vbe0201/6260af21f51b820a7458609b66e8f73e to your computer and use it in GitHub Desktop.
My Advent of Code 2021 solutions written in C++
#include <algorithm>
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
void part1() {
std::ifstream input{"input.txt"};
auto view = std::ranges::istream_view<int>(input);
int previous = *view.begin();
int count = 0;
for (auto depth : view) {
/* Load previous depth and store current depth as next round's previous. */
auto previous_depth = depth;
std::swap(previous, previous_depth);
/* We increment count when the depth increased from the previous round. */
if (previous_depth < depth) {
count += 1;
}
}
std::cout << count << " measurements are larger than the previous measurement!\n";
}
void part2() {
std::puts("Check back when ranges don't suck anymore in C++23.");
}
int main() {
part1();
part2();
return 0;
}
#include <charconv>
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
constexpr std::string_view g_whitespace{" "};
void part1() {
std::ifstream input{"input.txt"};
int horizontal = 0;
int depth = 0;
for (std::string line; std::getline(input, line); /* ... */) {
/* Split each line at the whitespace. */
auto split = std::views::transform(std::views::split(line, g_whitespace), [](auto s) {
return std::string_view(s.begin(), s.end());
});
/* Extract the command and parse the `X` value into an integer. */
const auto command = *split.begin();
int amount{};
{
const auto amount_str = *std::ranges::next(split.begin());
std::from_chars(amount_str.data(), amount_str.data() + amount_str.size(), amount);
}
/* Process the command. */
if (command == "forward") {
horizontal += amount;
} else if (command == "down") {
depth += amount;
} else if (command == "up") {
depth -= amount;
}
}
std::cout << horizontal * depth << " is the final position!\n";
}
void part2() {
std::ifstream input{"input.txt"};
int horizontal = 0;
int depth = 0;
int aim = 0;
for (std::string line; std::getline(input, line); /* ... */) {
/* Split each line at the whitespace. */
auto split = std::views::transform(std::views::split(line, g_whitespace), [](auto s) {
return std::string_view(s.begin(), s.end());
});
/* Extract the command and parse the `X` value into an integer. */
const auto command = *split.begin();
int amount{};
{
const auto amount_str = *std::ranges::next(split.begin());
std::from_chars(amount_str.data(), amount_str.data() + amount_str.size(), amount);
}
/* Process the command. */
if (command == "forward") {
horizontal += amount;
depth += amount * aim;
} else if (command == "down") {
aim += amount;
} else if (command == "up") {
aim -= amount;
}
}
std::cout << horizontal * depth << " is the final position!\n";
}
int main() {
part1();
part2();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment