-
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
4 changed files
with
171 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,2 @@ | ||
Day 25: | ||
3 |
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,39 @@ | ||
##### | ||
.#### | ||
.#### | ||
.#### | ||
.#.#. | ||
.#... | ||
..... | ||
|
||
##### | ||
##.## | ||
.#.## | ||
...## | ||
...#. | ||
...#. | ||
..... | ||
|
||
..... | ||
#.... | ||
#.... | ||
#...# | ||
#.#.# | ||
#.### | ||
##### | ||
|
||
..... | ||
..... | ||
#.#.. | ||
###.. | ||
###.# | ||
###.# | ||
##### | ||
|
||
..... | ||
..... | ||
..... | ||
#.... | ||
#.#.. | ||
#.#.# | ||
##### |
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,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; | ||
} |
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,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 */ |