Last active
December 3, 2021 17:02
-
-
Save CommitThis/0d51316673db2878a547a2fbc41dff98 to your computer and use it in GitHub Desktop.
Advent of Code
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
Title workaround. |
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
#include <algorithm> | |
#include <array> | |
#include <cstdint> | |
#include <fstream> | |
#include <iostream> | |
#include <iterator> | |
#include <numeric> | |
#include <string> | |
#include <vector> | |
/* https://www.foonathan.net/2020/05/fold-tricks/#get-the-last-element*/ | |
template <std::size_t Size, typename Iter, typename Op> | |
constexpr auto window(Iter first, Iter last, Op && op) { | |
if (std::distance(first, last) < Size) { | |
return; | |
} | |
auto iters = std::array<Iter, Size>{}; | |
std::iota(std::begin(iters), std::end(iters), first); | |
std::apply([&](auto & ... args){ | |
while ((args,...) != last) { | |
op((*args)...); | |
(++args, ...); | |
} | |
}, iters); | |
} | |
auto get_data() | |
{ | |
auto input = std::ifstream{"data.txt"}; | |
auto data = std::vector<std::int32_t>{}; | |
for (std::string line; std::getline(input, line);) { | |
data.push_back(std::stoi(line)); | |
} | |
return data; | |
} | |
template <typename Iter> | |
auto count_increases(Iter first, Iter last) | |
{ | |
auto result = 0ull; | |
while (first++ != last) { | |
first = std::adjacent_find(first, last, | |
[&](auto const & x, auto const & y){ | |
return y > x; | |
}); | |
result++; | |
} | |
return result; | |
} | |
auto part_one() { | |
// 1195 | |
auto data = get_data(); | |
auto result = count_increases(std::begin(data), std::end(data)); | |
std::cout << result << "\n"; | |
} | |
auto part_two() { | |
// 1235 | |
auto data = get_data(); | |
auto sums = std::vector<std::int32_t>{}; | |
window<3>(std::begin(data), std::end(data), | |
[&](auto a, auto b, auto c){ | |
sums.push_back(a + b + c); | |
}); | |
auto result = count_increases(std::begin(sums), std::end(sums)); | |
std::cout << result << "\n"; | |
} | |
auto main() -> int | |
{ | |
part_one(); | |
part_two(); | |
} |
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
#include <algorithm> | |
#include <array> | |
#include <cstdint> | |
#include <fstream> | |
#include <iostream> | |
#include <iterator> | |
#include <numeric> | |
#include <string> | |
#include <vector> | |
using namespace std::string_literals; | |
enum class direction | |
{ | |
forward, | |
up, | |
down | |
}; | |
auto from_string(std::string dir) | |
{ | |
if (dir == "forward"s) { | |
return direction::forward; | |
} | |
else if (dir == "up"s) { | |
return direction::up; | |
} | |
return direction::down; | |
} | |
struct instruction | |
{ | |
direction dir; | |
int steps; | |
}; | |
auto get_data() | |
{ | |
auto input = std::ifstream{"day2.data"}; | |
auto data = std::vector<instruction>{}; | |
for (std::string line; std::getline(input, line);) { | |
auto pos = line.find(" "s); | |
auto dir = line.substr(0, pos); | |
auto steps = line.substr(pos + 1, std::size(line)); | |
data.emplace_back(instruction{from_string(dir), std::stoi(steps)}); | |
} | |
return data; | |
} | |
auto part_one() | |
{ | |
auto data = get_data(); | |
auto x = 0, y =0; | |
for (auto insn : data) { | |
switch (insn.dir) { | |
case direction::forward: x += insn.steps; break; | |
case direction::up: y -= insn.steps; break; | |
case direction::down: y += insn.steps; break; | |
} | |
} | |
std::cout << x * y << "\n"; | |
} | |
auto part_two() | |
{ | |
auto data = get_data(); | |
auto x = 0, y = 0, aim = 0; | |
for (auto insn : data) { | |
switch (insn.dir) { | |
case direction::forward: x += insn.steps; y += aim * insn.steps; break; | |
case direction::up: aim -= insn.steps; break; | |
case direction::down: aim += insn.steps; break; | |
} | |
} | |
std::cout << x * y << "\n"; | |
} | |
auto main() -> int | |
{ | |
part_one(); | |
part_two(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment