Compare commits

..

9 Commits

Author SHA1 Message Date
aaf9b54e03 use same naming as clj solution 2025-12-28 05:42:23 -10:00
406c5906eb remove dead code 2025-12-28 05:40:22 -10:00
f16918d137 they see me foldin' 2025-12-28 05:39:48 -10:00
8ff89aa2c2 yay, flags 2025-12-27 17:36:22 -10:00
787476b47c std::accumulate all the nonsense 2025-12-27 17:36:17 -10:00
4df6ee4533 universal initialization syntax 2025-12-15 18:28:27 -10:00
c49493733a use unsigned 2025-12-15 18:26:34 -10:00
c18cb831ff use unsigned 2025-12-15 18:25:44 -10:00
fb82f11b39 simplify 2025-12-15 10:14:26 -10:00
3 changed files with 41 additions and 54 deletions
+2
View File
@@ -0,0 +1,2 @@
CompileFlags:
Add: [--std=c++23 ]
+2 -15
View File
@@ -24,6 +24,7 @@ int main() {
} }
std::cout << "Part 1: " << part_1(v) << std::endl; std::cout << "Part 1: " << part_1(v) << std::endl;
std::cout << "Part 2: " << part_2(v) << std::endl; std::cout << "Part 2: " << part_2(v) << std::endl;
return 0;
} }
int64_t part_1(const std::vector<Data> &v) { int64_t part_1(const std::vector<Data> &v) {
@@ -60,24 +61,10 @@ int64_t solve_n(int64_t n) {
} }
int64_t part_2(const std::vector<Data> &v) { int64_t part_2(const std::vector<Data> &v) {
int64_t ans{0}; return std::accumulate(v.begin(), v.end(), 0ll, [](int64_t acc, Data d) {
for (auto d : v) {
for (auto n{d.min}; n <= d.max; ++n) {
ans += solve_n(n);
}
}
// todo: dead code, delete me, then inline solve_n
int64_t foobar = std::accumulate(v.begin(), v.end(), 0, [&ans](int64_t acc, Data d) {
for (auto n{d.min}; n <= d.max; ++n) { for (auto n{d.min}; n <= d.max; ++n) {
acc += solve_n(n); acc += solve_n(n);
} }
return acc; return acc;
}); });
// help please :)
// why ans != foobar? looks like same but not same
// stepping through with debugger, it even seems the same on last iteration of accumulate
return ans;
} }
+30 -32
View File
@@ -1,18 +1,21 @@
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 7" // :autocmd BufWritePost *.cpp :silent exec "!script/async-build 7"
#include <algorithm>
#include <cstdint> #include <cstdint>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <numeric> #include <numeric>
#include <ranges>
#include <set> #include <set>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
using Grid = std::vector<std::string>; using Grid = std::vector<std::string>;
using Location = std::tuple<size_t, size_t>; using Location = std::tuple<size_t, size_t>;
int64_t part_1(const Grid &grid); uint64_t part_1(const Grid &grid);
int64_t part_2(const Grid &grid); uint64_t part_2(const Grid &grid);
int main() { int main() {
Grid grid; Grid grid;
@@ -35,19 +38,17 @@ char at_loc(const Grid &grid, size_t row, size_t col) {
} }
Location find_start(const Grid &grid) { Location find_start(const Grid &grid) {
for (int64_t row{0}; row < grid.size(); ++row) { const auto col{grid[0].find_first_of('S')};
for (int64_t col{0}; col < grid[row].length(); ++col) { if (col != std::string::npos) {
if (grid[row][col] == 'S') { return {0, col};
return Location{row, col}; } else {
std::unreachable();
} }
}
}
throw "unreachable";
} }
int64_t part_1(const Grid &grid) { uint64_t part_1(const Grid &grid) {
int64_t ans{0}; uint64_t ans{0};
Location start = find_start(grid); Location start{find_start(grid)};
const auto [start_row, start_col]{start}; const auto [start_row, start_col]{start};
std::set<size_t> cols{}; std::set<size_t> cols{};
cols.insert(start_col); cols.insert(start_col);
@@ -67,34 +68,31 @@ int64_t part_1(const Grid &grid) {
return ans; return ans;
} }
void insert_or_increment(std::map<size_t, int64_t> &cols, size_t col, void insert_or_increment(std::map<size_t, uint64_t> &cols, size_t col,
int64_t value) { uint64_t value) {
auto iter = cols.find(col); cols.insert({col, 0}).first->second += value;
if (iter == cols.end()) {
cols.insert({col, value});
} else {
iter->second += value;
}
} }
int64_t part_2(const Grid &grid) { uint64_t part_2(const Grid &grid) {
Location start = find_start(grid); Location start = find_start(grid);
const auto [start_row, start_col]{start}; const auto [start_row, start_col]{start};
std::map<size_t, int64_t> cols; using namespace std::ranges;
cols.insert(std::make_pair(start_col, 1)); auto view = iota_view{start_row, grid.size()};
for (size_t row{start_row}; row < grid.size(); ++row) { std::map<size_t, uint64_t> starting_cols{{start_col, 1}};
std::map<size_t, int64_t> next_cols; auto cols = fold_left(view, starting_cols, [&grid](auto cols, auto row) {
for (auto [col, cnt] : cols) { return fold_left(cols, std::map<size_t, uint64_t>{},
[&grid, &row](auto next_cols, auto col_cnt) {
auto [col, cnt] = col_cnt;
if (at_loc(grid, row, col) == '^') { if (at_loc(grid, row, col) == '^') {
insert_or_increment(next_cols, col - 1, cnt); insert_or_increment(next_cols, col - 1, cnt);
insert_or_increment(next_cols, col + 1, cnt); insert_or_increment(next_cols, col + 1, cnt);
} else { } else {
insert_or_increment(next_cols, col, cnt); insert_or_increment(next_cols, col, cnt);
} }
} return next_cols;
cols = std::move(next_cols); });
} });
return std::accumulate( return fold_left(cols, 0ull, [](const uint64_t acc, const auto &entry) {
cols.begin(), cols.end(), 0ll, return acc + entry.second;
[](int64_t acc, auto entry) { return acc + entry.second; }); });
} }