Compare commits

..

18 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
ebdab4a2e0 do day 7 2025-12-15 09:42:38 -10:00
4adce460ba fix scripts 2025-12-15 09:42:34 -10:00
516667944a add async builds 2025-12-15 08:38:16 -10:00
7c270c49aa do day 6 2025-12-14 06:45:23 -10:00
db573795d3 inline 2025-12-13 22:29:59 -10:00
094dc367ad do day 5 2025-12-13 22:22:02 -10:00
6dbec069ca do day 4 2025-12-13 21:09:58 -10:00
90de771f84 optimize for dumb pr 2025-12-13 19:55:55 -10:00
d32acd64fd comment 2025-12-13 19:54:39 -10:00
16 changed files with 434 additions and 23 deletions
+2
View File
@@ -0,0 +1,2 @@
CompileFlags:
Add: [--std=c++23 ]
+10
View File
@@ -0,0 +1,10 @@
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
+11
View File
@@ -0,0 +1,11 @@
3-5
10-14
16-20
12-18
1
5
8
11
17
32
+4
View File
@@ -0,0 +1,4 @@
123 328 51 64
45 64 387 23
6 98 215 314
* + * +
+16
View File
@@ -0,0 +1,16 @@
.......S.......
...............
.......^.......
...............
......^.^......
...............
.....^.^.^.....
...............
....^.^...^....
...............
...^.^...^.^...
...............
..^...^.....^..
...............
.^.^.^.^.^...^.
...............
+5
View File
@@ -0,0 +1,5 @@
#!/bin/bash
set -e
script/build $1&
View File
+1 -1
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
set -e set -e
./build $1 script/build $1
gdb a.out gdb a.out
+1 -1
View File
@@ -2,5 +2,5 @@
set -e set -e
./build $1 script/build $1
./a.out ./a.out
+1
View File
@@ -1,3 +1,4 @@
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 1"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
+4 -17
View File
@@ -1,4 +1,4 @@
// :autocmd BufWritePost *.cpp :silent exec "!./build 2" // :autocmd BufWritePost *.cpp :silent exec "!script/async-build 2"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <numeric> #include <numeric>
@@ -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) { for (auto n{d.min}; n <= d.max; ++n) {
ans += solve_n(n); acc += 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) {
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;
} }
+5 -4
View File
@@ -1,4 +1,4 @@
// :autocmd BufWritePost *.cpp :silent exec "!./build 3" // :autocmd BufWritePost *.cpp :silent exec "!script/async-build 3"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <numeric> #include <numeric>
@@ -26,14 +26,15 @@ char get_max_char(std::string_view s) {
} }
int64_t solve(const std::string_view line, size_t cnt_to_activate) { int64_t solve(const std::string_view line, size_t cnt_to_activate) {
const auto search{line.substr(0, line.size() - cnt_to_activate + 1)}; const std::string_view search{line.substr(0, line.size() - cnt_to_activate + 1)};
const char max_char{get_max_char(search)}, null_terminator{'\0'}; const char max_char{get_max_char(search)}, null_terminator{'\0'};
const auto max_char_idx{search.find(&max_char, 0)}; const size_t max_char_idx{search.find(&max_char, 0)};
const int64_t max_char_as_int = max_char - '0'; const int64_t max_char_as_int = max_char - '0';
if (cnt_to_activate == 1) { if (cnt_to_activate == 1) {
return max_char_as_int; return max_char_as_int;
} }
const auto recur{solve(line.substr(max_char_idx + 1), cnt_to_activate - 1)}; const int64_t recur{solve(line.substr(max_char_idx + 1), cnt_to_activate - 1)};
// slightly hacky... integral concat better?
return std::stoll(std::string(&max_char) + std::to_string(recur)); return std::stoll(std::string(&max_char) + std::to_string(recur));
} }
+97
View File
@@ -0,0 +1,97 @@
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 4"
#include <cstdint>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
// investigate nob.h and c macros, macroify derive(eq, hash, ...)
struct Roll {
int64_t row;
int64_t col;
};
bool operator==(const Roll &lhs, const Roll &rhs) {
return lhs.row == rhs.row && lhs.col == rhs.col;
}
using Grid = std::vector<std::string>;
using Rolls = std::vector<Roll>;
int64_t part_1(const Grid &grid, const Rolls &rolls);
int64_t part_2(Grid, std::vector<Roll> rolls);
int main() {
std::ifstream file{"input/2025-4.txt"};
std::string line;
Roll roll_iter{};
std::vector<Roll> rolls;
Grid grid;
while (std::getline(file, line)) {
grid.push_back(line);
for (char c : line) {
if (c == '@') {
Roll local_copy = roll_iter;
rolls.push_back(local_copy);
}
roll_iter.col += 1;
}
roll_iter.row += 1;
roll_iter.col = 0;
}
std::cout << "Part 1: " << part_1(grid, rolls) << std::endl;
std::cout << "Part 2: " << part_2(grid, rolls) << std::endl;
}
void is_roll_helper(size_t row, size_t col, const Grid &grid, short &ans) {
if (row >= 0 && row < grid.size() && col >= 0 && col < grid[row].size() &&
grid[row][col] == '@') {
ans += 1;
}
}
short num_neighbors(const Grid &grid, const Roll &roll) {
short ans{0};
is_roll_helper(roll.row - 1, roll.col - 1, grid, ans);
is_roll_helper(roll.row - 1, roll.col, grid, ans);
is_roll_helper(roll.row - 1, roll.col + 1, grid, ans);
is_roll_helper(roll.row, roll.col - 1, grid, ans);
is_roll_helper(roll.row, roll.col + 1, grid, ans);
is_roll_helper(roll.row + 1, roll.col - 1, grid, ans);
is_roll_helper(roll.row + 1, roll.col, grid, ans);
is_roll_helper(roll.row + 1, roll.col + 1, grid, ans);
return ans;
}
int64_t part_1(const Grid &grid, const Rolls &rolls) {
int64_t ans{0};
for (auto roll : rolls) {
auto cnt{num_neighbors(grid, roll)};
if (cnt < 4) {
ans += 1;
}
}
return ans;
}
int64_t part_2(Grid grid, Rolls rolls) {
int64_t last_roll_cnt = INT64_MAX;
int64_t ans{0};
Rolls to_delete{};
do {
to_delete.clear();
for (auto roll : rolls) {
auto cnt{num_neighbors(grid, roll)};
if (cnt < 4) {
ans += 1;
to_delete.emplace_back(roll);
}
}
for (auto roll : to_delete) {
std::erase(rolls, roll);
grid[roll.row][roll.col] = '.';
}
} while (to_delete.size() != 0);
return ans;
}
+77
View File
@@ -0,0 +1,77 @@
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 5"
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Range {
int64_t min;
int64_t max;
};
constexpr int compare(const Range &a, const Range &b) {
if (a.min == b.min) {
return a.max < b.max;
}
return a.min < b.min;
}
using Ranges = std::vector<Range>;
using Nums = std::vector<int64_t>;
int64_t part_1(const Ranges &ranges, const Nums &nums);
int64_t part_2(const Ranges &ranges, const Nums &nums);
int main() {
std::ifstream file{"input/2025-5.txt"};
std::string line;
Ranges ranges;
char throwaway;
while (std::getline(file, line) && line != "") {
Range r{};
sscanf(line.c_str(), "%ld-%ld", &r.min, &r.max);
ranges.push_back(r);
}
int64_t num;
Nums nums{};
while (file >> num) {
nums.push_back(num);
}
std::sort(ranges.begin(), ranges.end(), compare);
std::cout << "Part 1: " << part_1(ranges, nums) << std::endl;
std::cout << "Part 2: " << part_2(ranges, nums) << std::endl;
return 0;
}
int64_t part_1(const Ranges &ranges, const Nums &nums) {
int64_t ans{};
for (auto num : nums) {
for (auto range : ranges) {
if (num >= range.min && num <= range.max) {
ans += 1;
break;
}
}
}
return ans;
}
int64_t part_2(const Ranges &ranges, const Nums &nums) {
int64_t ans{};
int64_t scanner{0};
for (auto range : ranges) {
if (scanner < range.min) {
ans += range.max - range.min + 1;
scanner = range.max;
} else if (scanner <= range.max) {
ans += range.max - scanner;
scanner = range.max;
}
}
return ans;
}
+102
View File
@@ -0,0 +1,102 @@
// :autocmd BufWritePost *.cpp :silent exec "!script/async-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;
}
+98
View File
@@ -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;
});
}