From 85b305265e35c65cfb28b095aa3de8f88b52e709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Cort=C3=A9s?= Date: Thu, 3 Dec 2020 05:53:45 -0300 Subject: [PATCH] day 3 solution on c++ :3 --- 2020/day_3/toboggan_trajectory.py | 4 ++- 2020/day_3/toboggan_trayectory.cc | 57 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 2020/day_3/toboggan_trayectory.cc diff --git a/2020/day_3/toboggan_trajectory.py b/2020/day_3/toboggan_trajectory.py index b08bd7f..e67232e 100644 --- a/2020/day_3/toboggan_trajectory.py +++ b/2020/day_3/toboggan_trajectory.py @@ -22,7 +22,9 @@ def solve_a(): def solve_b(): slopes = ((1, 1), (3, 1), (5, 1), (7, 1), (1, 2)) - return prod([count_trees(slope) for slope in slopes]) + results = [count_trees(slope) for slope in slopes] + print(results) + return prod(results) print(solve_a()) diff --git a/2020/day_3/toboggan_trayectory.cc b/2020/day_3/toboggan_trayectory.cc new file mode 100644 index 0000000..c6fa835 --- /dev/null +++ b/2020/day_3/toboggan_trayectory.cc @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +std::vector read_input_file() { + std::ifstream file("input"); + std::vector data; + std::string line; + + while(std::getline(file, line)) + data.push_back(line); + + return data; +} + +struct slope{ int x; int y; }; + +int count_trees(std::vector map, const slope &s){ + int trees = 0; + + int x = s.x; + int y = s.y; + int w = map[0].size(); + int h = map.size(); + + while(y < h) { + if(map[y][x%w] == '#') trees++; + + x += s.x; + y += s.y; + } + + return trees; +} + +int solve_a(std::vector map) { + return count_trees(map, {3, 1}); +} + +long solve_b(std::vector map) { + std::vector slopes {{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}}; + + long result = 1; + for(auto& slope: slopes) result *= count_trees(map, slope); + return result; +} + + +int main() { + auto data = read_input_file(); + + std::cout << solve_a(data) << std::endl; + std::cout << solve_b(data) << std::endl; + + return 0; +}