|
1 | 1 | #include "aoc18/day16.h"
|
2 | 2 |
|
| 3 | +#include <algorithm> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include "absl/strings/numbers.h" |
| 8 | +#include "absl/strings/str_split.h" |
| 9 | + |
3 | 10 | namespace aoc18 {
|
4 |
| -namespace day16 {} // namespace day16 |
| 11 | +namespace day16 { |
| 12 | + |
| 13 | +using Opcode = std::function<Registers(Registers, int, int, int)>; |
| 14 | +Registers AddR(Registers r, int a, int b, int c) { r[c] = r[a] + r[b]; return r; } |
| 15 | +Registers AddI(Registers r, int a, int b, int c) { r[c] = r[a] + b; return r; } |
| 16 | +Registers MulR(Registers r, int a, int b, int c) { r[c] = r[a] * r[b]; return r; } |
| 17 | +Registers MulI(Registers r, int a, int b, int c) { r[c] = r[a] * b; return r; } |
| 18 | +Registers BanR(Registers r, int a, int b, int c) { r[c] = r[a] & r[b]; return r; } |
| 19 | +Registers BanI(Registers r, int a, int b, int c) { r[c] = r[a] & b; return r; } |
| 20 | +Registers BorR(Registers r, int a, int b, int c) { r[c] = r[a] | r[b]; return r; } |
| 21 | +Registers BorI(Registers r, int a, int b, int c) { r[c] = r[a] | b; return r; } |
| 22 | +Registers SetR(Registers r, int a, int _, int c) { r[c] = r[a] ; return r; } |
| 23 | +Registers SetI(Registers r, int a, int _, int c) { r[c] = a ; return r; } |
| 24 | +Registers GtIR(Registers r, int a, int b, int c) { r[c] = a > r[b]; return r; } |
| 25 | +Registers GtRI(Registers r, int a, int b, int c) { r[c] = r[a] > b; return r; } |
| 26 | +Registers GtRR(Registers r, int a, int b, int c) { r[c] = r[a] > r[b]; return r; } |
| 27 | +Registers EqIR(Registers r, int a, int b, int c) { r[c] = a == r[b]; return r; } |
| 28 | +Registers EqRI(Registers r, int a, int b, int c) { r[c] = r[a] == b; return r; } |
| 29 | +Registers EqRR(Registers r, int a, int b, int c) { r[c] = r[a] == r[b]; return r; } |
| 30 | + |
| 31 | + |
| 32 | +std::optional<Registers> ParseRegisters(const std::string& line) { |
| 33 | + static const std::regex re(R"(^\w+:\s+\[(\d+), (\d+), (\d+), (\d+)\]$)"); |
| 34 | + std::smatch match; |
| 35 | + if (!std::regex_match(line, match, re)) { |
| 36 | + return std::nullopt; |
| 37 | + } |
| 38 | + if (match.size() != 5) { |
| 39 | + return std::nullopt; |
| 40 | + } |
| 41 | + Registers reg; |
| 42 | + if (!absl::SimpleAtoi(match[1].str(), ®[0])) { return std::nullopt; } |
| 43 | + if (!absl::SimpleAtoi(match[2].str(), ®[1])) { return std::nullopt; } |
| 44 | + if (!absl::SimpleAtoi(match[3].str(), ®[2])) { return std::nullopt; } |
| 45 | + if (!absl::SimpleAtoi(match[4].str(), ®[3])) { return std::nullopt; } |
| 46 | + return reg; |
| 47 | +} |
| 48 | + |
| 49 | +std::optional<Instruction> ParseInstruction(const std::string& line) { |
| 50 | + const std::vector<absl::string_view> parts = absl::StrSplit(line, ' '); |
| 51 | + if (parts.size() != 4) { |
| 52 | + return std::nullopt; |
| 53 | + } |
| 54 | + Instruction ins; |
| 55 | + if (!absl::SimpleAtoi(parts[0], &ins.code)) { return std::nullopt; } |
| 56 | + if (!absl::SimpleAtoi(parts[1], &ins.a)) { return std::nullopt; } |
| 57 | + if (!absl::SimpleAtoi(parts[2], &ins.b)) { return std::nullopt; } |
| 58 | + if (!absl::SimpleAtoi(parts[3], &ins.c)) { return std::nullopt; } |
| 59 | + return ins; |
| 60 | +} |
| 61 | + |
| 62 | +std::optional<std::vector<Sample>> ParseSamples(const std::vector<std::string>& lines) { |
| 63 | + std::vector<Sample> samples; |
| 64 | + int idx{0}; |
| 65 | + while (idx < lines.size()) { |
| 66 | + if (lines[idx][0] != 'B') { |
| 67 | + // This means we're done parsing the section of the input that deals with |
| 68 | + // samples. |
| 69 | + break; |
| 70 | + } |
| 71 | + const auto maybe_before = ParseRegisters(lines[idx]); |
| 72 | + if (!maybe_before.has_value()) { |
| 73 | + return std::nullopt; |
| 74 | + } |
| 75 | + const auto maybe_instruction = ParseInstruction(lines[idx + 1]); |
| 76 | + if (!maybe_before.has_value()) { |
| 77 | + return std::nullopt; |
| 78 | + } |
| 79 | + const auto maybe_after = ParseRegisters(lines[idx + 2]); |
| 80 | + if (!maybe_after.has_value()) { |
| 81 | + return std::nullopt; |
| 82 | + } |
| 83 | + samples.emplace_back(Sample{ |
| 84 | + .before = maybe_before.value(), |
| 85 | + .after = maybe_after.value(), |
| 86 | + .instruction = maybe_instruction.value() |
| 87 | + }); |
| 88 | + idx += 4; |
| 89 | + } |
| 90 | + return samples; |
| 91 | +} |
| 92 | + |
| 93 | +int CountPotentialOpcodes(const Sample& sample) { |
| 94 | + static const std::vector<Opcode> ops{ |
| 95 | + AddR, |
| 96 | + AddI, |
| 97 | + MulR, |
| 98 | + MulI, |
| 99 | + BanR, |
| 100 | + BanI, |
| 101 | + BorR, |
| 102 | + BorI, |
| 103 | + SetR, |
| 104 | + SetI, |
| 105 | + GtIR, |
| 106 | + GtRI, |
| 107 | + GtRR, |
| 108 | + EqIR, |
| 109 | + EqRI, |
| 110 | + EqRR |
| 111 | + }; |
| 112 | + return std::count_if( |
| 113 | + ops.begin(), |
| 114 | + ops.end(), |
| 115 | + [&sample](const Opcode& op) { |
| 116 | + return op( |
| 117 | + sample.before, |
| 118 | + sample.instruction.a, |
| 119 | + sample.instruction.b, |
| 120 | + sample.instruction.c |
| 121 | + ) == sample.after; |
| 122 | + } |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +int Part1(const std::vector<Sample>& samples) { |
| 127 | + return std::count_if( |
| 128 | + samples.begin(), |
| 129 | + samples.end(), |
| 130 | + [](const Sample& s) { return CountPotentialOpcodes(s) >= 3; } |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace day16 |
5 | 135 | } // namespace aoc18
|
0 commit comments