From 02e256a6cde678bc05ea1a868b717ed7aa12ee37 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Thu, 3 Dec 2020 00:12:29 -0500 Subject: [PATCH] Add day 3 implementation --- day3/day3.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 day3/day3.cpp diff --git a/day3/day3.cpp b/day3/day3.cpp new file mode 100644 index 0000000..246296a --- /dev/null +++ b/day3/day3.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +constexpr char TREE_CHAR = '#'; + +std::vector read_input(const std::string &filename) { + std::vector input; + std::string line; + std::ifstream file(filename); + while (std::getline(file, line)) { + input.push_back(line); + } + + return input; +} + +/** + * Find the number of trees that move along a path + * @param input The input for the puzzle + * @param xDelta The number of tiles to move right each step + * @param yDelta The number of tiles to move down each step + * @return int The number of trees encountered along this path + */ +int findNumTrees(const std::vector &input, int xDelta, int yDelta = 1) { + int xCursor = 0; + int numTrees = 0; + for (int i = 0; i < input.size(); i += yDelta) { + const std::string &row = input.at(i); + numTrees += (row.at(xCursor) == TREE_CHAR); + xCursor = (xCursor + xDelta) % row.length(); + } + + return numTrees; +} + +int part1(const std::vector &input) { + return findNumTrees(input, 3); +} + +long part2(const std::vector &input) { + return 1L * findNumTrees(input, 1, 1) * findNumTrees(input, 3, 1) * findNumTrees(input, 5, 1) * + findNumTrees(input, 7, 1) * findNumTrees(input, 1, 2); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + std::cerr << "./" << argv[0] << "" << std::endl; + return 1; + } + + auto input = read_input(argv[1]); + std::cout << part1(input) << std::endl; + std::cout << part2(input) << std::endl; +}