Compare commits
12 Commits
7c270c49aa
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| aaf9b54e03 | |||
| 406c5906eb | |||
| f16918d137 | |||
| 8ff89aa2c2 | |||
| 787476b47c | |||
| 4df6ee4533 | |||
| c49493733a | |||
| c18cb831ff | |||
| fb82f11b39 | |||
| ebdab4a2e0 | |||
| 4adce460ba | |||
| 516667944a |
@@ -0,0 +1,16 @@
|
||||
.......S.......
|
||||
...............
|
||||
.......^.......
|
||||
...............
|
||||
......^.^......
|
||||
...............
|
||||
.....^.^.^.....
|
||||
...............
|
||||
....^.^...^....
|
||||
...............
|
||||
...^.^...^.^...
|
||||
...............
|
||||
..^...^.....^..
|
||||
...............
|
||||
.^.^.^.^.^...^.
|
||||
...............
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
script/build $1&
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
./build $1
|
||||
script/build $1
|
||||
gdb a.out
|
||||
+1
-1
@@ -2,5 +2,5 @@
|
||||
|
||||
set -e
|
||||
|
||||
./build $1
|
||||
script/build $1
|
||||
./a.out
|
||||
@@ -1,3 +1,4 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 1"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
+3
-16
@@ -1,4 +1,4 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 2"
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 2"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
@@ -24,6 +24,7 @@ int main() {
|
||||
}
|
||||
std::cout << "Part 1: " << part_1(v) << std::endl;
|
||||
std::cout << "Part 2: " << part_2(v) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 ans{0};
|
||||
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) {
|
||||
return std::accumulate(v.begin(), v.end(), 0ll, [](int64_t acc, Data d) {
|
||||
for (auto n{d.min}; n <= d.max; ++n) {
|
||||
acc += solve_n(n);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 3"
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 3"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 4"
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 4"
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 5"
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 5"
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 6"
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 6"
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 7"
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <ranges>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using Grid = std::vector<std::string>;
|
||||
using Location = std::tuple<size_t, size_t>;
|
||||
|
||||
uint64_t part_1(const Grid &grid);
|
||||
uint64_t part_2(const Grid &grid);
|
||||
|
||||
int main() {
|
||||
Grid grid;
|
||||
std::ifstream file{"input/2025-7.txt"};
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
grid.emplace_back(line);
|
||||
}
|
||||
std::cout << "Part 1: " << part_1(grid) << std::endl;
|
||||
std::cout << "Part 2: " << part_2(grid) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char at_loc(const Grid &grid, size_t row, size_t col) {
|
||||
if (row >= 0 && row < grid.size() && col >= 0 && col < grid[row].size()) {
|
||||
return grid[row][col];
|
||||
} else {
|
||||
return '.';
|
||||
}
|
||||
}
|
||||
|
||||
Location find_start(const Grid &grid) {
|
||||
const auto col{grid[0].find_first_of('S')};
|
||||
if (col != std::string::npos) {
|
||||
return {0, col};
|
||||
} else {
|
||||
std::unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t part_1(const Grid &grid) {
|
||||
uint64_t ans{0};
|
||||
Location start{find_start(grid)};
|
||||
const auto [start_row, start_col]{start};
|
||||
std::set<size_t> cols{};
|
||||
cols.insert(start_col);
|
||||
for (size_t row{start_row}; row < grid.size(); ++row) {
|
||||
std::set<size_t> next_cols{};
|
||||
for (auto col : cols) {
|
||||
if (at_loc(grid, row, col) == '^') {
|
||||
ans += 1;
|
||||
next_cols.insert(col + 1);
|
||||
next_cols.insert(col - 1);
|
||||
} else {
|
||||
next_cols.insert(col);
|
||||
}
|
||||
}
|
||||
cols = next_cols;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
void insert_or_increment(std::map<size_t, uint64_t> &cols, size_t col,
|
||||
uint64_t value) {
|
||||
cols.insert({col, 0}).first->second += value;
|
||||
}
|
||||
|
||||
uint64_t part_2(const Grid &grid) {
|
||||
Location start = find_start(grid);
|
||||
const auto [start_row, start_col]{start};
|
||||
using namespace std::ranges;
|
||||
auto view = iota_view{start_row, grid.size()};
|
||||
std::map<size_t, uint64_t> starting_cols{{start_col, 1}};
|
||||
auto cols = fold_left(view, starting_cols, [&grid](auto cols, auto row) {
|
||||
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) == '^') {
|
||||
insert_or_increment(next_cols, col - 1, cnt);
|
||||
insert_or_increment(next_cols, col + 1, cnt);
|
||||
} else {
|
||||
insert_or_increment(next_cols, col, cnt);
|
||||
}
|
||||
return next_cols;
|
||||
});
|
||||
});
|
||||
return fold_left(cols, 0ull, [](const uint64_t acc, const auto &entry) {
|
||||
return acc + entry.second;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user