Compare commits
3 Commits
7c270c49aa
...
ebdab4a2e0
| Author | SHA1 | Date | |
|---|---|---|---|
| ebdab4a2e0 | |||
| 4adce460ba | |||
| 516667944a |
16
input/2025-7.sample.txt
Normal file
16
input/2025-7.sample.txt
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
.......S.......
|
||||||
|
...............
|
||||||
|
.......^.......
|
||||||
|
...............
|
||||||
|
......^.^......
|
||||||
|
...............
|
||||||
|
.....^.^.^.....
|
||||||
|
...............
|
||||||
|
....^.^...^....
|
||||||
|
...............
|
||||||
|
...^.^...^.^...
|
||||||
|
...............
|
||||||
|
..^...^.....^..
|
||||||
|
...............
|
||||||
|
.^.^.^.^.^...^.
|
||||||
|
...............
|
||||||
5
script/async-build
Executable file
5
script/async-build
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
script/build $1&
|
||||||
@ -1,5 +1,5 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
./build $1
|
script/build $1
|
||||||
gdb a.out
|
gdb a.out
|
||||||
@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
./build $1
|
script/build $1
|
||||||
./a.out
|
./a.out
|
||||||
@ -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>
|
||||||
|
|||||||
@ -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>
|
||||||
|
|||||||
@ -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>
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 4"
|
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 4"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 5"
|
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 5"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 6"
|
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 6"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|||||||
100
src/day7.cpp
Normal file
100
src/day7.cpp
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 7"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <numeric>
|
||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using Grid = std::vector<std::string>;
|
||||||
|
using Location = std::tuple<size_t, size_t>;
|
||||||
|
|
||||||
|
int64_t part_1(const Grid &grid);
|
||||||
|
int64_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) {
|
||||||
|
for (int64_t row{0}; row < grid.size(); ++row) {
|
||||||
|
for (int64_t col{0}; col < grid[row].length(); ++col) {
|
||||||
|
if (grid[row][col] == 'S') {
|
||||||
|
return Location{row, col};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw "unreachable";
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t part_1(const Grid &grid) {
|
||||||
|
int64_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, int64_t> &cols, size_t col,
|
||||||
|
int64_t value) {
|
||||||
|
auto iter = cols.find(col);
|
||||||
|
if (iter == cols.end()) {
|
||||||
|
cols.insert({col, value});
|
||||||
|
} else {
|
||||||
|
iter->second += value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t part_2(const Grid &grid) {
|
||||||
|
Location start = find_start(grid);
|
||||||
|
const auto [start_row, start_col]{start};
|
||||||
|
std::map<size_t, int64_t> cols;
|
||||||
|
cols.insert(std::make_pair(start_col, 1));
|
||||||
|
for (size_t row{start_row}; row < grid.size(); ++row) {
|
||||||
|
std::map<size_t, int64_t> next_cols;
|
||||||
|
for (auto [col, cnt] : cols) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cols = std::move(next_cols);
|
||||||
|
}
|
||||||
|
return std::accumulate(
|
||||||
|
cols.begin(), cols.end(), 0ll,
|
||||||
|
[](int64_t acc, auto entry) { return acc + entry.second; });
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user