Add solution for "Day 24: Crossed Wires", part 2
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
|
||||
#include <bitset>
|
||||
#include <forward_list>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <aoc/common/LogicGate.hpp>
|
||||
@@ -33,13 +35,31 @@ class CrossedWires
|
||||
virtual void finish() override;
|
||||
private:
|
||||
static constexpr char getInputWireDelimiter();
|
||||
static constexpr char getInput1WiresChar();
|
||||
static constexpr char getOutputWiresChar();
|
||||
bool isProcessingInputWires_;
|
||||
std::unordered_map<std::string, bool> wires_;
|
||||
std::forward_list<LogicGate> unevaluatedGates_;
|
||||
// Uses input wire + gate kind as keys.
|
||||
std::unordered_map<std::string, std::shared_ptr<LogicGate>> gatesByInput_;
|
||||
// Uses output wire as keys.
|
||||
std::unordered_map<std::string, std::shared_ptr<LogicGate>> gatesByOutput_;
|
||||
std::forward_list<std::shared_ptr<LogicGate>> unevaluatedGates_;
|
||||
std::bitset<64> z_;
|
||||
std::set<std::string> swappedOutputWires_;
|
||||
void processInputWire(const std::string& line);
|
||||
void processLogicGate(const std::string& line);
|
||||
bool tryEvaluateLogicGate(const LogicGate& gate);
|
||||
void setOutputWire(const std::string& wire, const bool value);
|
||||
void validateFullAdderWires();
|
||||
// Returns a tuple containing a bool indicating whether validation was successful, the name of the output wire from
|
||||
// the AND gate, and the name of the wire from the XOR gate.
|
||||
std::tuple<bool, std::string, std::string> validateAndXorGatePair(const std::string& inputWire);
|
||||
// Returns a tuple containing a bool indicating whether validation was successful, the name of the output wire from
|
||||
// the AND gate, and the name of the wire from the XOR gate.
|
||||
std::tuple<bool, std::string, std::string> validateAndXorGatePair(const std::string& inputWire1,
|
||||
const std::string& inputWire2);
|
||||
// Returns a tuple containing a bool indicating whether validation was successful, and the name of the output wire
|
||||
// from the OR gate.
|
||||
std::pair<bool, std::string> validateOrGate(const std::string& inputWire1, const std::string& inputWire2);
|
||||
void swapOutputWires(std::string& outputWire1, std::string& outputWire2);
|
||||
};
|
||||
|
||||
@@ -22,11 +22,13 @@ class LogicGate
|
||||
{
|
||||
public:
|
||||
enum class Kind { And, Or, Xor, Unknown };
|
||||
static std::string getKindString(const Kind kind);
|
||||
|
||||
std::string inputWire1;
|
||||
std::string inputWire2;
|
||||
std::string outputWire;
|
||||
Kind kind{ Kind::Unknown };
|
||||
std::string getKindString() const;
|
||||
};
|
||||
|
||||
std::istream& operator>>(std::istream& is, LogicGate& logicGate);
|
||||
|
||||
Reference in New Issue
Block a user