do day 6
This commit is contained in:
parent
db573795d3
commit
7c270c49aa
4
input/2025-6.sample.txt
Normal file
4
input/2025-6.sample.txt
Normal file
@ -0,0 +1,4 @@
|
||||
123 328 51 64
|
||||
45 64 387 23
|
||||
6 98 215 314
|
||||
* + * +
|
||||
102
src/day6.cpp
Normal file
102
src/day6.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 6"
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using Grid = std::vector<std::string>;
|
||||
using Ops = std::vector<char>;
|
||||
|
||||
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<char> 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<std::vector<int64_t>> blocks;
|
||||
for (auto i{0}; i < grid.size() - 1; ++i) {
|
||||
std::vector<int64_t> 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<int64_t> 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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user