Skip to content

Commit

Permalink
2023: day 13 part 1 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
yut23 committed Dec 13, 2023
1 parent 0f9149a commit 8d73353
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 2023/input/day13/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
38 changes: 38 additions & 0 deletions 2023/src/day13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/******************************************************************************
* File: day13.cpp
*
* Author: Eric T. Johnson (yut23)
* Created: 2023-12-13
*****************************************************************************/

#include "day13.hpp"
#include "lib.hpp" // for parse_args, DEBUG
#include <iostream> // for cout, cerr

void print_grid(std::ostream &os, const aoc::day13::Grid &grid) {
for (const auto &line : grid) {
os << line << "\n";
}
}

int main(int argc, char **argv) {
std::ifstream infile = aoc::parse_args(argc, argv);

auto grids = aoc::day13::read_grids(infile);

int part_1 = 0;
for (const auto &grid : grids) {
part_1 += 100 * aoc::day13::find_reflection(grid);
part_1 += aoc::day13::find_reflection(aoc::day13::transpose(grid));
if constexpr (aoc::DEBUG) {
print_grid(std::cerr, grid);
std::cerr << "\n";
print_grid(std::cerr, aoc::day13::transpose(grid));
std::cerr << "\n\n";
}
}

std::cout << part_1 << "\n";

return 0;
}
66 changes: 66 additions & 0 deletions 2023/src/day13.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/******************************************************************************
* File: day13.hpp
*
* Author: Eric T. Johnson (yut23)
* Created: 2023-12-13
*****************************************************************************/

#include "lib.hpp"
#include <cassert> // for assert
#include <cstddef> // for size_t
#include <iostream> // for istream
#include <string> // for string, getline
#include <vector> // for vector

namespace aoc::day13 {

using Grid = std::vector<std::string>;

Grid transpose(const Grid &grid) {
if (grid.size() == 0) {
return {};
}
Grid new_grid(grid[0].size());
for (const std::string &row : grid) {
for (std::size_t i = 0; i < row.size(); ++i) {
new_grid[i].push_back(row[i]);
}
}
return new_grid;
}

/// returns 0 if no reflection is found
std::size_t find_reflection(const Grid &grid) {
// just check every row to see if it's a line of reflection
// if this is a bottleneck, I'll improve it later
for (std::size_t i = 1; i < grid.size(); ++i) {
bool is_reflection = true;
for (int j = 0; i - j > 0 && i + j < grid.size(); ++j) {
if (grid[i - j - 1] != grid[i + j]) {
is_reflection = false;
break;
}
}
if (is_reflection) {
return i;
}
}
return 0;
}

std::vector<Grid> read_grids(std::istream &is) {
std::vector<Grid> grids(1);
std::string line;
Grid curr_grid;
while (std::getline(is, line)) {
if (line.empty()) {
grids.emplace_back();
continue;
} else {
grids.back().push_back(std::move(line));
}
}
return grids;
}

} // namespace aoc::day13

0 comments on commit 8d73353

Please sign in to comment.