-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#.##..##. | ||
..#.##.#. | ||
##......# | ||
##......# | ||
..#.##.#. | ||
..##..##. | ||
#.#.##.#. | ||
|
||
#...##..# | ||
#....#..# | ||
..##..### | ||
#####.##. | ||
#####.##. | ||
..##..### | ||
#....#..# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |