From 7c270c49aa580ef918aad6b9e09d6e58fc17a7b5 Mon Sep 17 00:00:00 2001 From: ajet Date: Sun, 14 Dec 2025 06:45:19 -1000 Subject: [PATCH] do day 6 --- input/2025-6.sample.txt | 4 ++ src/day6.cpp | 102 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 input/2025-6.sample.txt create mode 100644 src/day6.cpp diff --git a/input/2025-6.sample.txt b/input/2025-6.sample.txt new file mode 100644 index 0000000..337b837 --- /dev/null +++ b/input/2025-6.sample.txt @@ -0,0 +1,4 @@ +123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * + diff --git a/src/day6.cpp b/src/day6.cpp new file mode 100644 index 0000000..705ff3c --- /dev/null +++ b/src/day6.cpp @@ -0,0 +1,102 @@ +// :autocmd BufWritePost *.cpp :silent exec "!./build 6" +#include +#include +#include +#include +#include +#include + +using Grid = std::vector; +using Ops = std::vector; + +int64_t part_1(const Grid &grid, const Ops &ops); +int64_t part_2(const Grid &grid, const Ops &ops); + +int main() { + Grid grid; + std::string line; + std::ifstream file{"input/2025-6.txt"}; + size_t row{0}; + while (std::getline(file, line)) { + grid.emplace_back(line); + row += 1; + } + auto op_line{grid[grid.size() - 1]}; + std::istringstream reader{op_line}; + char op; + std::vector ops; + while (reader >> op) { + ops.push_back(op); + } + std::cout << part_1(grid, ops) << std::endl; + std::cout << part_2(grid, ops) << std::endl; + return 0; +} + +int64_t part_1(const Grid &grid, const Ops &ops) { + std::vector> blocks; + for (auto i{0}; i < grid.size() - 1; ++i) { + std::vector current_line{}; + auto line = grid[i]; + std::istringstream reader{line}; + int64_t num; + while (reader >> num) { + current_line.push_back(num); + } + blocks.emplace_back(current_line); + } + int64_t ans{0}; + for (int64_t i{0}; i < blocks[0].size(); ++i) { + if (ops[i] == '*') { + int64_t curr{1}; + for (int64_t j{0}; j < blocks.size(); ++j) { + curr *= blocks[j][i]; + } + ans += curr; + } else { + int64_t curr{0}; + for (int64_t j{0}; j < blocks.size(); ++j) { + curr += blocks[j][i]; + } + ans += curr; + } + } + return ans; +} + +int64_t part_2(const Grid &grid, const Ops &ops) { + size_t op_iter{}; + uint64_t ans{}; + std::vector block; + for (size_t col{}; col < grid[0].length(); ++col) { + std::string col_str{""}; + for (size_t row{}; row < grid.size() - 1; ++row) { + char c{grid[row][col]}; + if (c >= '0' && c <= '9') { + col_str += c; + } + } + if (col_str == "") { + if (ops[op_iter] == '*') { + int64_t temp{1}; + for (auto n : block) { + temp *= n; + } + ans += temp; + } else { + auto temp{0}; + for (auto n : block) { + temp += n; + } + ans += temp; + } + block.clear(); + ++op_iter; + continue; + } else { + int64_t num{std::stoll(col_str)}; + block.push_back(num); + } + } + return ans; +}