Skip to content

Commit

Permalink
2024 day 25: solve part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
yut23 committed Jan 28, 2025
1 parent 2fa62f8 commit e32a472
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 2024/answer_tests/day25/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Day 25:
3
39 changes: 39 additions & 0 deletions 2024/input/day25/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#####
.####
.####
.####
.#.#.
.#...
.....

#####
##.##
.#.##
...##
...#.
...#.
.....

.....
#....
#....
#...#
#.#.#
#.###
#####

.....
.....
#.#..
###..
###.#
###.#
#####

.....
.....
.....
#....
#.#..
#.#.#
#####
41 changes: 41 additions & 0 deletions 2024/src/day25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/******************************************************************************
* File: day25.cpp
*
* Author: Eric T. Johnson (yut23)
* Created: 2025-01-27
*****************************************************************************/

#include "day25.hpp"
#include "lib.hpp"
#include <fstream> // for ifstream
#include <iostream> // for cout

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

auto [keys, locks] = aoc::day25::read_input(infile);

int count = 0;
for (const auto &lock : locks) {
if constexpr (aoc::DEBUG) {
std::cerr << "checking " << lock << ":\n";
}
for (const auto &key : keys) {
if constexpr (aoc::DEBUG) {
std::cerr << " " << key << "";
}
if (lock.check_fit(key)) {
if constexpr (aoc::DEBUG) {
std::cerr << ": fits";
}
++count;
}
if constexpr (aoc::DEBUG) {
std::cerr << "\n";
}
}
}
std::cout << count << "\n";

return 0;
}
89 changes: 89 additions & 0 deletions 2024/src/day25.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/******************************************************************************
* File: day25.hpp
*
* Author: Eric T. Johnson (yut23)
* Created: 2025-01-27
*****************************************************************************/

#ifndef DAY25_HPP_DOJZBHTX
#define DAY25_HPP_DOJZBHTX

#include "unit_test/pretty_print.hpp" // for repr

#include <array> // for array
#include <iostream> // for istream
#include <string> // for string, getline
#include <utility> // for pair
#include <vector> // for vector

namespace aoc::day25 {

struct Schematic {
bool is_key;
std::array<char, 5> pins;

bool check_fit(const Schematic &other) const {
if (this->is_key == other.is_key) {
return false;
}
if (this->is_key) {
return other.check_fit(*this);
}
// *this is a lock, other is a key
for (unsigned int i = 0; i < pins.size(); ++i) {
if (pins[i] + other.pins[i] > 5) {
return false;
}
}
return true;
}
};

std::istream &operator>>(std::istream &is, Schematic &schematic) {
Schematic tmp{};
std::string line;
// consume blank line if present
is >> std::ws;
if (!std::getline(is, line)) {
return is;
}
tmp.is_key = line[0] == '.';
for (int i = 0; i < 5; ++i) {
if (!std::getline(is, line)) {
break;
}
for (int j = 0; j < 5; ++j) {
tmp.pins[j] += line[j] == '#';
}
}
std::getline(is, line);
if (is) {
std::swap(schematic, tmp);
}
return is;
}

std::ostream &operator<<(std::ostream &os, const Schematic &schematic) {
os << (schematic.is_key ? "key" : "lock") << " ";
os << pretty_print::repr(schematic.pins, {.char_as_number = true});
return os;
}

std::pair<std::vector<Schematic>, std::vector<Schematic>>
read_input(std::istream &is) {
std::vector<Schematic> keys;
std::vector<Schematic> locks;
Schematic schematic;
while (is >> schematic) {
if (schematic.is_key) {
keys.push_back(std::move(schematic));
} else {
locks.push_back(std::move(schematic));
}
}
return {keys, locks};
}

} // namespace aoc::day25

#endif /* end of include guard: DAY25_HPP_DOJZBHTX */

0 comments on commit e32a472

Please sign in to comment.